# Basic time scaled movement

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

## Recommended Posts

So lately for time scaled movement I've just been using this:
Position += Velocity * DeltaTime;

Which worked fine until I added acceleration...

Acceleration = (force * frictionforce) / mass
_____________________________________
Velocity += acceleration * DeltaTime;

Position += Velocity.

Problem is (if you haven't already spotted which you guys probably have) the velocity is no longer scaled.
Faster machines = faster objects ><

Here's an example of the math I did, comparing a machine that runs at one frame per second and a machine that runs at 5 frames per second.
Acc = 50 / second
Initial velocity = 0 / second

Machine 1
Time: - - - - 1.0
velocity: 0 0 0 0 50
Position: 0 0 0 0 50

machine 2
Time: 0.2 0.2 0.2 0.2 0.2
velocity: 10 20 30 40 50
Position: 10 30 60 100 150

So machine 2 (in this case) moves three times further than machine 1 in the same time span.

How can I fix this?

##### Share on other sites
Part 1)
Scale velocity as well!! (why did you stop scaling it when adding acceleration?)Velocity += acceleration * DeltaTime; Position += Velocity * DeltaTime;Part 2)
The above will have fixed most of your issues, but the simulation will still be slightly different on two computers if the frame-rate changes. You can solve this by using a more robust numerical integration technique (the one you're currently using is called Euler's method).
Or, what most people do is to simply use a fixed time-step, which makes both computers produce the same result despite the use of an approximate integrator.

##### Share on other sites
If I scale both, won't my position end up like this:

machine 2
Time: 0.2 0.2 0.2 0.2 0.2
velocity: 10 20 30 40 50
ScaledVel: 2 4 6 8 10
Position: 2 6 12 20 30

##### Share on other sites
30 is closer to 50 than 150 is.

Remeber that the formula you're trying to implement here does scale velocity by time, and scales acceleration by time twice:
[font="Courier New"][/font]

See part 2 to fix the rest.
When using a numerical integrator, the size of your steps affects the accuracy of the result (these techniques only give estimated results!). So, because both computers have used different step sizes, they've come out with different estimations of the answer.

##### Share on other sites
I wish I took physics now ><

See part 2 to fix the rest.[/quote]

Also looking at that page, what's the difference between "s0" and "si"? They both appear to be initial position.

Lastly...
these techniques only give estimated results![/quote]
What's the most accurate technique for movement/acceleration scaled with time?

##### Share on other sites

No, in my earlier post, there was a "Part 2)" section where I said that using an approximate integrator isn't a problem as long as you used a fixed time-step.

e.g. Instead of something like:deltaTime = ....//time since last frame Update( deltaTime );You want something like:deltaTime = .... //time since last frame deltaTime += m_leftOverTimeFromLastFrame; const double timeStepSize = 1/60.0f;//60 updates per second while( deltaTime > timeStepSize ) { Update( timeStepSize ); //N.B. update is always called with the same (fixed) time step value deltaTime -= timeStepSize; } m_leftOverTimeFromLastFrame = deltaTime;//there might be half a step left over, save it for next frame.
Also looking at that page, what's the difference between "s0" and "si"? They both appear to be initial position.[/quote]Yeah they're the same :/
What's the most accurate technique for movement/acceleration scaled with time?[/quote]Your current method (Euler's method) is fine, assuming that you're using a fixed-time step as above -- the smaller the time-step, the more accurate it will be (but also will take more CPU time).
However, if you do want it to be more accurate for whatever reason, you can try RK4.