0
votes

I'm trying to triangulate a convex polygon.

screenshot

On the left are the triangulated polygons, while on the right is the convex polygon without triangulation. (both are wrong) but the one on the right is in the correct position and besides the fact that it's missing the convex part, it's pretty ok...

I create the triangles and add them to an arraylist

    float[] vertices = polygonObject.getPolygon().getTransformedVertices();
    float[] worldVertices = new float[vertices.length];

    for (int i = 0; i < vertices.length; ++i) {
        System.out.println(vertices[i]);
        worldVertices[i] = vertices[i] / ppt;
    }

    // Make triangles
    Vector<float[]> trianglesVertices = new Vector<float[]>();

    EarClippingTriangulator triangulator = new EarClippingTriangulator();
    ArrayList<PolygonShape> triangles = new ArrayList<PolygonShape>();

    ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);

    for (int i = 0; i < pointsCoords.size / 6; i++)
    {
        trianglesVertices.add(new float[] {
                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),
        });

        PolygonShape triangleShape = new PolygonShape();
        triangleShape.set(trianglesVertices.get(i));
        triangles.add(triangleShape);
    }

later I loop that arraylist and create bodies, but somehow libgdx(probably me, not libgdx) totally messes it up.

            for(PolygonShape triangle:triangles){
                BodyDef bd = new BodyDef();
                bd.type = BodyDef.BodyType.StaticBody;
                Body body = world.createBody(bd);
                body.createFixture(triangle, 1);

                bodies.add(body);

                triangle.dispose();
            }

Here is the shape I am trying to render in libgdx (created in Tiled) correct shape

1
@LearnCocos2D thank you for editing the question, but I put libgdx in the title because I use EarClippingTriangulator class from the libgdx framework, and I thought it might be important to mention that.Mars

1 Answers

1
votes

I found another example and it worked!

https://github.com/MobiDevelop/maps-editor/blob/master/maps-basic/src/com/mobidevelop/maps/basic/BasicMapRenderer.java#L151

the way I was creating the triangles from this array was wrong

ShortArray pointsCoords = triangulator.computeTriangles(worldVertices);

here is the code that does create the triangles correctly,

    for (int i = 0; i < pointsCoords.size; i += 3) {
        int v1 = pointsCoords.get(i) * 2;
        int v2 = pointsCoords.get(i + 1) * 2;
        int v3 = pointsCoords.get(i + 2) * 2;

        PolygonShape triangleShape = new PolygonShape();
        triangleShape.set(
                new float[]{
                        worldVertices[v1 + 0],
                        worldVertices[v1 + 1],
                        worldVertices[v2 + 0],
                        worldVertices[v2 + 1],
                        worldVertices[v3 + 0],
                        worldVertices[v3 + 1]
                }

        );
        triangles.add(triangleShape);
    }