Suppose I have gimbal angles pitch, yaw and roll. Could any one tell me how to convert from gimbal angles to quaternion?

Any suggestion will be highly appreciated.

From the wildmagic libary (Matrix3.h):

First go from Euler angles to a rotation matrix:

template <typename Real>

void Matrix3<Real>::MakeEulerXYZ (Real xAngle, Real yAngle, Real zAngle)

{

Real cs, sn;

`cs = Math<Real>::Cos(xAngle); sn = Math<Real>::Sin(xAngle); Matrix3 xMat( (Real)1, (Real)0, (Real)0, (Real)0, cs, -sn, (Real)0, sn, cs);`

`cs = Math<Real>::Cos(yAngle); sn = Math<Real>::Sin(yAngle); Matrix3 yMat( cs, (Real)0, sn, (Real)0, (Real)1, (Real)0, -sn, (Real)0, cs);`

`cs = Math<Real>::Cos(zAngle); sn = Math<Real>::Sin(zAngle); Matrix3 zMat( cs, -sn, (Real)0, sn, cs, (Real)0, (Real)0, (Real)0, (Real)1);`

`*this = xMat*(yMat*zMat);`

}

And then from a rotation matrix to a quaternion.

//----------------------------------------------------------------------------

void HQuaternion::FromRotationMatrix (const HMatrix& rot)

{

// Algorithm in Ken Shoemake’s article in 1987 SIGGRAPH course notes

// article “HQuaternion Calculus and Fast Animation”.

`const int next[3] = { 1, 2, 0 };`

`float trace = rot(0,0) + rot(1,1) + rot(2,2); float root;`

`if (trace > (float)0) { // |w| > 1/2, may as well choose w > 1/2 root = sqrtf(trace + 1.0f); // 2w mTuple[0] = 0.5f*root; root = 0.5f/root; // 1/(4w) mTuple[1] = (rot(2,1) - rot(1,2))*root; mTuple[2] = (rot(0,2) - rot(2,0))*root; mTuple[3] = (rot(1,0) - rot(0,1))*root; } else { // |w| <= 1/2 int i = 0; if (rot(1,1) > rot(0,0)) { i = 1; } if (rot(2,2) > rot(i,i)) { i = 2; } int j = next[i]; int k = next[j];`

`root = sqrtf(rot(i,i) - rot(j,j) - rot(k,k) + (float)1); float* quat[3] = { &mTuple[1], &mTuple[2], &mTuple[3] }; *quat[i] = 0.5f*root; root = 0.5f/root; mTuple[0] = (rot(k,j) - rot(j,k))*root; *quat[j] = (rot(j,i) + rot(i,j))*root; *quat[k] = (rot(k,i) + rot(i,k))*root; }`

}

There are some details… This is using a left handed coord sys, and the euler angle are X first, Y, then Z. I don’t think either of these conform to the standard flight system, but this hopefully will give you a start.

The code is at:

wildmagic

Every rotation can be converted to quaternion singularly and then combined.

qx = [sin(pitch*0.5f), 0, 0, cos(pitch*0.5f)];

qy = [0, sin(yaw*0.5f), 0, cos(yaw*0.5f)];

qz = [0, 0, sin(roll*0.5f), cos(roll*0.5f)];

and now you can combine the quaternion in the order you prefer.

qFinal = qx*qy*qz;

I leave the simplification as exercise.

Nice, hadn’t thought about that. Skip the rotation matrix altogether…

Thanks a lot to both of you!

Be warned though, quats are just one parametrization of possible rotations out of many. You can get gimbal lock with quats too.

Can you post an example of that?

As far as I understand, each parametrization has it’s own attributes / flaws. Quaternions don’t suffer from gimbal lock, while euler coordinates do.

Not true.

You get a gimbal lock example on page 53. The idea is that you can combine quats representing rotations much like you can matrices. You can convert rotation matrices into quats. So what if you combine quats representing rotations around x, y, z axes? The results must be identical as when combining rotation matrices and so you get gimbal lock.

Because you are using quaternion in a wrong way.

If you use Euler to compute matrix and then matrix to compute quaternion (or Euler to compute quaternion) you still have gimbal lock but is not a problem of quaternion is the same Euler problem.

Quaternion are used to represent an orientation, given ANY orientation you are still able to rotate around all axis.

If you are using C++, take a look at the quaternion example at:

http://clanlib.org/wiki/Examples#3D_examples

(You will need to download the complete source ( http://clanlib.org/wiki/Download ) for the example )

It shows how quaternion vs euler angles work.

Since clanlib’s has a BSD style license, you should be able to extract the code (if required).

Rosario, you miss the point. Quaternions are able to represent *ALL* rotations, but so are Euler rotation matrices, including the gimbal lock case rotations. They are simply a parametrization. You can avoid gimbal lock with matrices much the same as with quaternions.

Oh, come on, ugluk! Wiki does a complete definition of the gimbal lock: http://en.wikipedia.org/wiki/Gimbal_lock

The gimbal system is a physical representation of Euler rotation coordinate space. It has a lock, but it has nothing to do with quaternions.

You can avoid gimbal lock with matrices much the same as with quaternions.

Matrix contain arbitrary rotation, but it does not represent a particular rotation coordinate space (you can’t just change any component and still have a rotation matrix). It can be constructed from both Euler and quaternion given rotations.

Your initial statement was about quaternions, not matrices:

You can get gimbal lock with quats too.

Matrix contain arbitrary rotation, but it does not represent a particular rotation coordinate space (you can’t just change any component and still have a rotation matrix). It can be constructed from both Euler and quaternion given rotations.

What do you mean by “it does not represent a particular rotation coordinate space”. Certainly, the columns of a rotation matrix are the basis vectors of a “coordinate space”. That’s why the rotation matrices are orthogonal.

You can even change any component of the matrix and reorthogonalize and renormalize the matrix. With quats you need to renormalize the quat.

Your initial statement was about quaternions, not matrices:

I gave you a reference to an example, so please comment on the example. I did not say I used quats built from euler matrices in my programs.

thanks for this one… it helps me a lot.

Ok…

I am making a helicopter sim.

I use euler to quaternions to rotate my camera perfectly.

No gimbal lock.

However…

When you rotate an object, the object will gimbal lock…whether using euler matrix, or euler quaternions.

If you see a flight sim using opengl…they are cheating the gimbal lock im sure of it.

My guess is they are using an invisible viewport with a cam rotating around the object in question,

then posting it as an image.

Woops, didnt mean to release any secrets, just trying to promote more flight sims!

hey thankz a lot but how to avoid gimble lock using matrices

Do not build the rotation matrix from component rotations. Build it from a representation where the rotation is succinctly represented by a single rotation.

An example of this (if applied properly) is a unit quaternion. If you need to handle, not just changes in rotation, but changes in translation too, consider using a dual unit quaternion.