Synchronizing time step.
Is my first post here. I'm having problems with time step.
I'm using a physics engine that I have to tell how much times it will update each second, but my program is running at inconstant rate.
Have some way to force the main loop to spend a specific amount of time each pass?
Yes, I have a way of forcing any piece of code to execute in any arbitrary length of time. I used this to predict the final outcome of the universe in less than two microseconds.
Originally Posted by Auradrummer
More seriously, you need to occassionally call a time-getting function to see if you're getting too far "ahead", sleeping if necessary. For instance, if each loop iteration takes dt seconds, then:
Where CurrentTime() gives the current time in seconds (double precision), slop is the maximum time you are allowed to overshoot (possibly 0.0), and WaitUntil() is a function which causes the program to sleep until the time is greater than or equal to t.
double t = CurrentTime();
if( t > CurrentTime() + slop )
WaitUntil( t );
t += dt;
Good physics engines have ways to deal with this common problem.
Typically, you either have an interpolation (if your a bit slow), or the engine 'cycles twice' (or more) for it's fixed timestep to catch up.
Bullet, for example, offers a fairly standardized approach - but each engine can have different service for this, and you'll need to check documentation / example programs.