# Thread: rotation along the axis of the object

1. ## rotation along the axis of the object

I have a cube on screen that I can rotate using the standard: glRotatef(...). The rotation is done ok as long as it is aligned to the Global/Initial/Default XYZ axis of (1,0,0) , (0,1,0) and (0,0,1). What I am trying to do is rotate the cube along its OWN axis which obviously changes as the cube changes. How is that achieved ?
assume there is no translation. I can obtain the XYZ axis of the cube at any time and I can rotate along the local X axis for a while before the cube starts spinning in all directions. I am assuming this is due to axis no longer being 90 degrees to each other.

2. ## Re: rotation along the axis of the object

To rotate around cube axis, translate to its center, rotate around the wanted axis, then translate to its position in world space.

Remember that all these transformations are done via matrices in opengl, that is why they are done in the reverse order.

3. ## Re: rotation along the axis of the object

Thank you for the reply. The cube actually stays in the centre. i.e. it is centered at 0,0,0 so no translation is taking place.
When you rotate the cube say around the y-axis, the cube's X axis obviously changes. How can I then rotate around the X-Axis ? do I have to use glRotated(angle, X, Y, Z) ? and what values to put in the X , Y and Z. As far as I know that would be the X axis of the cube obtained from the matrix. This works ok and after a while the cube starts spinning out of control.

4. ## Re: rotation along the axis of the object

As far as I know that would be the X axis of the cube obtained from the matrix
Yes that's correct. If the cube XYZ axis are aligned at the beginning with the world space XYZ axis then the matrix that stores the cube transformation contains in each column the transformed X, Y and Z axis. The 1st column is X, the 2nd, Y and the 3rd, Z.
I think they should be normalized, but you can renormalize them after extracting them from the matrix. Then you can use these vectors as the rotation axis in the glRotate function, but remember that they should be normalized.
Now, I don't see for now why the cube is spinning out of control... It would be interesting to have more precisions about your implementation.

5. ## Re: rotation along the axis of the object

The type of rotations you are asking for are sometimes called 'Object Space Rotations'. I coded these up many years ago (before OpenGL) and have been curious to see how difficult it would be in OpenGL. My goal was to do this without using any of my own rotation or matrix multiplication subroutines - just using OpenGL calls. Took me a while to work out the kinks, but I think I have it. I've posted it in the form of a simple OpenGL-GLUT program at:

Object Space Rotations

This program displays a cube with X-Y-Z axes labeled. You can rotate the entire scene using the arrow keys. AND - you can rotate the cube around its current X, Y,or Z axis using the 'x', 'y', or 'z' key on your keyboard. The arrow keys do conventional Euler Sequence Rotations (i.e. around either the X or Y axis). The 'x', 'y', and 'z' keys call glRotate with values extracted from the current orientation matrix (called 'max' in my code). Specifically, to do an X body rotation, glRotate is called with the first row of the current orientation matrix. To do a Y body rotation, the second row of the matrix is used, and so on.

ukopenglnewbie - I believe this example implements the approach you and dletozeun have been discussing.

6. ## Re: rotation along the axis of the object

Can you repost this code? I have the exact same question as ukopenglnewbie. I think your code post on pastebin has expired.

7. ## Re: rotation along the axis of the object

Originally Posted by ducky
Can you repost this code? I have the exact same question as ukopenglnewbie. I think your code post on pastebin has expired.
A slightly improved version of my original code is posted at: Object Space Rotations

8. ## Re: rotation along the axis of the object

I have generalized the code I posted above to do both Object and Screen Space rotations. If anyone knows of a better way to do this stuff, please let us know ....

MaxH - Object and Screen Space Rotation Demo

9. ## Object and Screen Space Rotation Demo

For some reason the code I posted was deleted after about a day. So here it is again. This time with a lighted teapot instead of simply-colored cube.

Object & Screen Space Rotation Demo

10. ## Screen and Object Space Rotations

A cleaner version of the code I posted above is at:

Screen & Object Space Rotations

#### Posting Permissions

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