Sign in to follow this  
Vectorg

Timing for Physics vs Rendering

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by Vectorg
Is 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this