我正在使用LibGDX在Android Studio中制作游戏。我的播放器应该上下飞行,并相应地调整角度(向上倾斜时向上/向下,向下倾斜时向前/向下)。我对此有两个问题:
这是我的玩家的定义:
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