cant get it to rotate right

Started by
5 comments, last by TheAdmiral 17 years, 6 months ago
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.
Advertisement
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....
9x out of 10 this is a model space problem. Make sure your model is oriented the way you think it is.

Happy coding!
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.
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
Admiral

[Edited by - TheAdmiral on September 28, 2006 2:26:26 PM]
Ring3 Circus - Diary of a programmer, journal of a hacker.
Quote:Original post by TheAdmiral
glTranslatef(-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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by JohnBolton
Not quite.
Ah... hence the disclaimer [wink].
Duly edited - thanks for pointing that out.

Admiral

Ring3 Circus - Diary of a programmer, journal of a hacker.

This topic is closed to new replies.

Advertisement