PDA

View Full Version : Problems with translations/rotations...

Evil-Dog
07-29-2001, 09:12 AM
I'm looking for a way to do my rotation around x,y and z indepently to prevent that the second rotation depends on the first one.
I don't know if my request is clear.
I have that code :
glTranslatef(GetPos().x, GetPos().y, GetPos().z);

glRotatef(GetAngle().x,1.0f,0.0f,0.0f);
glRotatef(GetAngle().y,0.0f,1.0f,0.0f);
glRotatef(GetAngle().z,0.0f,0.0f,1.0f);

The object have it's own x,y and z rotation angle
This objective is to have like a spaceship that can turn left/right, roll left/right and pitch up/down
but for exemple if I turn left 90 degrees the pitch down with in fact roll the spaceship cause the ship is now turned....you see the problem ? It's tough to explain...Hope you understand...
The weird thing is that the first works fine....the second depends on the first and the third is fine....
Help me please !!

Thanks for your concern ! http://www.opengl.org/discussion_boards/ubb/smile.gif

Evil-Dog
07-29-2001, 09:14 AM
Oops I made a mistake
I mean the first "rotation" works fine...the second depends on the first one and the 3rd one works fine too...

Aster
07-29-2001, 10:44 AM
I have encountered the same problem when creating a camera system that can move in an arbitrary direction in space. The way I solved it was to save the current direction vector and rotate it every time you rotate the camera. By default, the camera is looking on the negative z axis, so the original direction vector is always d0 = (x0, y0, z0) = (0, 0, -1). If your current direction vector is d1 = (x1, y1, z1), you can easily calculate the rotation angle and vector around which you rotate. Take dot product d0 . d1 = x0 * x1 + y0 * y1 + z0 * z1. It is also equal to |d0| * |d1| * cos(angle between d0 and d1). Now you know the angle of rotation, alpha. Then take cross product r = (xr, yr, zr) = d0 x d1 = (y0 * z1 - z0 * y1, z0 * x1 - x0 * z1, x0 * y1 - y0 * x1). Now you know the vector around which you rotate. Then,

glRotatef(alpha, xr, yr, zr);
glTranslatef(-posX, -posY, -posZ);

, where (posX, posY, posZ) is the position of the camera, and

/* draw your world. */

The problem arises because of the order of rotations. Try to change the order and you'll see that the other rotations will be dependent on the first one. Rotating our "virtual" direction vector around x, y, and then z axes and then calling a single glRotate will not cause a problem. Although there are couple of other problems that are worth mentioning. Rotating the direction vector by small increments every time is not recommended because floating-point calculation errors accumulate. So I used doubles for maximum precision; I calculated the rotation matrix only once to minimize overhead, and I normalized the direction vector after each rotation. I hope this was helpful.

Cheers

Evil-Dog
07-29-2001, 10:49 AM
Thanks a lot Aster !
That was a very complete reply I'll try it and post what hapenned !

Thanks again http://www.opengl.org/discussion_boards/ubb/smile.gif