# Rotate Objects

I have drawn a cube. Assume that this cube is not on the y axis. I am trying to rotate this cube around an axis that is parallel to the y axis and runs through the center of the cube. How do I do this?

Translate it to the origin, rotate it in the y, translate it back again to its original position.

To make rotation of an object simple, always make the center of the object its origin (0,0,0).
That way you can just rotate it then translate it to the correct position.
Else you have to move the point that you want to rotate from to 0,0,0. Let’s say you want to rotate a cube from one corner, then you would translate that corner to 0,0,0 then rotate it.

Originally posted by jjoseph1981:
I have drawn a cube. Assume that this cube is not on the y axis. I am trying to rotate this cube around an axis that is parallel to the y axis and runs through the center of the cube. How do I do this?

Originally posted by blood.angel:
Translate it to the origin, rotate it in the y, translate it back again to its original position.

That is wrong.

Translate the cube to where you want it, then rotate it around its y axis. That is how OpenGL works. If you translate after rotating, the translation is done in the rotated coordinate system.

Originally posted by nexusone:
[b]Let’s say you want to rotate a cube from one corner, then you would translate that corner to 0,0,0 then rotate it.

[/b]

This is not quite correct. To rotate something around a point other than its origin, translate its origin to the point, rotate, then translate it back.

[This message has been edited by Jambolo (edited 03-10-2002).]

Try something like this:

// Where Rot_Y is the amount you want
// rotate on every rotaion event.
glRotatef( Rot_Y, 0.0, 1.0, 0.0 );

//here we draw the cude…

//**********************************

If you want to translate the object to some other place and then rotate it, you should do something like:

// Where Trans_Y is the amount
// you want to translate
glTranslatef( 0.0, Trans_Y, 0.0 );

glRotatef( Rot_Y, 0.0, 1.0, 0.0 );

// here we draw the cude…

// are translating the object to the origin

If you rotate your object you will also rotate its axes… So if you rotate and then translate , you will have the object rotated to its new position, and then translated according to such rotated axis. What this mean is that if you rotate the object 45 degrees, it will be translated to a perpendicular position rather than along the axis you wanted to tranlate it on, e.g. your Y. There are a few reasons why you do this and why you dont do this…
Take a piece of paper. Rotate an object with its axis, and then translate it. Where will the object go? Then translate it first and then rotate it…! You will see the difference.

Have fun.

Hey guys, thanks alot. I got it working some time back. I’m sorry i couldn’t thank you guys earlier.

The correct way is :

glTranslate(origin points)
glRotatef(desired axis)
glTranslate(to desired position)

Even if I didn’t word my reply correctly, you did it the why I trying to get across…

Originally posted by jjoseph1981:
[b]Hey guys, thanks alot. I got it working some time back. I’m sorry i couldn’t thank you guys earlier.

The correct way is :