简体   繁体   中英

libGDX Box2D Stop Move When Touching Wall

在此处输入图片说明

This happen when my Body object touching with wall and I hold right key. So it is stop as long I hold right key.

When I release my right key it dropped. How to fix this problem?

Here my script:

World Definition:

this.world = new World(new Vector2(0, -9.8f), true);

Create ground body from tilemap:

//create body and fixture variables
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;

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

    x = Static.toMeter(rect.getX() + rect.getWidth() / 2);
    y = Static.toMeter(rect.getY() + rect.getHeight() / 2);
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(x, y);

    body = world.createBody(bdef);

    x = Static.toMeter(rect.getWidth() / 2);
    y = Static.toMeter(rect.getHeight() / 2);
    shape.setAsBox(x, y);
    fdef.shape = shape;
    fdef.filter.categoryBits = HookaHookaGame.GROUND_BIT;
    body.createFixture(fdef);
}

Player body definition:

BodyDef bodyDef = new BodyDef();
bodyDef.position.set(Static.toMeter(128), Static.toMeter(HookaHookaGame.HEIGHT));
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);

// Define mario shape
PolygonShape shape = new PolygonShape();
shape.setAsBox(Static.toMeter(32) / 2, Static.toMeter(32) / 2);

FixtureDef fixture = new FixtureDef();
fixture.shape = shape;

body.createFixture(fixture);

// Define foot shape
shape = new PolygonShape();
shape.setAsBox(Static.toMeter(32 / 4) / 2, Static.toMeter(32 / 4) / 2, new Vector2(0, Static.toMeter((-32 + 8) / 2)), 0);

fixture = new FixtureDef();
fixture.shape = shape;
// Create filter
fixture.filter.categoryBits = HookaHookaGame.MARIO_BIT;
fixture.filter.maskBits = HookaHookaGame.GROUND_BIT;

body.createFixture(fixture).setUserData(this);

My ContactListener here my beginContact() method:

int cDef;

Fixture a, b;
MySprite spriteA, spriteB;

a = contact.getFixtureA();
b = contact.getFixtureB();

cDef = a.getFilterData().categoryBits | b.getFilterData().categoryBits;

switch (cDef) {
    case HookaHookaGame.GROUND_BIT | HookaHookaGame.MARIO_BIT:
        System.out.println("Foot with ground");
        if(a.getUserData() != null) {
            spriteA = (MySprite) a.getUserData();
            spriteA.onHit();
        }

        if(b.getUserData() != null) {
            spriteB = (MySprite) b.getUserData();
            spriteB.onHit();
        }
        break;
}

Handling user input:

private void handleInput() {
    //control our player using immediate impulses
    if (Gdx.input.isKeyPressed(Input.Keys.D))
        player.moveRight();
    else if (Gdx.input.isKeyPressed(Input.Keys.A))
        player.moveLeft();
    else
        player.stopMove();

    if (Gdx.input.isKeyPressed(Input.Keys.SPACE))
        player.jump();
}

I read his tutorial and it seem he didn't do anything about it and it works fine. His source code GitHub

The problem is that your linear impulse is to strong.

You can test this with the Mario project, if you increase the impulse you will get stuck at walls, too.

To solve this either lower the impulse, lower the friction (but then you might need code that stops Mario) or instead of applying an linear impulse you could apply a torch and use a circle shape to let the player "roll".

None of these solutions are perfect, but you can try and see which one suits you most.

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