Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
6 replies to this topic

#1 reaperrar   Members   -  Reputation: 134

Like
1Likes
Like

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?

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 30385

Like
1Likes
Like

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.

#3 reaperrar   Members   -  Reputation: 134

Like
0Likes
Like

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

#4 Hodgman   Moderators   -  Reputation: 30385

Like
0Likes
Like

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:
Posted Image


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.

#5 reaperrar   Members   -  Reputation: 134

Like
0Likes
Like

Posted 12 June 2011 - 10:15 PM

I wish I took physics now ><

See part 2 to fix the rest.

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!

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

#6 Hodgman   Moderators   -  Reputation: 30385

Like
1Likes
Like

Posted 12 June 2011 - 10:25 PM

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.

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.

#7 reaperrar   Members   -  Reputation: 134

Like
1Likes
Like

Posted 13 June 2011 - 04:11 AM

Tyvm for your help




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.



PARTNERS