0
votes

Firstly, I have done much googling and checking other stackoverflow posts about this, but cannot get a working reply or a snippet of working code. Maths is not my strength.

I need to have a routine that takes a camera point (CX,CY,CZ) and rotate it about a lookat point (LX,LY,LZ) by three rotation angles (RX,RY,RZ). Using euler rotations leads to gimbal lock in some cases which I need to avoid. So I heard about using quaternions.

I found this to convert the rotations into a quaternion http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm

and this to convert from a quaternion back to euler XYZ rotations http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

They seem to work fine, but I need to know how to use the quaternion to rotate the CX,CY,CZ around LX,LY,LZ and then return the new CX,CY,CZ without issues of gimbal lock.

There is so much out there about this, that I am sure a good explanation and snippet of code will help not only me but many others in the future.

So please help if you can. Many thanks.

1

1 Answers

2
votes

The short answer, if your quaternion is Q and the new camera point is C':

C' = Q*(C-L)*Q^-1 + L

where the points are augmented with Cw=0 and multiplication and inverse are according to quaternion rules.

Specifically, let D = C - L. Then we let F = Q*D:

Fw = Qw*0  - Qx*Dx - Qy*Dy - Qz*Dz
Fx = Qw*Dx + Qx*0  + Qy*Dz - Qz*Dy
Fy = Qw*Dy - Qx*Dz + Qy*0  + Qz*Dx
Fz = Qw*Dz + Qx*Dy - Qy*Dx + Qz*0

Finally, we get C' = F*Q^-1 + L:

Cw' = 0
Cx' = Fw*Qx - Fx*Qw + Fy*Qz - Fz*Qy + Lx
Cy' = Fw*Qy - Fx*Qz - Fy*Qw + Fz*Qx + Ly
Cz' = Fw*Qz + Fx*Qy - Fy*Qx - Fz*Qw + Lz

However, be aware that if you're creating the quaternion from an Euler representation, you'll still end up with gimbal lock. The gimbal lock is a property of the Euler representation and the quaternion will just represent the same transformation. To get rid of gimbal lock, you'll need to avoid the Euler representation altogether, unless I misunderstand how you're using it.