I want to rotate a 3D scene In the way it should be using a trackball but using a mouse. I´m sure most of you know whats the gimbal lock problem. I´ve found some papers and a library but it´s not useful for me. Do you know where to find some information that can teach me the math basis of the problem and how to solve it?. I am trying to do it mapping the coordinates of the camera over a virtual sphere without the use of quats but It´s quite difficult. Where can I find a “stupids for” tutorial about this?..

Assuming that you have a target ‘up’ vector, you can do a cross product between that and your scene’s ‘up’ vector (prolly (0;1;0)). Normalize the result, that’s your rotation axis. Get the angle between the two up vectors. If the angle is very small, don’t bother to rotate, as your rotation axis may be very close to (0;0;0) anyway, otherwise that’s your rotation angle. Use angle-axis rotation (glRotatef(angle,axis.x,axis.y,axiz.z); or so), using these two results. That should solve gimbal lock.

Pitch your head up 90 degrees. p=90,y=0,r=0. You are looking straight up.

Roll CW 90 degrees. p=90,y=0,r=90. You are still looking straight up but now you have turned to the left.

Pitch down 90 degrees. p=0, y=0, r=90.
You are now looking to the left with your head straight (as if p=0,y=90,r=0), but according to your Euler angles, you should be looking straight ahead with your head tilted over to the right.

There are two ways to fix this.

Use a rotation matrix or quaternions to keep track orientation.

Allow only one angle (typically yaw) to go up to (and beyond) +/- 90 degrees.

I don’t agree with the math above, it is like the proof that 1==2, you are ‘dividing by zero’.

Yaw is not a defined quantity when pitch = 90 degrees. Therein lies the strangeness.

Theorem: 2 = 1

Proof:

Given: A = B
Multiply by A: A^2 = BA
Subtract B^2: A^2 - B^2 = BA - B^2
Factor: (A + B) (A - B) = B (A - B)
Cancel: (A + B) = B
Substitute: B + B = B
Collect: 2B = B
Cancel: 2 = 1

QED

(Acknowledge Gerry Roach for the proof typing)

[This message has been edited by nickels (edited 03-19-2002).]