# OpenGL cant get it to rotate right

This topic is 4313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, im trying to rotate objects from the center of their body. I am using visual c++ with the opengl, its rotating from the bottom left corner of the front face (+z normal - righthand coord system) of a cube. I used the glBegin(Gl_QUAD) and drew six faces that ended up as a cube. How can i get my rotate functions to rotate from the center of cube's body instead of rotating from the front face corner(+z normal - right hand coord system)? thanks for any help.

##### Share on other sites
i posted over in the opengl section, so if you have any advice please post there, i cant seem to find the place to delete my post....

##### Share on other sites
9x out of 10 this is a model space problem. Make sure your model is oriented the way you think it is.

Happy coding!

##### Share on other sites
make sure one corner of your cube is not sitting at 0,0,0. that would cause a problem. if it is, translate your cube so that 0,0,0 is at the center of your cube and then try rotating it.

##### Share on other sites
You need not change the model's data if it's difficult - you can effectively change the model's orientation with a conjugate-translation:

The point about which a model rotates is the origin of the current space's coordinates. In your case, it looks like this is at the corner of the cube. In order to rotate about its centre, you need to translate the object so that it is centred about the origin. Supposing that the centre of the object is at (x0, y0, z0) wrt the current coordinates; call glTranslatef(-x0, -y0, -z0), rotate, then finally translate back. The whole operation goes from

glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);

to

(Edit:)
glTranslatef(x0, y0, z0);glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);glTranslatef(-x0, -y0, -z0);

Sorry if any of my syntax is wrong: OpenGL isn't my native tongue and I haven't used it in a while.

Regards

[Edited by - TheAdmiral on September 28, 2006 2:26:26 PM]

##### Share on other sites
Quote:
 Original post by TheAdmiralglTranslatef(-x0, -y0, -z0);glRotatef(angle, x_axis_component, y_axis_component, z_axis_component);glTranslatef(x0, y0, z0);

Not quite. To rotate around an arbitrary point T in OpenGL, you do this:
    glTranslatef( T.x, T.y, T.z );    glRotatef( angle, x, y, z );    glTranslatef( -T.x, -T.y, -T.z );
In math form, it corresponds to v' = TRT-1v. Using D3D's row vector notation, it would be written as v' = vT-1RT.

##### Share on other sites
Quote:
 Original post by JohnBoltonNot quite.
Ah... hence the disclaimer [wink].
Duly edited - thanks for pointing that out.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002176
×