# best time step?

This topic is 4899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Aside from experimentation, is there a mathematical way to determine the best fixed time step for a physics spring simulation?

##### Share on other sites
the best time step is infinitely small, so 1/infinity. don't try to implement that though, you'll get roundoff error ;)

on a more serious note, a better idea might be to average out the time it's taken to do your physics loop over the last 3-15 trials (depending on how fast you wish your timestep's response to be) and use that as your dt. of course, however, that falls into the category of experimentation... that and you also have to set some default values so that the program doesn't go balistic before the average is up and running

##### Share on other sites
My model could be low poly or high poly, and that would change how long the simulation runs, but
not the physics involved, so I don't see the reason why I would base the timestep value on the length
of the simulation?

##### Share on other sites
Suppose the force in the spring is described by

F= -k x

where k is the spring constant and x is the displacement from equilibrium. Then we can write

F = m a = m x''

where m is the mass of the oscillating thing, a is acceleration, and x'' is the 2nd derivative of x with respect to time. This gives you the following differential equation

m x'' = -k x

or,

x'' = -(k / m) x.

Now, the units of k are Mass/Time^2 so the units of (k / m) are 1/Time^2. From this you can get a timscale for the spring, i.e.

t* = sqrt(m / k)

is the characteristic timescale of the spring.

RULE OF THUMB: Use a timestep that is 1/10th the characteristic timescale.

However, be warned that if you have to integrate for a long time you will need to be careful that the integration method you use is appropriate. Alternatively, the exact solution to this equation is

x(t) = x(0) cos(w t) + (x'(0)/w) sin(w t)

where w = sqrt(k / m), x(0) is the initial displacement, and x'(0) is the initial velocity.

-Josh

##### Share on other sites
Quote:
 Original post by JakeMMy model could be low poly or high poly, and that would change how long the simulation runs, butnot the physics involved, so I don't see the reason why I would base the timestep value on the length of the simulation?

When you integrate numerically you are approximating the solution and that results in an error at each timestep. The more timesteps you take, the greater the error you accumulate, i.e. roundoff error as Uthman said.

-Josh

##### Share on other sites
Thanks, I will try that.

##### Share on other sites
There is no mathematically best time-step. It is a trade-off:
1. A shorter time-step will result in a lower frame rate -- more time spent on physics, less time spent on graphics.
2. A shorter time-step means less error due to approximation, but more error due to lack of floating-point precision.
Realistically, the error due to lack of floating-point precision is usually much less than the error due to approximation, so the real trade-off is a more accurate simulation vs. a better frame rate.

The absolute maximum time-step would be the average duration of a frame. What is the use of redrawing a frame if the simulation is the same as the last frame? However, a time-step of that duration might look jittery (especially with low frame rates), so half the average frame duration would be better.

##### Share on other sites
Though as others have said there is not necessarily a "best time step", in some cases there is a calculatable point at which the time step starts to go bad. Many numerical methods, e.g., Euler, Verlet, Runge-Kutta and others, are "conditionally stable." They require the time step to be within a given range (high or low), or otherwise the simulation will eventually overflow. The calculation is both problem dependent (heavy vs. light objects, big vs. small objects, etc.) and method-dependent (Euler vs. Verlet). The name given to the condition that defines the upper bound on a stable time step is the "Courant-Friedrichs-Levy" or "CFL" condition. It is a necessary, but not sufficient condition for simulation stability.

##### Share on other sites
The maximum timestep can easily be longer than the average duration of a frame. You just need to be able to interpolate or extrapolate the motions of things when drawing. Interpolating or extrapolating is probably a lot cheaper than running a full physics step, so this can allow higher framerates without requiring more work from the physics engine.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 12
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015649
×