Archived

This topic is now archived and is closed to further replies.

Moving a rotated object

This topic is 5850 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 question about moving model objects after rotating them in Opengl. Currently my program allows a user to rotate an object using keys on the keyboard and also allows the object to be moved based on whatever direction it is facing. The object rotates nicely but moves in odd directions. The code I use looks basically like this: //camera code here ... //object code glPushMatrix(); glTranslatef(objectx,objecty,objectz); glRotatef(objectangle,objectxaxis,objectyaxis,objectzaxis); //get current viewing matrix glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrixobject); //move the object for the next loop objectx+=modelMatrixobject[2]*speed; objecty+=modelMatrixobject[6]*speed; objectz+=modelMatrixobject[10]*speed; DrawObject(); glPopMatrix(); To obtain movement values I also tried using a function I have to create a matrix from the rotation values that returns the same types of values that the glGetDoublev call did but this method did not work either. The object moves in very strange directions that do not correspond at all to where it is facing. Is there something I am missing? My camera movement works fine using glRotatef and then glTranslatef but correct object movement does not seem possible using these methods. If anyone can help it would be much appreciated.

Share this post


Link to post
Share on other sites
Do all translations (movement) BEFORE you do ANY rotations. Rotating even once will screw up all future movement.

------------------------------
Trent (ShiningKnight)
E-mail me
"Whoso is a man, is a nonconformist"
--Ralph Waldo Emerson

Share this post


Link to post
Share on other sites
All translations are done before rotating. I even tried moving the code that adds to the object position to many different places in the DrawScene function but the result is the same. The modelmatrix values I can get for the movement look right but in most cases the object just does not move where it is supposed to, often moving backwards or sideways. Since object movement is done in so many demos and games it seems weird it should be this difficult.

Share this post


Link to post
Share on other sites
Could be your math...

Try also...

push matrix
translating
push matrix
rotate
pop matrix

draw object

calc new position

pop matrix

Edited by - ANSI2000 on December 11, 2001 1:58:00 PM

Share this post


Link to post
Share on other sites
Isn''t it rotate before move? Because, well, maybe ASCII art will explain...

symbols- + is un-rotated object, x is rotated object, lines show movement

Your way:
Translate first Then rotate
Say, -3 on the Z


+ x
| /
| /
+ +

Notice the rotate rotates EVERYTHING around the origin, not just the object.

Rotate First Then translate
x
|
|
x x




Share this post


Link to post
Share on other sites
Oops my ASCII art got messed up, so I''ll try to draw it again
First time, translate first
+
|
|
+

then, rotate
___x
__/
_/
+

Second time: rotate first
x
then, translate
x
|
|
x

glRotatef rotates everything around the origin, but does not change the translate direction.

Share this post


Link to post
Share on other sites
Movement must be done before rotating it because if you rotate the object first and then move it, it will just be rotating in a big circle around the origin. For example, if it is being rotated 30 degrees around the y axis, moving and then translating makes it so that it is at its correct position and turned 30 degrees. If I modify the program to rotate and then translate, it turns first and then moves straight in the direction that it was rotated. When the program is run like this and I hold down a key to rotate it, it continually spins in a large circle around the origin with the larger the numbers passed to glTranslatef, the larger the circle radius. Moving and then translating works perfectly for the rotation but not for the movement. Maybe these methods are not how people normally achieve object movement. Oh well, thanks for the help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what does your drawObject() method do - make sure it does something like

glBegin(GL_TRINAGLES);
glVertex3f(-0.5, 0.0, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glEnd();

(to draw a triangle around the place translated to)

as you are translating into place then drawing your object (if you are already doing this then ignore)

Share this post


Link to post
Share on other sites