Sign in to follow this  
vbuser1338

Time based movement

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this