Public Group

# Motion with glTranslatef()

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

## 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 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     }}

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013660
×