PDA

View Full Version : Please help me with this rotation problem



ELIMT
12-04-2001, 01:21 AM
How can I rotate a 3-D object drawn at ORIGIN
to rotate along an axis parallel to either the x or y or z axis with respect to previous rotation.

I used

glRotatef(angle_x, 1.0, 0.0, 0.0);
glRotatef(angle_y, 0.0, 1.0, 0.0);
glRotatef(angle_z, 0.0, 0.0, 1.0);
drawObject();

Where each angle is updated by clicking the mouse(using the mouse call-back function)

But what I get is something very strange.
For example when I click to rotate 20 degrees on the x-axis it is fine, but when I click to rotate 30 degrees on the z-axis, then 30 degrees on the y-axis etc. Everything seems to go wrong.

All I want is to rotate the current object
position by a degree about an axis which passes through the current object position and which is parallel to the axis(depending on which angle is being updated).

Please help me,much appreciated Thank You.

Sinned
12-04-2001, 03:45 AM
I don't know if you do this, but are you clearing your modelview matrix before you apply the rotations? If not, then the crazy results may be caused by the accumulated rotations performed on the modelview matrix. What I mean is that you might be applying your new rotations on top of the currently rotated coordinate system.

For example, on your first click you rotate the object 20 degrees along the x-axis. Then on your second click, you're expecting to rotate the object 20 degrees along the x-axis and 30 degrees along the z-axis, you are really rotating your object on a coordinate system that has already been rotated 20 degrees along the x-axis.

To clear the matrix, you can use glPushMatrix(), glLoadIdentity(), and glPopMatrix().

Sinned

rixed
12-05-2001, 03:04 AM
First, you should not use three consecutive glRotate in this case : you can easily (if you are used to) calculate the matrix thats handle the three consecutives rotations with your three angles. Just elementary linear calculus.

But to answer your question : if I understood, you want to rotate an object around the axis by saying "a little more around Z, then a little less around Y", etc, for example.

The problem is that the order of rotation matters : 20 around Z then 180 around Y is not equivalent to 180 around Y then 20 around Z. (figure it out with a long object along the X axis).
So, if you have already moved your abject around the axis, then want to turn it a little bit more around Y or around X, you can't simply add a delta to your Y (or X) angle. If you do so, your object will seams to rotate randomly. (it will allways work with the Z angle as it is your last rotation).

The solution for your problem is to KEEP the result of preceding rotations on the stack, and then just glRotate around the axes you want to turn around. Problems will rapidly shows up, however, because by keeping the top level matrix, you also keep and accumulate the calculus approximation. The solution is then to read, renormalise and repush the matrix from time to time.

(renormalisation is just ensuring that every 3 axis size is 1 and that they are all perpendicular. It requires some simple calculus but as you do not do this every frame its not a problem at all)