Saturday, April 7, 2018

libgdx - Box2d collision on isometric maps built in Tiled


I am trying to add Box2d collision to a map I created in Tiled http://www.mapeditor.org/


It all worked when I was creating standard othergraphic maps but now I have changed to isometric the objects layer (Where I am creating the bodies for box2d) no longer match the rendering from the IsometricTiledMapRenderer.


enter image description here


I created the collision using the MapBodyBuilder class (written by someone else much smarter) Code below:


public class MapBodyBuilder {


// The pixels per tile. If your tiles are 16x16, this is set to 16f
private static float ppt = 0;
public static Array bodies;

public static Array buildShapes(Map map, float pixels, World world) {
ppt = pixels;

MapObjects objects = map.getLayers().get("Obstacles").getObjects();

bodies = new Array();


for(MapObject object : objects) {

if (object instanceof TextureMapObject) {
continue;
}

Shape shape;

if (object instanceof RectangleMapObject) {

shape = getRectangle((RectangleMapObject)object);
}
else if (object instanceof PolygonMapObject) {
shape = getPolygon((PolygonMapObject)object);
}
else if (object instanceof PolylineMapObject) {
shape = getPolyline((PolylineMapObject)object);
}
else if (object instanceof CircleMapObject) {
shape = getCircle((CircleMapObject)object);

}
else {
continue;
}


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




bodies.add(body);

shape.dispose();
}
return bodies;
}


private static PolygonShape getRectangle(RectangleMapObject rectangleObject) {
Rectangle rectangle = rectangleObject.getRectangle();
PolygonShape polygon = new PolygonShape();
Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / ppt,
(rectangle.y + rectangle.height * 0.5f ) / ppt);
polygon.setAsBox(rectangle.width * 0.5f / ppt,
rectangle.height * 0.5f / ppt,
size,
0.0f);
return polygon;

}

private static CircleShape getCircle(CircleMapObject circleObject) {
Circle circle = circleObject.getCircle();
CircleShape circleShape = new CircleShape();
circleShape.setRadius(circle.radius / ppt);
circleShape.setPosition(new Vector2(circle.x / ppt, circle.y / ppt));
return circleShape;
}


private static PolygonShape getPolygon(PolygonMapObject polygonObject) {
PolygonShape polygon = new PolygonShape();
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;
}


polygon.set(worldVertices);
return polygon;
}

private static ChainShape getPolyline(PolylineMapObject polylineObject) {
float[] vertices = polylineObject.getPolyline().getTransformedVertices();
Vector2[] worldVertices = new Vector2[vertices.length / 2];

for (int i = 0; i < vertices.length / 2; ++i) {

worldVertices[i] = new Vector2();
worldVertices[i].x = vertices[i * 2] / ppt;
worldVertices[i].y = vertices[i * 2 + 1] / ppt;
}

ChainShape chain = new ChainShape();
chain.createChain(worldVertices);
return chain;
}


}


Any help would be greatly appreciated.


Thanks


pps I know this has been asked before generate box2d bodies from isometric tiled map


But I cannot find an answer.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...