Box2D旋转问题

时间:2018-10-05 16:05:40

标签: java android libgdx

我正在使用LibGDX在Android Studio中制作游戏。我的播放器应该上下飞行,并相应地调整角度(向上倾斜时向上/向下,向下倾斜时向前/向下)。我对此有两个问题:

  1. 当玩家撞到地面且未达到最低角度时,它会先“反弹”一点,然后再停留在地面上。
  2. 当玩家击打地面或天花板时,它会保持原角度,并且不会像我想要的那样平放在地面上。

这是我的玩家的定义:

public void definePlayer()
{
    BodyDef bdef = new BodyDef();
    bdef.position.set(-3150 / JungleRush.PPM, -350 / JungleRush.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(330 / JungleRush.PPM, 130 / JungleRush.PPM);

    fdef.filter.categoryBits = JungleRush.PLAYER_BIT;
    fdef.filter.maskBits = JungleRush.CEILING_BIT | JungleRush.GROUND_BIT | JungleRush.ENEMY_BIT;

    fdef.shape = shape;

    b2body.createFixture(fdef);
}

这是一种使玩家倾斜的方法,在我的更新方法中被调用(ANGLE_UP为30度,ANGLE_DOWN为-35度):

public void anglePlayer()
{
    if(player.b2body.getLinearVelocity().y > 0)
    {
        if(player.b2body.getAngle() < ANGLE_UP && !isTouchingCeiling)
        {
            player.b2body.setAngularVelocity(player.b2body.getLinearVelocity().y * 0.25f);
        }
        else
        {
            player.b2body.setAngularVelocity(0f);
        }
    }
    else if(player.b2body.getLinearVelocity().y < 0)
    {
        if(player.b2body.getAngle() > ANGLE_DOWN && !isTouchingGround)
        {
            player.b2body.setAngularVelocity(player.b2body.getLinearVelocity().y * 0.25f);
        }
        else
        {
            player.b2body.setAngularVelocity(0f);
        }
    }

    spaceship.setRotation((player.b2body.getAngle() * RADIANS_TO_DEGREES) - 90);
}

(请忽略飞船参考,那是我为玩家拍摄的Sprite)

这是玩家在地面上休息时的样子: Player picture

0 个答案:

没有答案