How to make this game loop deterministic

Posted by Lanaru on Game Development See other posts from Game Development or by Lanaru
Published on 2012-11-04T17:32:27Z Indexed on 2012/11/04 23:19 UTC
Read the original article Hit count: 348

Filed under:
|
|

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?

© Game Development or respective owner

Related posts about java

Related posts about game-loop