Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Box2D以相同的速率移动实体,而不考虑FPS_Java_Libgdx_Box2d - Fatal编程技术网

Java Box2D以相同的速率移动实体,而不考虑FPS

Java Box2D以相同的速率移动实体,而不考虑FPS,java,libgdx,box2d,Java,Libgdx,Box2d,对不起,我不能正确地说出我的标题,但我会在这里更清楚地解释我的问题 我正在使用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(

对不起,我不能正确地说出我的标题,但我会在这里更清楚地解释我的问题

我正在使用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方法):

这会在播放器机身上施加一个力,使其加速度增加。我如何使海岸,就像我的第一个例子一样,身体移动的速度保持不变,以30fps、10fps、60fps等。我知道世界的时间步长参数。步长是模拟的时间量,但该值不应变化


提前谢谢。

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

编辑: 正如@Tenfour04所提到的,为了防止高增量值(导致巨大的跳跃),我们通常为增量设置一个上限

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

我不会使用可变时间步长-这是我使用的方法:

private float time = 0;
private final float timestep = 1 / 60f;

public void updateMethod(float delta) {
    for(time += delta; time >= timestep; time -= timestep)
        world.step(timestep, 6, 2);
}
基本上是一个可变的ish时间步,但统一更新

如果您以非常低的FPS运行游戏,或者如果您使用应用程序配置(例如,用于测试)强制它运行游戏,这将保持与正常60 FPS实例大致相同的更新速度


看看

Oh:)我认为timeStep参数必须始终具有相同的值,并且永远不会更改?一些游戏开发人员使用固定的增量(例如1/60)实现步骤逻辑。因此,他们确保每一帧都会对游戏产生相同的影响。这对多人游戏的实现是有好处的。不过,这对在较慢的机器上运行游戏的人来说不是一个很大的缺点吗?实际上,在较慢的机器上,如果你使用实际的delta,可能会有一些“跳跃”。例如,如果delta突然变为“2”,移动的物体、碰撞检测等将遇到麻烦。使用安全网来防止导致物理问题的巨大时间跳跃。这将导致极慢设备上的mo变慢,但比另一种方法要好:
world.step(Math.min(Gdx.graphics.getDeltaTime(),0.15f),6,2我认为你的代码总是像世界一样工作;因为您不更改时间步长variable@cokceken但关键是它被称为每秒的可变次数,这允许它在基本上与说world.step(delta,6,2)相同的情况下捕捉。当delta变大时,用户可能会体验到大跳跃,但是这确实比博客文章中描述的简单可变时间步有优势。我linkedI更希望不看到大跳跃,所以我这样回答,你关于物理模拟的工作是对的,但我认为你不应该优化游戏约2fps。以2-10 fps的速度,玩家无论如何都不会喜欢游戏,所以你可以防止意外情况,比如跳跃,让游戏变慢。
world.step(Math.min(Gdx.graphics.getDeltaTime(), 0.15f), 6, 2);
private float time = 0;
private final float timestep = 1 / 60f;

public void updateMethod(float delta) {
    for(time += delta; time >= timestep; time -= timestep)
        world.step(timestep, 6, 2);
}