gpt4 book ai didi

java - Box2D 以相同的速率移动物体而不管 FPS

转载 作者:行者123 更新时间:2023-11-30 10:34:49 25 4
gpt4 key购买 nike

抱歉,我无法正确表达我的标题,但我会在这里更清楚地解释我的问题。

我正在使用 libgdx。

当我想移动纹理使其覆盖与所有 FPS 相同的距离时,我会这样做:

//...define Player class with x property up here.

Player player = new Player();
int SPEED = 100
public void render() {
player.x += SPEED * Gdx.graphics.getDeltaTime();
}

现在我想知道如何对 box2d 中的物体产生同样的影响。这是一个示例(扩展 ApplicationAdapter 的类的 render 方法):

public void render() {

//clear screen, ... do other stuff up here.

playerBody.applyForce(new Vector2(0.5f / PIXEL_PER_METER, 0.0f), playerBody.getWorldCenter(), true);
//PIXEL_PER_METER -> applied to scale everything down

//update all bodies
world.step(1/60f, 6, 2);
}

这会在 playerBody 上施加一个力,使其加速度增加。我如何上岸,就像我的第一个例子一样, body 移动的速度保持恒定在 30fps、10fps、60fps 等。我知道 world.step 的 timeStep 参数是模拟的时间量但是这个值不应该改变。

提前谢谢你。

最佳答案

您可以使用增量更新所有主体(不是 1/60 固定增量)

world.step(Gdx.graphics.getDeltaTime(), 6, 2);

编辑:正如@Tenfour04 提到的,为了防止高 delta 值(导致巨大的跳跃),我们主要为 delta 设置一个上限。

world.step(Math.min(Gdx.graphics.getDeltaTime(), 0.15f), 6, 2);

关于java - Box2D 以相同的速率移动物体而不管 FPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com