libgdxpolygontriangulationconvexconcave

Libgdx polygon triangulation


Ok, so I have a polygon (simple but concave) that I'm trying to cut into triangles to make it collide with an other polygon.

I knew my polygone was concave, so i decided to use LibGDX EarClippingTriangulator to manage to cut it into triangles.

So, with this code, I get my triangles vertices :

public void triangulate()
    {
        Vector<float[]> trianglesVertices = new Vector<float[]>();
        ShortArray pointsCoords = new ShortArray();
        EarClippingTriangulator triangulator = new EarClippingTriangulator();

        // Cut in triangles
        pointsCoords = triangulator.computeTriangles(this.getTransformedVertices());

        // Make triangles
        for (int i = 0; i < pointsCoords.size / 6; i++)
        {
            trianglesVertices.add(new float[] {
                    pointsCoords.get(i), pointsCoords.get(i+1),
                    pointsCoords.get(i+2), pointsCoords.get(i+3),
                    pointsCoords.get(i+4), pointsCoords.get(i+5),
            });
            Polygon triangle = new Polygon(trianglesVertices.get(i));
            triangles.add(triangle);
        }

        System.out.printf("Triangulation made %d triangles.\n", pointsCoords.size / 6);
    }

But when i try to draw thoses triangles I just made, they just stack in the 0,0 coord.. And, is it normal that all triangles seems almost the sames, I mean they all got the same orientation ?

I didn't found so much info about this trangulation use for libgdx Can you help ?

(Sorry for my english i'm french, and sorry for no pictures, i'm too young here)

EDIT: This is my polygon (in CCW)

hitbox.setVertices(new float[]{  
                this.getX() + 13, this.getY() - 60,
                this.getX() + 16, this.getY() - 74,
                this.getX() + 39, this.getY() - 74,
                this.getX() + 45, this.getY() - 105,
                this.getX() + 81, this.getY() - 105,
                this.getX() + 88, this.getY() - 74,
                this.getX() + 108, this.getY() - 74,
                this.getX() + 114, this.getY() - 61,
                this.getX() + 106, this.getY() - 30, // Top right
                this.getX() + 101, this.getY() - 29,
                this.getX() + 101, this.getY() - 57,
                this.getX() + 83, this.getY() - 62,
                this.getX() + 75, this.getY() - 50,
                this.getX() + 65, this.getY() - 4, // Top mid
                this.getX() + 62, this.getY() - 4, // Top mid
                this.getX() + 52, this.getY() - 50,
                this.getX() + 44, this.getY() - 62,
                this.getX() + 25, this.getY() - 56, 
                this.getX() + 25, this.getY() - 30,
                this.getX() + 19, this.getY() - 30,  // Top left
                });

EDIT2: Now i got enough point to show you the polygon here it is

enter image description here


Solution

  • The problem is with your loop:

            // Make triangles
            for (int i = 0; i < pointsCoords.size / 6; i++)
            {
                trianglesVertices.add(new float[] {
                        pointsCoords.get(i), pointsCoords.get(i+1),
                        pointsCoords.get(i+2), pointsCoords.get(i+3),
                        pointsCoords.get(i+4), pointsCoords.get(i+5),
                });
                Polygon triangle = new Polygon(trianglesVertices.get(i));
                triangles.add(triangle);
            }
    

    First triangle will have correct coordinates, but second one will will use 1, 2, 3, 4, 5, 6 elements of pointsCoords that doesn't make any sence. You should multiply i by 6 inside loop to take offset into account:

                        pointsCoords.get(i*6), pointsCoords.get(i*6 + 1),
                        pointsCoords.get(i*6 + 2), pointsCoords.get(i*6 + 3),
                        pointsCoords.get(i*6 + 4), pointsCoords.get(i*6 + 5),