Cylinder Rotation

I need to calculate cylinder rotation angel on a chosen bone.
I made the following calculation but it doesn’t work.
Please tell me what are my mistakes.

Thanks a lot for your time.

tVector vec;
GLUquadricObj *cyl;
cyl = gluNewQuadric();
/////Find the center mass of the chosen bone///////
////finds the difference between the base of the bone and its center mass//////////
VectorDifference(¢er,&selectedBone->trans, &vec);
glTranslatef(center.x, center.y, center.z-0.5);
glRotatef(atan2(vec.y,vec.x), 0.0f, 0.0f, 1.0f);
glRotatef(atan2(vec.y,vec.z), 0.0f, 1.0f, 0.0f);
glRotatef(atan2(vec.z,vec.x), 1.0f, 0.0f, 0.0f);
gluCylinder(cyl,cylinder_radius,cylinder_radius,cylinder_height, 30, 30);

All those glRotate’s and glTranslate’s make my head spin. You should do that stuff on your own and use glMultMatrix.

Why? Because its ugly and results in mixing of drawing and physics code, which is bad.

[This message has been edited by ioquan (edited 04-13-2003).]

Those rotates won’t actually do what you think they should do.

Use your own orientation representation, either matrices or quaternions, and use them for all your math. That will make the math look as simple as for position.

Can you tell me how to work with

Generally you store the orientations of your objects as either quaternions or matrices, or something similar to matrices which I call “a 3 vector system”. I will explain the 3 vector system, because it is easier to understand and work with.

The 3 vector system means that your object’s orientation is stored as 3 vectors, one each for its local x,y, and z axes. You perform rotations on these vectors directly (i.e. no use of glRotate). Doing this gives you a great amount of control over how your object rotates.

Imagine an airplane. When the nose tilts up, it is rotating around the x axis. When this happens, the y and z orientation vectors change, and the x one stays the same. Now lets say you want to roll the plane (so one wing is higher than the other). To do this you would rotate the x and y vectors around the z vector. …Does that make sense so far?

In order to achieve that, you will need some functions that can rotate points/vectors around an axis vector.

Once you have this, using glMultMatrix to orient your object is easy. You simply put the 3 vectors into the 3 rows of the matrix. You can put the position of the object in the 4th row to do translations and rotations all in one matrix. Then you simply call glMultMatrix(matrix_data); where matrix_data is a pointer to your matrix lined up in memory.

This system has many many advantages. One of those is that you always have the 3 orientation vectors available for calculations (makes it easy to determine the loooking direction, etc.) It also makes it easy to have varying behavior for different types of objects. For instance, you will want your camera to rotate differently than your other objects (the rotation around the y axis for cameras should be done around the global y axis, the vector (0,1,0), whereas the y rotation for most other objects should be around the object’s local y vector) You should use the same system for all of your objects (preferrably in a shared base class). Then you can use overriding to achieve the different types of behavior for the different object types.