Sign in to follow this  

Motion with glTranslatef()

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this