# Thread: How do I rotate an object around the global axes instead of local?

1. ## How do I rotate an object around the global axes instead of local?

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.

2. ## Re: How do I rotate an object around the global axes instead of local?

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.

Code :
```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();```

3. ## Re: How do I rotate an object around the global axes instead of local?

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.

4. ## Re: How do I rotate an object around the global axes instead of local?

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)

5. ## Re: How do I rotate an object around the global axes instead of local?

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)
Rotate(year, 1, 0, 0)

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

6. ## Re: How do I rotate an object around the global axes instead of local?

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);
glRotate(local);
glPopMatrix;

-nemesis-

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

7. ## Re: How do I rotate an object around the global axes instead of local?

I should have said the cube has its center at (0,0,0). Trabslating is no problem, its the local axis not staying still.

8. ## Re: How do I rotate an object around the global axes instead of local?

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.

9. ## Re: How do I rotate an object around the global axes instead of local?

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

10. ## Re: How do I rotate an object around the global axes instead of local?

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...
Code :
```  ____________
/___________/|       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).]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•