LIBGDX加速整个游戏(使用box2d)



我想知道如何加快使用 libgdx 完成的整个游戏(例如在单击按钮后)。我在游戏中的方法是修改

world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);

但我现在确定这是否是个好主意。如果有更好的方法来存档它?

使用 Box2D 时,您可以通过修改物理步骤来加快游戏速度。一个问题是您应该使用恒定的步进时间。我在游戏中使用以下代码:

private float accumulator = 0;
private void doPhysicsStep(float deltaTime) {
    // fixed time step
    // max frame time to avoid spiral of death (on slow devices)
    float frameTime = Math.min(deltaTime, 0.25f);
    accumulator += frameTime;
    while (accumulator >= Constants.TIME_STEP) {
        WorldManager.world.step(Constants.TIME_STEP, Constants.VELOCITY_ITERATIONS, Constants.POSITION_ITERATIONS);
        accumulator -= Constants.TIME_STEP;
    }
}

这可确保步进时间是恒定的,但它与渲染循环同步。您可以使用它并像doPhysicsStep(deltaTime * speedup)一样调用它(默认情况下加速为 1,按下按钮后可能是 1.5)。这可能会导致结果不佳,但您可以尝试一下。

否则,你可以像注释中建议的那样走艰难的道路,并通过修改代码中必要的每个地方来投入更多时间(所有力都需要修改,在许多情况下并不像force * speedup那么微不足道,因为在现实/物理世界中,并非所有东西都表现为线性)。

最新更新