後改善,但我似乎無法消除偶爾的消耗。的Android遊戲固定時間步長小膠質
我GameTimer類包含被稱爲每一幀來推進時間的方法稱爲蜱()中,用canStep()和步驟()如下面的方法沿着:
public void tick() {
long thisTime = System.nanoTime();
frameTime = thisTime - lastTime;
frameTimeMs = (int)(frameTime/NANO_TO_MILLI);
frameCounter++;
secondCounter -= frameTime;
if(secondCounter <= 0) {
fps = frameCounter;
frameCounter = 0;
secondCounter += NANO_TO_SECOND;
}
lastTime = thisTime;
accumulator += Math.min(frameTimeMs, deltaTime * 5);//frameskip >5 frames
alpha = accumulator/deltaTime;
}
public boolean canStep() {
return accumulator >= deltaTime;
}
public void step() {
accumulator -= deltaTime;
alpha = accumulator/deltaTime;
}
NB:的DeltaTime保持1000F除以每秒更新的期望數量(例如30)。
我的主遊戲循環運行每幀這樣的邏輯如下:
gameTimer.tick();
while(gameTimer.canStep()) {
update();
gameTimer.step();
}
draw();
爲了計算一個遊戲對象的移動予執行以下操作:
float time = gameTimer.getDeltaTime()/1000f; //how much of a second per update
velocity = speed * direction * time; //simplified: velocity, speed & direction are vec3s
previousPosition = position;//simplified: values are copied
position += velocity;
甲移動的遊戲對象的繪製位置然後計算在抽籤時使用線性插值:
lerp(drawPosition, previousPosition, position, 1.0f + gameTimer.getAlpha());
遊戲在賭注在50到60 fps。嘗試每秒更新沒有產生更好的結果。此外,增加或減少frameskip也不會刪除gitter。物體突然向前跳躍幾個像素,我一直在努力解決這個問題。
任何人都可以看到的是我錯過上面的代碼中任何明顯的問題?
任何幫助,將不勝感激:)
問題在於時間源和圖形緩衝區排隊的方式。請參閱https://source.android.com/devices/graphics/architecture.html#loops,瞭解問題的解釋以及如何使用編排器來修復問題。 – fadden