我有一个(简单的)游戏引擎;在游戏循环中,我有一个"deltaTime“变量。(从上一帧开始的时间,以秒为单位),deltaTime变量与id完全一致。
然而,即使deltaTime很好,我也不能准确地转换实体的位置。它在FPS非常低的情况下工作得很好,但当它变得更高(15<)时,它就会崩溃,不能工作。这个物体走得非常非常慢。FPS越快,速度就越慢。我甚至试着将FPS限制在120。
我在这里找到了疑似有问题的代码区域:
curEntity.pos.y+= curEntity.velocity.y * deltaTime;pos.y、velocity.y和deltaTime都是浮点数。
这是怎么回事?我该如何解决这个问题呢?
如果你感兴趣,下面是获取deltaTime的代码:
long now = System.nanoTime();
deltaTime= (now - lastTime) / 1000000000f;
lastTime=now;//值得注意的是,我正在尝试做Unity做的事情;此外,我还尝试将pos.y、velocity.y和deltaTime改为替身
发布于 2015-01-26 14:39:36
在摆弄了一段时间后,我找到了答案:我有自上次循环通过以来的增量时间,而不是上次物理更新的增量时间()。这里(如果有人关心的话)是答案:
while(running){
long now = System.nanoTime();
//HERE IS WHERE THE DELTA TIME CODE WAS LAST
tickDelta += (now - lastTime) / nsPerTick;
frameDelta += (now - lastTime) / nsPerFrame;
lastTime=now;
if(tickDelta>=1){
//THIS IS WHERE IT SHOULD BE
//GETS THE TIME SINCE LAST TICK
deltaTime= (now - deltaTimeTimer) / 1000000000f;
deltaTimeTimer=now;
ticks++; Tick(); tickDelta=0;
}
if(frameDelta>=1){frames++; Render(); frameDelta=0;}
if(System.currentTimeMillis() - Timer >= 1){Timer+=1000; System.out.println("FPS: "+frames+"; TPS: "+ticks +"; deltaTime: "+deltaTime); ticks=0;frames=0;}
try{Thread.sleep(5);} catch(Exception err){} //TODO: Remove
//System.out.println(deltaTime);
}
}https://stackoverflow.com/questions/28145379
复制相似问题