Jump to content
  • Advertisement
Sign in to follow this  
Paultergeist

Motion with glTranslatef()

This topic is 3854 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

Hi! I have a little problem... I need to create a smooth motion of a square from a point to another. How can i use glTranslate to do it? I can only create another square at the final point, and i want to create the ilussion that the square is going from one point to another. Can anyone help me? Thanks

Share this post


Link to post
Share on other sites
Advertisement
The key to this is that you need to break up the square's movement into smaller pieces, rather than just moving it the entire distance at once. To break it up in such a way that the movement appears smooth regardless of framerate, you need to know two things: how much time has elapsed since movement started, and how quickly you want the square to get to its destination.



bool MoveBox (
float timeElapsed; //the time elapsed since the box started moving
float boxMoveTime; //the amount of time it will take the box to reach its destination
vec3 boxStart; //the box's starting location
vec3 boxDest; //the box's destination
)
{
//if we're past the amount of time given for movement, set the box
//at the desired location. otherwise, move the box to an intermediate location
if (timeElapsed >= boxMoveTime)
{
glLoadIdentity();
glTranslatef(boxDest.x, boxDest.y, boxDest.z);
return (true); //return true to indicate the box is done moving
}
else
{
//calculate the vector pointing in the direction of movement
vec3 boxDirection = boxDest - boxStart;

//figure out where the box should be on the path
vec3 boxLocation = boxStart + (boxDirection * (timeElapsed/boxMoveTime));

glLoadIdentity();
glTranslatef(boxLocation.x, boxLocation.y, boxLocation.z);
return (false); //return false to indicate the box still has more movement
}
}



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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!