你好,我在LibGDX的帮助下开发了一款游戏,并在其中使用了Box2d。问题是,当我在 hdpi 或平板电脑上运行我的游戏时,它运行良好,但在ldpi和mdpi的情况下,box2d 主体没有相应地运行。
我认为,在这些手机上渲染需要更多时间。那么,如何针对 ldpi 和 mdpi 手机优化我的游戏。我在 world.step 中传递的值是worldbox.step(Gdx.graphics.getDeltaTime(), 10, 2000);
谢谢。
使用帧速率作为时间步长是个坏主意。Box2D 手册说:
可变的时间步长会产生可变的结果,这使得调试变得困难。所以不要将时间步与你的帧速率联系起来(除非你真的,真的必须)。
此外,您对速度和位置迭代使用了太大的值。Box2D 手册说:
Box2D 的建议迭代次数是速度为 8 次,位置为 3 次。
尝试固定时间步长和推荐的迭代次数,如下所示:
float mAccomulated = 0;
float mTimeStep = 1.0f / 60.0f;
int mVelocityIterations = 8;
int mPositionIterations = 3;
void updatePhysicWorld()
{
float elapsed = Gdx.graphics.getDeltaTime();
// take into account remainder from previous step
elapsed += mAccomulated;
// prevent growing up of 'elapsed' on slow system
if (elapsed > 0.1) elapsed = 0.1;
float acc = 0;
// use all awailable time
for (acc = mTimeStep; acc < elapsed; acc += mTimeStep)
{
mWorld->Step(mTimeStep, mVelocityIterations, mPositionIterations);
mWorld->ClearForces();
}
// remember not used time
mAccomulated = elapsed - (acc - mTimeStep);
}