I have, what I think, is a working quaternion camera control. When I fly my spaceship around it has no gimbal lock. To set the camera (in OpenGL) I use gluLookAt. My up and forward vectors are calculated by the following:
up = rotation.Transform(Y_VECT);
forward = rotation.Transform(Z_VECT);
where rotation is a quaternion. The parameters are just basis vectors.
My Transform function looks like this. Remember that it does work (it seems):
Vector Transform(Vector v)
{
Vector nv;
nv.x = v.x*(w*w + x*x - y*y - z*z) + v.y*(2*x*y - 2*w*z) + v.z*(2*x*z + 2*w*y);
nv.y = v.x*(2*x*y + 2*w*z) + v.y*(w*w - x*x + y*y - z*z) + v.z*(2*y*z - 2*w*x);
nv.z = v.x*(2*x*z - 2*w*y) + v.y*(2*y*z + 2*w*x) + v.z*(w*w - x*x - y*y + z*z);
return nv;
}
Now, is this doing the same thing as that "q*p*q^-1" thing I've seen around? Or is it just coincidence that it works in this case?
My actual problem. When I try to use this to modify the vector that controls the camera's view relative to the ship (my "chase" vector) it gets gimbal lock. The quaternion used to rotate is a small constant increment. I make the call:
chasevect = cam_dx.Transform(chasevect);
which I thought would rotate the chase vector by the quaternion cam_dx. It doesn't work. Why?
Maybe I'll try the same way as the ship: Instead of incrementing the vector by a constant quaternion, update a new chase quaternion (my function to multiply 2 quaternions I know works) and use the chase quat to transform the original "basis" chase vector. I would think though that the methods should do the same thing.
[Edited by - Tesserex on May 5, 2007 8:11:37 PM]