velocity and frame rate help
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;
July 15, 2000 05:18 PM
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.
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;
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
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement