PDA

View Full Version : How do I rotate an object around the global axes instead of local?



blood.angel
01-21-2002, 04:56 AM
glPushMatrix();
glRotatef((GLfloat) xaxis, 1.0, 0.0, 0.0);
glRotatef((GLfloat) yaxis, 0.0, 1.0, 0.0);
glRotatef((GLfloat) zaxis, 0.0, 0.0, 1.0);
draw_cube();
glPopMatrix();

The above code rotates the objects axes along with the object.

Bob
01-21-2002, 05:03 AM
Not sure about this one, just a thing I heard somewhere and haven't bothered confirming yet.

Reverse the order in which you call the rotate functions.




glPushMatrix();
glRotatef((GLfloat) zaxis, 0.0, 0.0, 1.0);
glRotatef((GLfloat) yaxis, 0.0, 1.0, 0.0);
glRotatef((GLfloat) xaxis, 1.0, 0.0, 0.0);
draw_cube();
glPopMatrix();

blood.angel
01-21-2002, 05:26 AM
Sorry Bob, but that doesnt work. I dont think it matters what order the rotates are called.

Im trying to do a rubixs cube, and when the user rotates it I want the cube to be rotated determined by the users view. Hense the global axis, not local ones.

mmcoder
01-21-2002, 05:36 AM
Yep, after that first rotate the axis changes and the following rotations don't have the desired effect. The way I do it is to build up my own rotation matrix and apply it to my verts (although you can probibly extract its rotation and apply it to modelview)

wedge
01-21-2002, 11:37 PM
I think OGL do not suport rotating around global axes but you can translate the axes befor and after rotation.
ex. Rotate a Planet:
rotate(day, 1, 0, 0)
Translate(radius, 0, 0)
Rotate(year, 1, 0, 0)


[This message has been edited by wedge (edited 01-22-2002).]

nemesis
01-22-2002, 12:46 AM
Hi all!

If the center of the object and the global center aren't the same, the object have to be translated (as wedge've said).

In the case of a rubix cube, you should define each little cube coords to (0,0,0) and afterwards translate them to its current position.

The code would look more or less like this:
glPushMatrix;
glRotate(global);
glTranslate(radius);
glRotate(local);
glPopMatrix;

-nemesis-



[This message has been edited by nemesis (edited 01-22-2002).]

blood.angel
01-22-2002, 01:17 AM
I should have said the cube has its center at (0,0,0). Trabslating is no problem, its the local axis not staying still.

garbagecollectorman
01-22-2002, 02:37 AM
Instead of thinking of it as trying to rotate the object, have you tried thinking of it as a question of translation? Instead of:

glRotatef( anyAxis, ang1, ang2, ang3 );

have you tried something like:

//d = distance form center of object to global center
//p = pitch around global center
//y = yaw around global center

glTranslate( d*sin(p)*sin(y), d*sin(p), d*sin(p)*cos(y));


? It sounds like your problem might be one of positioning the object without rotating it, which might equate to a clever use of translation rather than of rotation.

mmcoder
01-22-2002, 07:18 AM
I could be wrong but I think he's not talking about rotating about the wrong axis (as the topic says) but rather the object's axis itself being rotated by glrotate. Here's an example of code that doesn't have this problem
----
r[0] = 0.0f; r[1] = yrot; r[2] = 0.0f;
Misc_vMat (m, r);

r[0] = xrot; r[1] = 0.0f; r[2] = 0.0f;
Misc_vMat (m2, r);

Misc_mConcat (m3, m, m2);

glBegin (GL_LINE_LOOP);
v[0] = -1.0f; v[1] = 0.0f; v[2] = 0.0f;
Misc_vRotateM (res, v, m3);
glVertex3fv (res);
v[0] = 0.0f; v[1] = 1.0f; v[2] = 0.0f;
Misc_vRotateM (res, v, m3);
glVertex3fv (res);
v[0] = 1.0f; v[1] = 0.0f; v[2] = 0.0f;
Misc_vRotateM (res, v, m3);
glVertex3fv (res);
glEnd ();
---
if I had done:
glRotate (yrot, 0, 1, 0)
glRotate (xrot, 1, 0, 0)
(in any order), each rotate would have a direct effect on the following rotate because of the axis change.
I'm guessing it's because the 1,0,0 is relative to where the object has gone. Anyway, it's an unpleasant effect and if anyone knows a way to avoid it using only opengl I'd love to be informed

nemesis
01-22-2002, 09:17 AM
I think translating IS the problem.

OpenGL transformations are made one over another until you call glPopMatrix (remember they are applied from the last to the first written). It's the way OpenGL works.

I see your problem now. This topic's title is confusing.

OK, let's make a drawing...
A rumix cube is a big cub built of 36 (9+8+9)
smaller cubes. Watching it from one side...



____________
/___________/| y
| 1 | 2 | 3 | | |
|___|___|___| | |___ x
| 4 | 5 | 6 | | /
|___|___|___| | /z
| 7 | 8 | 9 | |
|___|___|___|/


For example, to rotate the upper part of the cube, being all the small cubes defined in the 0,0,0 global coords, I would do:

glPushMatrix();
glRotate(Y); // Rotate all the upper part (3)
glTranslate(Y); // Translate all the upper part (2)

for (cube1 to cubeN) // Translate each small cube in x and z (1)
{
glPushMatrix();
glTranslate(X);
glTranslate(Z);
DRAW CUBE i
glPopMatrix();
}
glPopMatrix();

The order transformations are applied is between ().

I hope it helps.

-nemesis-

[This message has been edited by nemesis (edited 01-23-2002).]