# Timing for Physics vs Rendering

## Recommended Posts

Vectorg    104
I want to maintain consistent physics as frame rate changes. In my current game attempt, movement of objects is scaled by time between frames, but the physics still change as frame rate changes. How do I base physics on a constant rate, while frame rate is changing? Thanks.

##### Share on other sites
iequalshane    160
You can force you physics into a minimal time step by looping as needed based on the elapsed time since the last physics calculation. ie, if if .1 seconds passed and you want your steps to be no larger than .01 seconds you would loop 10 times with a .01 second step. Any remainder can be used as the final step because it will be under the minimal time step requirements. This is not the best way but it's a good hack.

##### Share on other sites
ogracian    180
This is a great article about how to sync your frame time.

http://www.gaffer.org/articles/Timestep.html

Hope that helps,
Oscar

##### Share on other sites
Vectorg    104
Thanks iequalshane and others.

It's been a while, but this morning I implemented the physics timing into my game according to iequalshane's comments. It works perfectly, and my code turned out to be very much like the article from the other posts.

Is this method really considered a hack? If so, then what would be the "sophisticated" way to go about it?

Thanks all.

##### Share on other sites
JohnBolton    1372
Quote:
 Original post by VectorgIs this method really considered a hack? If so, then what would be the "sophisticated" way to go about it?

I guess the part where the leftover time is not accumulated is what he is calling a hack.

##### Share on other sites
Extrarius    1412
The problem is likely that you're using a euler integrator, which isn't very accurate. The common solution for the simple physics used in games is to use something like a runge-kutta integrator. RK4 would probably fit your needs.
Runge Kutta on mathworld
Runge Kutta on wikipedia

If you want the physics to be entirely framerate independant (usually not required for games), you'll need to use a fixed physics timestep. With RK4, though, you'll be able to use a larger fixed timestep which means you can run the physics simulation less often.

Also, instead of doing as previously suggested and running the physics until your timestep is 0, you should do something like
while(DeltaTime >= MIN_TIMESTEP){   DoPhysics(MIN_TIMESTEP);   DeltaTime -= MIN_TIMESTEP;}/*...*/DeltaTime += ElapsedTime();
because this way you can keep your physics system always using the same delta time and thus the results should always be exactly the same, which isn't the case if you run it with different values once in a while.

##### Share on other sites
Vectorg    104
Yes, Extrarius, my code is functionally identical to your sample. My physics now remain constant for any reasonable frame rate.

Euler integrators and runge-kutta? These topics are beyond my current game application right now, but thanks for giving me even more stuff to study... and I have finals coming up.

Thanks.