There can be hard to keep the game in exact sync. I found this article with Glenn Fiedler that describe how you keep everything in sync even when graphic very in speed.
Everything is written for C++ but I use LibGDX where some step are built in, so I had to convert it. I did skip the last part he talk about because my game just don’t benefit from it. The code went very clean in the end.
1 2 3 4 5 6 7 8 9 10 11 | float timeToProgress = 0.01f; public void update(float delta) { if (delta > 0.25) delta = 0.25f; accumulator += delta; while (accumulator >= timeToProgress) { worldController.update(timeToProgress); accumulator -= timeToProgress; } worldRender.render(); } |
First if-statement just make sure I don’t run into a spiral of death, if calculation on the word take more then 25 milliseconds it just set 25. Then I add delta and loop so many times I need, so if there been 5 milliseconds from last time update was called it will loop 5 times and do the update to the world. Last thing is just render everything out.