Jump to content
  • Advertisement
Sign in to follow this  
vbuser1338

Time based movement

This topic is 4813 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

I am having trouble getting my timebased movement working. I want to move something like 100 pixels a second but the faster the computer updates the slower I get because the movement is less that 1 pixel. Well this is what I get. If I add a few extra calls to rendering it works better but when I don't it speeds up and it begins going slower.
LARGE_INTEGER currentTime;  // Current time

QueryPerformanceCounter(&currentTime);  // Find current time
if(first)
{
   first = false;
   lastUpdated = currentTime;  // Update the lastUpdated
}
// Time elapsed
float timedif = float(currentTime.QuadPart - lastUpdated.QuadPart) / float(frequency.QuadPart);
int interval = speed * timedif; // How much to move
lastUpdated = currentTime;  // Update the lastUpdated

The frequency is allready calculated But I get really chop movement. Does anyone know how I can fix this. Thanks, vbuser

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if I have understood your problem quite well, but I think that you you've got a problem because when the movement is less than 1px the integer is 0 so there is no movement ... the solution for this would be to define a static float var to which you add each frame the movement ... when the movement is larger or equal to 1 you set your movement integer to the float value and set float value to float value - 1 so the stuff beyong 0 is kept and continue doing this as described.

pseudo example:

//somewhere else defined --> timedif

int iSpeed;
int iInterval;
static float fTempSpeed = 0;

iSpeed = 100; //or whatever


if(fTempSpeed >= 1)
{
iInterval = (int)fTempSpeed;
fTempSpeed =- 1;
}
else
{
fTempSpeed =+ (iSpeed * timedif);
}


I actually hope this is what you were searching for and I hope if it was what you were searching for that I didn't implement any mistakes when describing how it works.

If you don't get it running just reply and I'll check it out in my source where it works when I've some more time.

cheers,
marcel

Share this post


Link to post
Share on other sites
Yes that was exactly what I was looking for. Last time I had this problem people said it was because I was not using a high enough resolution timer. But it works great now. Thanks for the help.
Vbuser

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!