Reputation: 71
Im trying to create a small Side scroller for Android with LibGdx Tiled and Box2D. I'm using Object Layer to get the collision between Player and the World. This is working fine if i use Rectangles for the Object Layers. But when I'm trying to use Polygones the collision isn't working. What am I doing wrong. (Sry for spelling mistakes)
Here is my code:
for(MapObject object : map.getLayers().get(5).getObjects()){
if(object instanceof RectangleMapObject) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set((rect.getX() + rect.getWidth() / 2) / AoF.PPM, (rect.getY() + rect.getHeight() / 2) / AoF.PPM);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2 / AoF.PPM, rect.getHeight() / 2 / AoF.PPM);
fdef.shape = shape;
body.createFixture(fdef);
}
if(object instanceof PolygonMapObject){
float[] vertices = ((PolygonMapObject) object).getPolygon().getTransformedVertices();
float[] worldVertices = new float[vertices.length];
for (int i = 0; i < vertices.length; ++i) {
worldVertices[i] = vertices[i] / AoF.PPM;
}
shape.set(worldVertices);
bdef.type = BodyDef.BodyType.StaticBody;
body = world.createBody(bdef);
fdef.shape = shape;
body.createFixture(fdef);
}
Upvotes: 2
Views: 1894
Reputation: 71
I finaly fixed it myself.
for(MapObject object : map.getLayers().get(5).getObjects()){
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;
}
bdef.type = BodyDef.BodyType.StaticBody;
body = world.createBody(bdef);
fdef.shape = shape;
body.createFixture(fdef);
}
private static PolygonShape getRectangle(RectangleMapObject rectangleObject) {
Rectangle rectangle = rectangleObject.getRectangle();
PolygonShape polygon = new PolygonShape();
Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / AoF.PPM,
(rectangle.y + rectangle.height * 0.5f ) / AoF.PPM);
polygon.setAsBox(rectangle.width * 0.5f /AoF.PPM,
rectangle.height * 0.5f / AoF.PPM,
size,
0.0f);
return polygon;
}
private static CircleShape getCircle(CircleMapObject circleObject) {
Circle circle = circleObject.getCircle();
CircleShape circleShape = new CircleShape();
circleShape.setRadius(circle.radius / AoF.PPM);
circleShape.setPosition(new Vector2(circle.x / AoF.PPM, circle.y / AoF.PPM));
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] / AoF.PPM;
}
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] / AoF.PPM;
worldVertices[i].y = vertices[i * 2 + 1] / AoF.PPM;
}
ChainShape chain = new ChainShape();
chain.createChain(worldVertices);
return chain;
}
Upvotes: 3