I manage to do that yesterday with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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