# moving for a game

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

## Recommended Posts

I want to do timebased movement in my game. The porblem is when I want it to wait a second before it moves the next sqaure it will stop the whole program for a second. I want it to just move a player every second. So what happens is moves counts the time and moves the player but you don't see it happen because it is not being drawn. And then when it reaches the square the program will start up again. Here is my code
void TravelTo(int tx,int ty)
{

clock_t endwait;
while(Curtis.x != tx)
{
endwait = 0;
Curtis.x += 1;

endwait = clock_t(clock() + 1 * CLOCKS_PER_SEC);
while (clock() < endwait) {}
}
}


Curtis is just a structure to the player How can I get it so that it will redraw after I move a square. Thanks vbuser  I don't know if I explained it that well basically I want to do time-based movement without stoping the program while waiting to the second or how long between each move.

##### Share on other sites
I think I understand -you mean one of the following:
you want:
1. smooth time based movement
OR
2. your object to move just once a second without stopping the entire game

Solution to 1
===============
Get the time since the last frame was drawn call this deltaTime
Say the velocity is 10m/s then the new position of the object is:

newpositionvector= positionvector + deltaTime*velocity

Solution to 2
===============
For the draw function of the object simply get the time since the last time it was moved - or even since the last frame. Add the times up - if greater than or equal to 1 then move, eg

DrawFunction()
{
get time since last frame (deltaTime)
timeSinceLastDraw += deltaTime;

if (timeSinceLastDraw >= 1.0s)
{
DrawTheObject
timeSinceLastDraw = 0.0;
}
}

cheers

##### Share on other sites
Thanks the second one is what I was looking for I never thought of doing it that way.
Thanks again vbuser

• ### What is your GameDev Story?

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

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015323
×