Jump to content
  • Advertisement
Sign in to follow this  
Gurbe Plotkop

ball rotation around own axis

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 */
glLoadIdentity();

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

After all it was a nice learning experience.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!