# ball rotation around own axis

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

## Recommended Posts

Happy making a nice ball in big black space. I want to rotate the ball but somehow my rotation of the ball is not going exactly as I want it to be. I want the ball to keep its own local coordinate system, and rotate around its local x, y or z axis. The ball is located at (0,0,0). So basically I do: translate() (Take steps back to look at the ball in space.) rotate(degreesx, x, 0, 0) rotate(degreesy, 0, y, 0) rotate(degreesz, 0, 0, z) The ball always correctly rotates around its own z-axis, but the x and y do not rotate around their respective axis. (If I only rotate one of the values it works fine, but combinations do not have desired results.) Rereading the opengl guides... basically this should work but it doesn't. What am I overlooking?

##### Share on other sites
The usual answer: rather than using a 'from scratch' Euler-angle construction, you'll need to use incremental rotations (this will most likely necessitate using a third-party math library, as it will be somewhat difficult to do exclusively through OpenGL function calls).

##### Share on other sites
Solution was just basic vector and matrix calculation. Brushed up my knowledge and it is working.

What did I do? Well..

Needed a function that projects the original-axis you want to rotate around to a new vector, based on the current GL_MODELVIEW_MATRIX.
This function does that:
------------------------------------------------------------
void project_vect_using_matrix(float (*vector)[3], float matrix[16])
{
float result_vect[3];
result_vect[0] = (*vector)[0] * matrix[0] + (*vector)[1] * matrix[4] + (*vector)[2] * matrix[8];
result_vect[1] = (*vector)[0] * matrix[1] + (*vector)[1] * matrix[5] + (*vector)[2] * matrix[9];
result_vect[2] = (*vector)[0] * matrix[2] + (*vector)[1] * matrix[6] + (*vector)[2] * matrix[10];
}
--------------------------------------------------------
Then in the main loop I needed to obtain the modelview matrix.
Put that into the above stated function with an original axis you want to rotate around (in my case the x-axis, y-axis or z-axis). The obtained rotation vector can be directly applied to standard glRotation.
--------------------------------------------------------
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
project_vect_using_matrix(&rotation_vector, modelview);
glRotatef(rotationAngle, rotation_vector[0], rotation_vector[1], rotation_vector[2]);
--------------------------------------------------------
Then it was a little bit of puzzle with function calls to opengl.
Like changing the initialization of the loop, and the camera transforms.
--------------------------------------------------------
glMatrixMode(GL_MODELVIEW);

if(flag_firstRun == 0) {

/* reset modelview matrix to the identity matrix */

/* move the camera back three units */
glTranslatef(0.0, 0.0, -3.0);
flag_firstRun = 1;
}
else
{
}
--------------------------------------------------------

After all it was a nice learning experience.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633662
• Total Posts
3013229
×