簡體   English   中英

libGDX Box2D碰壁時停止移動

[英]libGDX Box2D Stop Move When Touching Wall

在此處輸入圖片說明

當我的Body對象與牆壁接觸並按住右鍵時,會發生這種情況。 所以只要我按住右鍵就可以停止。

當我松開右鍵時,它就掉了。 如何解決這個問題?

這是我的腳本:

World定義:

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

從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身體定義:

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

我的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;
}

處理用戶輸入:

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();
}

我閱讀了他的教程,看來他對此沒有做任何事情,並且工作正常。 他的源代碼GitHub

問題是您的線性沖力很大。

您可以使用Mario項目對此進行測試,如果您增加了沖動,也會卡在牆上。

為了解決這個問題,要么降低沖動,降低摩擦(但是您可能需要停止Mario的代碼),或者代替施加線性沖動,您可以施加火炬並使用圓形讓玩家“滾動”。

這些解決方案都不是完美的,但是您可以嘗試看看哪種最適合您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM