I searched in the past to find a solution to this simple problem: How to make my world step at a same rate independently of the FPS. I tried different approachs like
using dynamic frequency values (calculated using FPS, elapsed time, etc..), exploiting vsync and I finally found the most flexible way of doing it, I wanted to share my code 🙂
This is java code (I use JBox2D now, but this code can be converted to C++)
Note: lastTimeUpdate and timeNow contains milliseconds
This code is run on every update interval (in a idle loop for example). In the case of a very low FPS rate (like 10fps), the word.step multiple times in the same update. The result
is a smooth simulation.
using dynamic frequency values (calculated using FPS, elapsed time, etc..), exploiting vsync and I finally found the most flexible way of doing it, I wanted to share my code 🙂
This is java code (I use JBox2D now, but this code can be converted to C++)
Code: Select all
long elapsed = (timeNow - lastTimeUpdate) / (1000 / 250);
float timeStep = 1.0f / 60;
if (elapsed > 0) {
for (int i=0;i<elapsed;i++)
world.step(timeStep, 10);
lastTimeUpdate = timeNow;
}
Note: lastTimeUpdate and timeNow contains milliseconds
This code is run on every update interval (in a idle loop for example). In the case of a very low FPS rate (like 10fps), the word.step multiple times in the same update. The result
is a smooth simulation.