moving for a game

Started by
1 comment, last by vbuser1338 19 years, 1 month ago
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 [edit] 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.
Advertisement
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

Ade
Thanks the second one is what I was looking for I never thought of doing it that way.
Thanks again vbuser

This topic is closed to new replies.

Advertisement