LIBGDX 加速整个游戏(使用 box2d)
我想知道如何加快使用 libgdx 完成的整个游戏(例如在单击按钮后).我在游戏中的方式是修改
I was wondering how i can speed up whole game done with libgdx (for example after clicking a button). The way i have in my game is modify a timestep variable used in
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
但我现在确定这是否是个好主意.有没有更好的存档方式?
but im now sure if it's a good idea. If there a any better way to archive that?
推荐答案
使用 Box2D 时,您可以通过修改物理步骤来加快游戏速度.一个问题是您应该使用恒定的步进时间.我在我的游戏中使用以下代码:
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;
}
}
这可确保您的 steptime 是恒定的,但它与渲染循环同步.您可以使用它并将其称为 doPhysicsStep(deltaTime * speedup)
(默认情况下加速为 1,按下按钮后可能为 1.5).这可能会导致效果不佳,但您可以尝试一下.
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.
否则,您可以像评论中建议的那样采取艰难的方式,并通过修改代码中必要的每个地方来投入更多时间(所有力量都需要修改,在许多情况下并不像force * speedup
,因为在现实/物理世界中,并非一切都是线性的).
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).
相关文章