0
votes

Does anyone have any suggestions on the best way to handle triangle intersections with rectangles and circles in LibGDX? I see the Intersector class has methods for testing intersections with triangles and rays, but I'm not sure how I can directly apply that to rectangles or circles. I also see there's an isPointInTriangle method, which I could perhaps use to cycle through the points of the triangle and rectangle?

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

Maybe I'm just missing an easy, already existing function within LibGDX? Any help would be very much appreciated.

1

1 Answers

1
votes

Triangles and rectangles are both considered to be polygons so use libGDX Polygon class to describe your triangles and rectangles and then use Intersector class to intersect them.

For circles you can either:

  1. Approximate it with polygon (say 10 points around in circle) and then use Intersector class.

  2. Create your own custom circle to polygon collision checker.

A simple algorithm to check polygon and circle collision that comes to mind:

if center of circle is inside the polygon =>>> they overlap.

else if distance between circle center and any of the polygons vertices is less then circle radius =>>> they overlap.

else =>>> they dont overlap.

This is considering you don't need to know what exactly is the overlapping portion of these figures and you only want to know if they overlap. If you need to know the overlapping portion then approximating your circle with polygon is the only way to go.