Jump to content
  • Advertisement
Sign in to follow this  
Paultergeist

Smooth motion to a square

This topic is 3948 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 need to translate a square from a place to another. The problem is the translate can´t be direct, it as to seem that the square is moving from a place to another. I know that glTranslate moves the square from a olace to another, but without any motion... How can i create the motion? Can anyone help me please? Thank You.

Share this post


Link to post
Share on other sites
Advertisement
Instead of suddenly changing the position from here to there, do many smaller translations over time until the goal position is reached. Each translation step has to be done obviously in direction to the goal position. E.g. if you start at position s and the goal position g has to be reached in T seconds by a linear motion, then at a time t the current position will be
p(t) := s + t / T * ( g - s ) , 0 <= t <= T


[Edited by - haegarr on December 5, 2007 1:00:33 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by haegarr
Instead of suddenly changing the position from here to there, do many smaller translations over time until the goal position is reached. Each translation step has to be done obviously in direction to the goal position. E.g. if you start at position s and the goal position g has to be reached in T seconds by a linear motion, then at a time t the current position will be
p(t) := s + t / T * ( g - s ) , 0<=t<=T


Yes, i know that's what i have to do, but i don´t know how...
First, i used the glTranslatef and it make the motion, but it was drawing square on top of a square... and i want to draw a new one, but delete the old one.
I´m using the glTranslatef inside a FOR cycle.

I believe i have to use the glPushMatrix and glPopMatrix, but i can´t understand how...

Share this post


Link to post
Share on other sites
You need a "render loop", e.g. in some C like pseudo-code:

timestamp atStart = nowInSeconds();
int t;
do {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
t = nowInSeconds() - atStart;
vector diff = goal - start;
diff *= t / T;
glTranslatefv(diff);
swapBuffers(); // system dependent; see WGL, GLX, CGL, or whatever you use
} while(t < T);
assuming that the square is already located at "start" when beginning.

EDIT: Above "start" and "goal" are of course vectors, as can be assumed by the type "vector" of diff. If you don't have a vector class at hand (or use a non OO language) you have to substitute it with something equivalent, like

struct {
float x,y,z;
} vector;
timestamp atStart = nowInSeconds();
int t;
do {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
t = nowInSeconds() - atStart;
vector diff;
diff.x = ( goal.x - start.x ) * t/T;
diff.y = ( goal.y - start.y ) * t/T;
diff.z = ( goal.z - start.z ) * t/T;
glTranslatefv(diff);
swapBuffers(); // system dependent; see WGL, GLX, CGL, or whatever you use
} while(t < T);


EDIT: swapBuffers() added in code snippets

[Edited by - haegarr on December 6, 2007 5:26:17 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Paultergeist
I believe i have to use the glPushMatrix and glPopMatrix, but i can´t understand how...

glPushMatrix and glPopMatrix are appropriate when local co-ordinate spaces play a role. They can be used in general as long as you hasn't started to render a new frame.1 Here you are playing an animation that naturally is required to last over several frames, so push/pop doesn't help you. I recommend you to track things like animated positions always in your own code instead of relying on OpenGL's matrix stack!

1You _can_ solve the problem by using push/pop, but it would be more an academic solution since it will not be possible any more when the rendered scene becomes more complex.

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!