0
\$\begingroup\$

this is how I create the polygon in Tiledthis is how it gets rendered in libgdx

this is how I render the polygons in Tiled this is how they get rendered in libgdx. Notice the top part of the triangles is straight instead of being hollow. here is the code I use to convert the polygon object to a polygonshape

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

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

    polygon.set(worldVertices);

    BodyDef bd = new BodyDef();
    bd.type = BodyDef.BodyType.StaticBody;
    Body body = world.createBody(bd);
    body.createFixture(polygon, 1);
    polygon.dispose();

it this normal behavour for libgdx? to flatten the top of the polygons?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your problem is that the physics library, probably Box2D, supports only convex shapes. You are passing in a non-convex (concave) shape, and what you get as a result is that the convex hull of your shape is used as collision body.

To avoid this, you can either manually make sure your collision primitives are convex, or you can triangulate the polygon (there are probably libraries available for this, if libgdx does not have it) and use the triangles as fixtures.

\$\endgroup\$
2
  • \$\begingroup\$ you're right! thank you, do you know where I can find an example of libgdx code doing this? \$\endgroup\$
    – mars
    Commented Feb 2, 2015 at 14:02
  • 1
    \$\begingroup\$ That part seems to be covered by the following SO question: stackoverflow.com/questions/28054240/… \$\endgroup\$ Commented Feb 3, 2015 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .