Basic time scaled movement

Started by
5 comments, last by reaperrar 12 years, 10 months ago
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?
Advertisement
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.
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
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"]91372d6d7dbb07b5d2a9220bf3ebf443.png[/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.
I wish I took physics now ><

See part 2 to fix the rest.[/quote]
Equation 2 on the wikipedia page you linked?

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?

Equation 2 on the wikipedia page you linked?
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.
Tyvm for your help

This topic is closed to new replies.

Advertisement