[LibGDX] Custom Box2D shape creation problem

Hi! I have a tmx file that contains all my level stuff.
In a layer i have created one polyline that is the border of all ground level, but my problem is transform my polyline in a box2D body.
Someone can suggest me a tutorial or a method to do this?
thanks in advice :slight_smile:

Take a look at the Physics Body Editor, it can autotrace shapes and has its own loader (not integrated with tiled maps, however).

As far as i know libgdx, you cannot get the vertices you need to create a Box2d-Shape directly from a MapObject. You can only get the Properities from the specific object.

So you can either store the vertices in your MapObjects properties, or you hardcode them.

To create a ground in your game you can use the ChainShape as specified here:
Libgdx JavaDoc

A nice Tutorial on how to create a ground with ChainShape:

Another possibility is to create a meta-data layer an color the tiles, you want to collide with. Then you can get the TiledMapTileLayer and from that get the Cell that is not null. This only works for box shaped grounds so maybe ChainShape fits you better in this case :).

If the MapObject is created from a “polyline” in Tiled, it will be a PolylineMapObject and if you cast it as such you can get the vertices.

it’s interesting, after i get the verticle how i have to do for transforn the coordinates in a body?
really thanks for all reply!

Thank you very much, i didn’t know that :).

i’m not able to cast in PolylineMapObject and i don’t find nothing on internet about this :’( if somebody knows how it can be doing please write below ;D

ok, i found how to do, here is the code :


PolylineMapObject polyline= (PolylineMapObject) map.getLayers().get("Layer_Name").getObjects().get("Polyline_Name");

for(int i=0; i<polyline.getPolyline().getVertices().length; i++)
	Gdx.app.log("INFO", "VAL : " + verticles[i]);