使用 applyLinearImpulse box2d 提高 body 速度

Increase body speed with applyLinearImpulse box2d

我试图让 body 以更快的速度移动。起初它开始加速,但很快就达到恒定速度。我如何让它保持加速?

我的代码如下所示:

 world = new World(new Vector2(0, 0), true);

    if (Gdx.input.isKeyPressed(Input.Keys.D))
        body.applyLinearImpulse(400f, 0, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.A))
        body.applyLinearImpulse(-400f, 0, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.W))
        body.applyLinearImpulse(0, 400f, pos.x, pos.y, true);
    if (Gdx.input.isKeyPressed(Input.Keys.S))
        body.applyLinearImpulse(0, -400f, pos.x, pos.y, true);

为了使 Body 随着时间的推移加速,通常使用 applyForceapplyLinearImpulse 用于立即改变速度。
请记住,只要你想加速,就必须调用 applyForce,而脉冲通常只应用一次。
想想一辆起步的汽车:车轮的转动,加上摩擦力,给整辆车增加了一个力,使它加速。
如果汽车随后达到一定速度并撞到一个箱子,则会对箱子施加一次冲量,几乎立即提高速度。
因此,您可以尝试将 applyLinearImpulse 调用更改为 applyForce 并确保它在每个 update 周期被调用,只要您按下给定的键。

我建议你阅读 Box2D tutorials on iforce2d.net