# Problems with translations/rotations...

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…

Thanks for your concern ! I mean the first “rotation” works fine…the second depends on the first one and the 3rd one works fine too…

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

Thanks again 