What's wrong with this game loop?
Code:
while(true)
{
double elapsed = (int) (System.currentTimeMillis() - lastUpdateTime)/1000f;
w.fps = 1/elapsed;
lastUpdateTime = System.currentTimeMillis();
w.step();
processInput();
processTasks();
gameView.getGamePanel().repaint();
if(System.currentTimeMillis() - lastStatsUpdateTime > 500)
{
lastStatsUpdateTime = System.currentTimeMillis();
w.updateStats();
}
try {
nextStepTime += frameTimeMs;
int sleepTime = (int) (nextStepTime - System.currentTimeMillis());
if(sleepTime > 0)
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
This is supposed to limit the FPS to 60. However, from frame to frame the calculated FPS varies wildly between 30, 60, and 0... but only those 3 numbers, or something very close. I've tried removing everything except the drawing code and it still does the same thing.
Does anyone see the problem? This has been bothering me for months, and I think it's the cause of some slight physics instability.