简体   繁体   English

libGDX Box2D碰壁时停止移动

[英]libGDX Box2D Stop Move When Touching Wall

在此处输入图片说明

This happen when my Body object touching with wall and I hold right key. 当我的Body对象与墙壁接触并按住右键时,会发生这种情况。 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: World定义:

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

Create ground body from tilemap: 从tilemap创建ground物体:

//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: Player身体定义:

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: 我的ContactListener我的beginContact()方法:

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 他的源代码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. 您可以使用Mario项目对此进行测试,如果您增加了冲动,也会卡在墙上。

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". 为了解决这个问题,要么降低冲动,降低摩擦(但是您可能需要停止Mario的代码),或者代替施加线性冲动,您可以施加火炬并使用圆形让玩家“滚动”。

None of these solutions are perfect, but you can try and see which one suits you most. 这些解决方案都不是完美的,但是您可以尝试看看哪种最适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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