Advertisement

Rotations along object axis

Started by January 26, 2003 09:08 AM
4 comments, last by Morat 22 years, 1 month ago
I''m working on a "fly in space" type of game, and I''m haing a bit of trouble getting the rotations right. I tried using glRotatef, rotating around the X,Yand Z axis in turn. However, its seems that unlike with translate and scale, the coordinate system of the world does not rotate, and all three rotations end up using the same unmodified coordinate system, instead of only the first rotation using an unmodified coordinate system. Not sure if that came across clearly.. I have a space ship (drawn in origo) that I want to apply Pitch, Yaw and Roll to, but only one of these turns out correct. The other two no longer use the ship''s axises. Grateful for help or a pointer to a tutorial that explains how to make rotations "stack" like translation and scale. Thanks, Morat
Sounds like you are having the same problems I had a few days ago. The first question would be where did you define the center of your model to be? The center is where the coordinates 0,0,0 would be in your model. If these coordinates are not in the center of your model then your model will not rotate the way you expect it to. If your model is centered, are you using 3 seperate calls to glRotatef() to set up your rotation about the three axis? You must give a call to glRotatef() to rotate on the x-axis then another call for the y-axis rotation and finaly a call for the z-axis. The first parameter in glRotatef() is for the rotation angle about an axis. The last 3 parameters for glRotatef() define the axis. here is an example of rotating about the x-axis : glRotatef(angle, 1, 0, 0). Here is sample code that I use for positioning a model and then rotating it about its axis:

// Select The Modelview Matrix
glMatrixMode(GL_MODELVIEW);

// Reset The Current Modelview Matrix to the centre of the world
glLoadIdentity();

// postion the model in the world; the models center is placed at this position
glTranslatef(Model->X, Model->Y, Model->Z);

// rotate the model about its x-axis
glRotatef(Model->AngleX, 1.0f, 0.0f, 0.0f);

// rotate the model about its y-axis
glRotatef(Model->AngleY, 0.0f, 1.0f, 0.0f);

// rotate the model about its z-axis
glRotatef(Model->AngleZ, 0.0f, 0.0f, 1.0f);

Hope that helps.
Advertisement
Thanks for the quick reply!

What you have described is exactly what I do. Three rotation, one alone each axis (and the model is centered in 0,0,0).

But only the first perfomed rotation (ie the last stated since opengl does it backwards) works as it should. The other two goes wierd on me.

I''m clueless.

Morat
check the ''game tutorial'' column in NeHe, the one about free 3d movement. It will teach you how to use quaternion to achive exactly what you want. I use this tutorial to make my game,Sky Assault - http://www.geocities.com/skyassault2002

Before this I use a rotation matrix to keep track of my ship orientation.When i want to rotate the ship with a rotation vector(rotation in x, y and z axis), I multiply the vector with the matrix to get the body space rotation vector. then i make another temporary rotation matrix from the vector and multiply this temporary matrix with the ship''s matrix to rotate it.

The problem that occurs to you is called gimbal lock. Without using quarternion or rotation matrix it''s very difficult to solve this problem(or imposible....)
Thanks, I''ll check it out at once!

Morat
Hmmmm, maybe I should look into that. I have had that exact same problem since I started playing with OpenGL about a year ago. When you rotate stuff, sometimes the axes move with you, other times they stay the same as before you rotate. It is very frustrating to get it to work the way you want. I''ve noticed that changing the order in which you call glRotate for each of the axes can change the result. That''s basically the cheap workaround that I have used for now

maybe quaternions are what i need

This topic is closed to new replies.

Advertisement