Archived

This topic is now archived and is closed to further replies.

velocity and frame rate help

This topic is 6360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites