LIBGDX加快了整个游戏的速度(使用box2d) [英] LIBGDX speeding up a whole game (using box2d)

查看:156
本文介绍了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;
    }
}

这可以确保您的步进时间是恒定的,但是它与渲染循环同步.您可以使用它并像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).

这篇关于LIBGDX加快了整个游戏的速度(使用box2d)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆