LIBGDX speeding up a whole game (using box2d)

巧了我就是萌 提交于 2019-12-01 12:36:11

When using Box2D you can speed up your game by modifying the physics step. One problem is that you should use a constant steptime. I use the following code below in my games:

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;
    }
}

This makes sure that your steptime is constant, but it's synchronized with the render loop. You could use that and call it like doPhysicsStep(deltaTime * speedup) (speedup is 1 by default, and maybe 1.5 after a button was pressed). This might probably result in not optimal results, but you could give it a try.

Otherwise you can go the hard way like it was suggested in the comments and invest more time by modifiying every place in your code where it is necessary (all forces need to be modified, which in many cases isn't as trivial as force * speedup, because in the real/physics world, not everything acts linear).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!