Advertisement

velocity and frame rate help

Started by July 14, 2000 10:14 PM
2 comments, last by youngo 24 years, 5 months ago
how would i make an object with a decreasing velocity move the same distance when the frame rate is not constant? this is something like what i do and it''s not working for me: QueryPerformanceCounter(&bb); velocity.x -= .1f*(bb.QuadPart - aa.QuadPart); if ( b.v.x <= 0) velocity.x = 0; QueryPerformanceCounter(&aa); object.x += velocity.x;
As much as you won''t believe it, this is an application of simple calculus. You take the formula for movement with acceleration (negative acceleration in your case), and integrate it with respect to t (that is, time). You can just plug time into the resulting formula to determine how far you''ve moved.
Advertisement
youngo, I dunno how QueryPerformanceCounter works, because I use timeGetTime(). But basically the formula you want is

velocity += acceleration * deltaTime;

position += velocity * deltaTime;

I get my deltaTime, in seconds, like this:

// during initialization:
baseTime = timeGetTime();

// each frame
FLOAT oldElapsedTime = elapsedTime;
elapsedTime = ( timeGetTime() - baseTime ) * 0.001f;
deltaTime = elapsedTime - oldElapsedTime;
The formula really should be:

temp = acceleration * deltaTime * 0.5f;
velocity += temp;
position += velocity * deltaTime;
velocity += temp;

The formula Eric gave creates some problems.
You really have to first add half of the acceleration to velocity, then add velocity to position and then add the other half of acceleration to velocity.


-Hans

This topic is closed to new replies.

Advertisement