Public Group

# Time based movement

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

## 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
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 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 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 on other sites
Always happy to help :)

cheers,
marcel

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634133
• Total Posts
3015747
×