0
\$\begingroup\$

My game loop below doesn't sleep as log as it should. The delta value always ends up less than 1, when it's supposed to be 16.6666 or close to 16.6666

double targetTime = 1000.0 / 60.0;//1000 millisecunts / 60 fps
long startTime;
long endTime = System.currentTimeMillis();
long delta;

long timer = System.currentTimeMillis();
int frameCount = 0;
while(running){
    startTime = System.currentTimeMillis();
    delta = startTime - endTime;
    endTime = startTime;

    tick(delta);
    render();
    frameCount +=1;
    try{Thread.sleep(targetTime - delta);}catch(InterruptedException e){}

    if(System.currentTimeMillis() - timer >= 1000){
        System.out.println("Rendered frames: " + frameCount);
    }
}
\$\endgroup\$
1
\$\begingroup\$

There's several layers of issues in the code above.

1) The pattern you're using to just sleep the remainder of the frame has an issue: each sleep is not simply the remaining delta: you've already called "render" in the current loop, taking X milliseconds, and then you "sleep to the beignning of the next frame", which takes (16 - X) milliseconds, and then you render and "sleep to the beginning of the 3rd frame", which takes (16 - (16 - X)) milliseconds, etc. This means you are effectively rendering twice per frame. This also means that for a fast render, X alternates between <1 milliseconds and about 16 milliseconds.

2) To fix the time delta accounting bouncing around, you can make a small adjustment and avoid calculating the sleep relative to the previous frame's time: instead of sleep(targetTime - delta), which is a measure of "how close the current frame follows the previous frame", you could use sleep(startTime + targetTime - System.currentTimeMillis()) which is a measure of "how long until the desired next frame from the current frame."

3) There's still a few issues with running your game logic (tick) at a variable frame rate based on the rendering speed, including drift over time due to the imprecision of Thread.sleep, and unstable behavior due to variable time steps, things can feel "higher friction" or glitch through/blow up when frame rate is too high or too low. You'll probably want to switch to a fixed-timestep loop (there are many resources on the internet, one popular one is at https://gafferongames.com/post/fix_your_timestep/) to get more stable simulation behavior.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.