2
\$\begingroup\$

I have several Shape objects - Circle, Rectangle and Polygon.

I use the built-in .intersects(Bounds1, Bounds2) method to detect collision but it's quite inaccurate.

Circle c1 = new Circle(20);
Bounds c1Bounds = c1.getBoundsInParent();
Rectangle r1 = new Rectangle(20,20);
Bounds r1Bounds = r1.getBoundsInParent();
... etc for other Shape objects

My collision code:

if (c1Bounds.intersects(r1Bounds) {
    //collision happened.
}

Why is the collision accuracy so bad and how can I improve it? The collision detection is REALLY bad for polygon objects. It is literally at least 50 pixels off.

\$\endgroup\$
1
  • \$\begingroup\$ Check out the answer provided by jewelsea here, his code works perfectly fine even with polygons and circles. \$\endgroup\$ Jul 18, 2018 at 9:55

1 Answer 1

2
\$\begingroup\$

getBoundsInParent() returns the bounding box around the shape — a rectangle.

The built-in intersects() check works only for rectangular shapes, not for circles, polygons, etc. For these, you'd need to implement the check yourself.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .