1

Image of an example of issue

enter image description here

.intersects is called on both of these nodes and returns true. I used getBoundsInParent() for both. Can someone help me fix this issue. I feel it as something to do with getBoundsInParent() not returning accurate bounds for circles but idk. Thanks

Sai Dandem
  • 8,229
  • 11
  • 26
BigWinnz101
  • 61
  • 1
  • 5
  • Related: [Checking Collision of Shapes with JavaFX](https://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx). – jewelsea Dec 22 '21 at 03:42

1 Answers1

4

getBoundsInParent() returns a Bounds object. https://openjfx.io/javadoc/11/javafx.graphics/javafx/geometry/Bounds.html

Look at the properties of Bounds. It represents a bounding "box". It does not account for the curvature of a circle.

If you want to see if the Circle intersects the Rectangle. There are a few options. You could calculate the intersecting Shape with Shape.intersect(Shape shape1, Shape shape2) and see if the bounds of the result has a non-zero dimension.

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • 3
    You can also test "by hand". The closest point of a rectangle to a circle is always either a corner of the rectangle, or a point on a side of the rectangle where a line to the center of the circle is perpendicular to the side. So for a rectangle with horizontal and vertical sides, you can check the distance of each corner, the points on the two vertical sides with the same y-coordinate as the center (if any), and the points on the two horizontal sides with the same x-coordinate as the center (if any), to the center, and see if any of those six distances are less than the radius. – James_D Dec 21 '21 at 16:51