I would like to have a way to make maps in a map editor (tiled) and be able to add box2d collision detection to it. I have already seen Aurelion Ribon’s program, however that doesn’t seem to work with large map images. Is there a simple way to do this? If not, is there anyway you would suggest it? While looking around, I found that you can export a an xml file, so I was thinking that maybe in runtime I make my program look at the tiles and if the id equals one that is collidable add a box2d body around it? This is a topdown game as well. Anything helps! Thanks, -cMp
Define a box2d body for each map tile.
http://dpk.net/2011/05/08/libgdx-box2d-tiled-maps-full-working-example-part-23/#p1
Uses the outdated map api but everything else is the same.
That’s what I was thinking, but would it cause performance or memory issues due to the large number of tiles on the map?
I doubt it, as long as the bodies are static.
I manage to do that yesterday with this:
BodyDef def = new BodyDef();
PolygonShape shape = new PolygonShape();
int current_body = 0;
for (MapObject object : mapa.getLayers().get(1).getObjects()) {
if (object instanceof RectangleMapObject) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
shape.setAsBox((rect.width / 2) / 10, (rect.height / 2) / 10);
def.position.x = (rect.x / 10) + ((rect.width / 10) / 2);
def.position.y = (rect.y / 10) + ((rect.height / 10) / 2);
cuerpos.add(world.createBody(def));
cuerpos.get(current_body).createFixture(shape, 1);
}
else if (object instanceof PolygonMapObject) {
Polygon poly = ((PolygonMapObject) object).getPolygon();
float vertices[] = poly.getTransformedVertices();
for (int x = 0; x < vertices.length; x++) {
vertices[x] /= 10;
}
System.out.println(vertices[0]);
shape.set(vertices);
def.position.x = poly.getOriginX() / 10;
def.position.y = poly.getOriginY() / 10;
cuerpos.add(world.createBody(def));
cuerpos.get(current_body).createFixture(shape, 1);
}
current_body++;
}
This code works only for rectangles and polygons but if think a bit you will be able to make it ellipse and polyline friendly
It’s not too late :), thank you very much for that, you’ve been really helpful!