简体   繁体   中英

Bodies not colliding properly in Box2D(LibGDX)

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?

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.

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))'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM