Circular and polygonal collision with Libgdx

Is there a way in Libgdx to check for a collision between a polygon and a circle?

I saw the Intersector class, but found only a collision for Circle and Rectangle. What about any other Polygon?

If I need to do this manually, what's the best way to do this with Libgdx?

+8
java polygon collision-detection circle libgdx
source share
3 answers

So, I managed to create a method for checking collisions between a circle and a polygon. At least it works for me.

Here is the code:

 public boolean overlaps(Polygon polygon, Circle circle) { float []vertices=polygon.getTransformedVertices(); Vector2 center=new Vector2(circle.x, circle.y); float squareRadius=circle.radius*circle.radius; for (int i=0;i<vertices.length;i+=2){ if (i==0){ if (Intersector.intersectSegmentCircle(new Vector2(vertices[vertices.length-2], vertices[vertices.length-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius)) return true; } else { if (Intersector.intersectSegmentCircle(new Vector2(vertices[i-2], vertices[i-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius)) return true; } } return false; } 
+10
source share

Unfortunately, I do not have enough reputation for comments, so I am adding this as another answer instead ...

Cristiano is great for checking that a circle overlaps one of the segments of a polygon line, but he does not verify that the more unusual case is when the circle is completely inside the polygon, which can happen if a small fast moving circle collides with a large polygon.

I tried the code below for Cristiano with a little modification to fix the problem ...

 public static boolean overlaps(Polygon polygon, Circle circle) { float []vertices=polygon.getTransformedVertices(); Vector2 center=new Vector2(circle.x, circle.y); float squareRadius=circle.radius*circle.radius; for (int i=0;i<vertices.length;i+=2){ if (i==0){ if (Intersector.intersectSegmentCircle(new Vector2(vertices[vertices.length - 2], vertices[vertices.length - 1]), new Vector2(vertices[i], vertices[i + 1]), center, squareRadius)) return true; } else { if (Intersector.intersectSegmentCircle(new Vector2(vertices[i-2], vertices[i-1]), new Vector2(vertices[i], vertices[i+1]), center, squareRadius)) return true; } } return polygon.contains(circle.x, circle.y); } 
+9
source share

... and follow Phil Anderson's wonderful answer, here is my version that just avoids creating new Vector2s every check and instead reuses static instances of Vector2.

 public class PolygonUtil { static final Vector2 center = new Vector2(); static final Vector2 vec1 = new Vector2(); static final Vector2 vec2 = new Vector2(); public static boolean overlaps(Polygon polygon, Circle circle) { float []vertices=polygon.getTransformedVertices(); center.set(circle.x, circle.y); float squareRadius=circle.radius*circle.radius; for (int i=0;i<vertices.length;i+=2){ if (i==0){ if (Intersector.intersectSegmentCircle(vec1.set(vertices[vertices.length - 2], vertices[vertices.length - 1]), vec2.set(vertices[i], vertices[i + 1]), center, squareRadius)) return true; } else { if (Intersector.intersectSegmentCircle(vec1.set(vertices[i-2], vertices[i-1]), vec2.set(vertices[i], vertices[i+1]), center, squareRadius)) return true; } } return polygon.contains(circle.x, circle.y); } 

}

0
source share

All Articles