1
\$\begingroup\$

I was just wondering, when simulating physics is it usually a good idea to get the time delta from the last frame and apply the relevant motion equations?

Something like:

void simulate(float dt)
{
    vel += (force / m) * dt;
}

In this case wouldn't there be an accumulated error over time due to precision? For example when trying to simulate a circular motion?

Or would it be better to get the entire elapsed time from the beginning, and then apply the motion:

void simulate(float elapsedTime)
{
    vel = vel0 + (force/m)*elapsedTime;
}

And since this doesn't depend on the previous simulate() steps, it's less likely to cause precision errors?

Thanks!

\$\endgroup\$
2
\$\begingroup\$

Using delta time will indeed introduce rounding/precision errors over time, but will allow you to change the force or mass instantly and go from there.

Using the second approach will not introduce accumulated precision errors, but won't allow you to change force or mass unless you calculate a new vel0 first and subtract a time offset from elapsedTime, in which case it quickly turns into your first approach.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.