Jump to content
  • Advertisement
Sign in to follow this  
vbuser1338

moving for a game

This topic is 4965 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 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.

Share this post


Link to post
Share on other sites
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

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!