Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Creation of a tiled map editor polyline in box2d is not working correctly

$
0
0

I’m currently trying to create a box2d polyline object in my libgdx game, but it’s not working as expected.

To get a better picture of my subject, have a look at how it should look like, and how it actually looks:

enter image description here

this is an overview of the relevant code:

code overview

where green is the working “ground” code, and red is the not yet working polyline code.

I got the polyline-creation method from Conner Anderson,

LibGDX – Box2D/TiledMap – Parsing an Object Layer From TiledMap Part 3

and it brought me to the point where I can print out the correct coordinates of the startpoint (next to santa) and endpoint (not in screen) of the polyline (see left bottom of second screenshot).

So the polyline is correctly read from the .tmx file, but the creation of the box2d is broken.

Here’s the code in code-form:

public class B2DLevel2 {
private static final String TAG = B2DLevel2.class.getName();

private Main main;

public static Ground ground;
public static NoFrictionWalls noFrictionWalls;

private float x, y;

public TiledMap map;
public Body body;

public B2DLevel2(Main main){
    this.main = main;

    x = 0;
    y = 0;

    map = GameScreen.map;

    BodyDef bdef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fdef = new FixtureDef();

    ground = new Ground();

    noFrictionWalls = new NoFrictionWalls();

    createLevel2B2D(GameScreen.santa, map, x,y, bdef, shape, fdef);

}

public void createLevel2B2D(Santa santa,TiledMap map, float x,float y,BodyDef bdef,PolygonShape shape, FixtureDef fdef) {


    //ground is in layer 2, iceground in 9, poly in l4
    ground.createGround(map, bdef, shape, fdef);

The Ground class:

public class Ground {
private static final String TAG = Ground.class.getName();

private static ChainShape createPolyline(PolylineMapObject polyline){

    float[] vertices = polyline.getPolyline().getTransformedVertices();
    Vector2[] worldVertices = new Vector2[vertices.length/2];

    for(int i = 0; i < worldVertices.length; i++){
        worldVertices[i] = new Vector2(vertices[i*2]/Constants.PPM, vertices[i*2+1]/Constants.PPM);

        Gdx.app.log("Ground.createPolyline() ","x " + vertices[i*2]/Constants.PPM);
        Gdx.app.log("Ground.createPolyline() ","y " + vertices[i*2+1]/Constants.PPM);
    }

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


public void createGround(TiledMap map, BodyDef bdef, PolygonShape shape, FixtureDef fdef){
    Body body;

    //create ground
    for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bdef.type = BodyDef.BodyType.StaticBody;
        bdef.position.set((rect.getX() + rect.getWidth() / 2)/ Constants.PPM , (rect.getY() + rect.getHeight() / 2) /Constants.PPM );

        body = GameScreen.world.createBody(bdef);
        body.setUserData(this);

        shape.setAsBox((rect.getWidth() / 2) / Constants.PPM, (rect.getHeight() / 2) / Constants.PPM);
        fdef.shape = shape;
        fdef.friction = 9f;
        body.createFixture(fdef);
    }

    //create polyline
    for(MapObject object : map.getLayers().get(14).getObjects().getByType(PolylineMapObject.class)){
        Shape gShape;
        bdef.type = BodyDef.BodyType.StaticBody;

        gShape = createPolyline((PolylineMapObject) object);

        body = GameScreen.world.createBody(bdef);
        body.setUserData(this);

        fdef.shape = gShape;
        fdef.friction = 0.1f;
        body.createFixture(fdef);
    }

After an extensive research on the web, i’ve decided to put my first question on stack, hope everything is understandable! :)

So, thanks in advance and best regards,

mat


Viewing all articles
Browse latest Browse all 434

Trending Articles