How to make this game loop deterministic
- by Lanaru
I am using the following game loop for my pacman clone:
long prevTime = System.currentTimeMillis();
while (running) {
long curTime = System.currentTimeMillis();
float frameTime = (curTime - prevTime) / 1000f;
prevTime = curTime;
while (frameTime > 0.0f) {
final float deltaTime = Math.min(frameTime, TIME_STEP);
update(deltaTime);
frameTime -= deltaTime;
}
repaint();
}
The thing is, I don't always get the same ghost movement every time I run the game (their logic is deterministic), so it must be the game loop. I imagine it's due to the final float deltaTime = Math.min(frameTime, TIME_STEP); line. What's the best way of modifying this to perform the exact same way every time I run it? Also, any further improvements I can make?