【问题标题】:How to move apply force to bodies in a top down game?如何在自上而下的游戏中对身体施加力?
【发布时间】:2021-09-05 20:08:23
【问题描述】:

所以我正在制作一个玩家使用 WASD 键移动的自上而下的游戏。为此,我使用

if (Gdx.input.isKeyPressed(Input.Keys.S) && b2dBody.getLinearVelocity().y >= -0.8) {
    b2dBody.applyLinearImpulse(new Vector2(0, -PLAYER_SPEED), b2dBody.getWorldCenter(), true);
} else if (Gdx.input.isKeyPressed(Input.Keys.W) && b2dBody.getLinearVelocity().y <= 0.8) {
    b2dBody.applyLinearImpulse(new Vector2(0f, PLAYER_SPEED), b2dBody.getWorldCenter(), true);
} else if (Gdx.input.isKeyPressed(Input.Keys.A) && b2dBody.getLinearVelocity().x >= -0.8) {
    b2dBody.applyLinearImpulse(new Vector2(-PLAYER_SPEED, 0), b2dBody.getWorldCenter(), true);
} else if (Gdx.input.isKeyPressed(Input.Keys.D) && b2dBody.getLinearVelocity().x <= 0.8) {
    b2dBody.applyLinearImpulse(new Vector2(PLAYER_SPEED, 0), b2dBody.getWorldCenter(), true);
}

但是因为重力设置为 0 world = new World(new Vector2(0, 0), true);,所以身体不会停止。我想知道是否有办法保持重力不变,同时让身体在一段时间后停止?提前致谢。

【问题讨论】:

  • 看看这是否有帮助:stackoverflow.com/questions/24417977/…
  • 我学到的一些术语:在游戏中,“运动学”身体意味着可以移动的东西,但不是物理对象。示例包括玩家头像和简单的移动平台。所以我认为你想使用运动体,而不是对物理体施加力。我不是 libGDX 方面的专家,所以请谨慎对待。

标签: java libgdx box2d


【解决方案1】:

当您创建 box2d 主体时,您可以设置用于创建主体的 BodyDef 对象的 linearDamping 值。
该值总是会在物体移动时减慢物体的速度(例如,在应用线性脉冲时)。

你可以这样使用它:

BodyDef bodyDef = new BodyDef();
//set other body values
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(42f, 42f);
bodyDef.angle = 0f;
//...
bodyDef.linearDamping = 10f; // this will make the body slow down when moving

//create the body
Body body = world.createBody(bodyDef);

// TODO move the body arround and it will automatically slow down

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2021-12-30
    相关资源
    最近更新 更多