0

I am writing a game using libGDX and I need detection of polygons, but the Intersector.overlapConvexPolygons method returns not at all what it should if it concerns complex shapes.

Based on this, what is the point of this method if it returns the same as the Rectangle check?

Maybe I'm doing something wrong?

public Vector getOverlap(Entity a, Entity b){
    Vector v=new Vector(); //my implementation of Vector2 class
    Intersector.MinimumTranslationVector mv=new Intersector.MinimumTranslationVector();
    if(Intersector.overlapConvexPolygons(a.getBounds(), b.getBounds(), mv)){
        v.set(mv.normal.x, mv.normal.y).scl(mv.depth);
    }
    return v;
}

Screenshots below: enter image description here enter image description here enter image description here

Community
  • 1
  • 1
Asidert
  • 131
  • 5
  • Couple of questions: Are your polygons ounter-clockwise wound convex polygons? Are the white lines suposed to be perfectly wrapping the black "spikes"? If the white lines I see on the images are for debugging your polygon creation may need a fix – Luis Fernando Frontanilla Jan 09 '20 at 09:27
  • @LuisFernandoFrontanilla after spending a couple of hours testing, most likely there is a mistake during the creation of polygon. – Asidert Jan 09 '20 at 10:16
  • It would be great if you could share the portion of code where you create the polygon – Luis Fernando Frontanilla Jan 09 '20 at 18:33

1 Answers1

0

Your polygons are most likely not convex, polygons are defined as convex if the lines defining the polygon do not intersect when the vector lines are extended.

Difference between concave and convex polygons diagramatically

To use concave polygons within LibGDX, one can split a concave polygon into triangles using the EarClippingTriangulator class included with LibGDX. An example of how to use this class to split a complex polygon can be found in this question.

Once the Polygon has been triangulated, one can create an object to encapsulate the entire set of triangles, with its own collision method that iteratively checks if the other Polygon object is overlapping with any of the constituent Polygons making up the whole object.

public class ConcavePolygon {
    ...

    public boolean isColliding(Polygon polygon) {
        for(Polygon triangle : triangles) {
            if(Intersector.overlapConvexPolygons(polygon, triangle, mv)) {
                return true; //Returns true if any triangles overlap polygon
            }
        }

        return false; //Returns false if none overlap
    }

    public boolean isColliding(ConvexPolygon convexPolygon) {
        for(Polygon triangle : triangles) {
            if(convexPolygon.isColliding(triangle)) {
                return true; //Returns true if any triangles overlap convex polygon
            }
        }

        return false; //Returns false if none overlap
    }
}

Ammar Tarajia
  • 177
  • 1
  • 13