1
\$\begingroup\$

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<Body> bodies;

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

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

    bodies = new Array<Body>();

    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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Finally figured out a way to do it.

I convert the verticies of the box2d poly from orthagraphic to isometric.

I edited the method from above to look like this. (The +320 and +175 to the co-ordinates was to compensate for a offset I had)

    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;

        // Set 2d verts to Iso + deal with the offset
        Vector2 vel = Iso.TwoDToIso(new Vector2(worldVertices[i].x,worldVertices[i].y ));
        worldVertices[i].x = vel.x + 320 ;
        worldVertices[i].y = vel.y+ 175;
    }

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

I call this new function to convert the verts for me

    public static Vector2 TwoDToIso(Vector2 point){
    Vector2 vel2 = new Vector2();

    vel2.x = point.x - point.y;
    vel2.y = -(point.x + point.y) / 2;
    return vel2;
}
\$\endgroup\$

You must log in to answer this question.

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