0
votes

So, in my game I have a box2D dynamic body with a polygon shape as my player and a box2D static body with a chain shape as a static box. The debug renderer is showing all the boxes correctly. When the player collides with the box from the top, it collides, but when from either side, it goes right trough. I have tried to use polygons for the static boxes, but it acts weirdly. I can't figure out what's wrong.

Here is the code:

player body and fixture definition:

        bdef=new BodyDef();
        bdef.position.set((WIDTH/2+169)/PPM,100/PPM);
        bdef.type=BodyType.DynamicBody;
        body=world.createBody(bdef);

        PolygonShape shape=new PolygonShape();
        shape.setAsBox(16/PPM,16/PPM);

        fdef=new FixtureDef();
        fdef.shape=shape;
        fdef.filter.categoryBits=B2DVars.BIT_PLAYER;
        fdef.filter.maskBits=B2DVars.BIT_BLOCK;
        body.createFixture(fdef).setUserData("player");

static box body and fixture definition:

            bdef=new BodyDef();
            fdef=new FixtureDef();
            bdef.type=BodyType.StaticBody;
            bdef.position.set((col+0.5f)*tileSize/PPM,(row+0.5f)*tileSize/PPM);

            ChainShape chainShape=new ChainShape();
            Vector2[] v=new Vector2[5];
            v[0]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
            v[1]=new Vector2(tileSize/2/PPM,-tileSize/2/PPM);
            v[2]=new Vector2(tileSize/2/PPM,tileSize/2/PPM);
            v[3]=new Vector2(-tileSize/2/PPM,tileSize/2/PPM);
            v[4]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
            chainShape.createChain(v);

            fdef.friction=0;
            fdef.shape=chainShape;
            fdef.isSensor=false;
            fdef.filter.categoryBits=B2DVars.BIT_BLOCK;
            fdef.filter.maskBits=B2DVars.BIT_PLAYER;
            world.createBody(bdef).createFixture(fdef).setUserData("block");

EDIT: The problem is no more, it was a problem with me setting the player position by transforming, but now I don't know, how to else make the player move when I push a button and stop when I release it?

2
Can you add the code that deals with the collisions?asherbar
It's Box2D. Isn't the colliding done in the libraries already?user4441930
You can add a ContactListener to your box2d world where you deal with the collisions (as explained in the wiki: github.com/libgdx/libgdx/wiki/box2d#contact-listeners). From your comment I understand you did not do this.asherbar

2 Answers

0
votes

Moving a DynamicBody should not be done via setTransform. The smoothest way to set a DynamicBody position is via MouseJoint, but whether or not it'll help you depends on what kind of movement you are after. This example is an excellent place to start.

0
votes

Use contact listener in this way to check the collision

'if((contact.getFixtureA().getBody() == bodyEdgeScreen &&
                        contact.getFixtureB().getBody() == body2)
                        ||
                        (contact.getFixtureA().getBody() == body2 &&
                                contact.getFixtureB().getBody() == bodyEdgeScreen))'