View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Basic time scaled movement

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

#1reaperrar  Members

Posted 09 June 2011 - 06:20 PM

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?

#2Hodgman  Moderators

Posted 09 June 2011 - 06:28 PM

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.

#3reaperrar  Members

Posted 09 June 2011 - 06:35 PM

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

#4Hodgman  Moderators

Posted 10 June 2011 - 09:26 PM

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:

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.

#5reaperrar  Members

Posted 12 June 2011 - 10:15 PM

I wish I took physics now ><

See part 2 to fix the rest.

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!

What's the most accurate technique for movement/acceleration scaled with time?

#6Hodgman  Moderators

Posted 12 June 2011 - 10:25 PM

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.

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.

Yeah they're the same :/

What's the most accurate technique for movement/acceleration scaled with time?

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.

#7reaperrar  Members

Posted 13 June 2011 - 04:11 AM