logo back up home forward   further reading more topics »

Maths - Conversion Quaternion to Euler

Definition of terms:

Java code to do conversion:

This depends on what conventions are used for the Euler Angles. The following assumes NASA Standard Airplane
/** assumes q1 is a normalised quaternion */

public void set(Quat4d q1) {
double sqw = q1.w*q1.w;
double sqx = q1.x*q1.x;
double sqy = q1.y*q1.y;
double sqz = q1.z*q1.z;
heading = Math.atan2(2.0 * (q1.x*q1.y + q1.z*q1.w),(sqx - sqy - sqz + sqw));
bank = Math.atan2(2.0 * (q1.y*q1.z + q1.x*q1.w),(-sqx - sqy + sqz + sqw));
attitude = Math.asin(-2.0 * (q1.x*q1.z - q1.y*q1.w));
}

Note1: I have replaced Math.atan(a/b) with Math.atan2(a,b) as suggested by Mark Elson

Note2: minorlogic has said that the above expressions for heading and bank dont require q1 to be normalised, but the expression for attitude does. Andy has suggested including the normalisation in the attitude expression as follows (this saves a CPU heavy square root operation).

/** q1 does not need to be normalised*/
public void set(Quat4d q1) {
   double sqw = q1.w*q1.w;
   double sqx = q1.x*q1.x;
   double sqy = q1.y*q1.y;
   double sqz = q1.z*q1.z;
   heading = Math.atan2(2.0 * (q1.x*q1.y + q1.z*q1.w),(sqx - sqy - sqz + sqw));
   bank = Math.atan2(2.0 * (q1.y*q1.z + q1.x*q1.w),(-sqx - sqy + sqz + sqw));
   attitude = Math.asin(-2.0 * (q1.x*q1.z - q1.y*q1.w)/(sqx + sqy + sqz + sqw));
}

Derivation

Thank you Andy for the following. Comparing the matrix derived above to the matrix expressed in the quaternion component terms from the quaternionToMatrix page, we can derive the quaternion to Euler transformations on your quaternionToEuler page:

image

so that

image

Likewise,

image   so  image

and

image

so

image

These transformations are fully self-consistent and can be verified by substituting the Euler angle expressions for the quaternion components. For example, take the expression for q:

image


Similar, but messier, verifications can be done for the other two angles:

image


image

Issues

The trig functions are many to one, therefore the inverse trig functions have many possible results. We usually assume that:
acos returns the angle between 0 an pi
asin returns the angle between -pi/2 an pi/2
atan returns the angle between -pi/2 an pi/2

This is what the java Math functions return for instance. I think this makes sense in the context of finding euler angles because we would usually want to rotate the shortest angle to rotate.

Example

we take the 90 degree rotation from this: rightup to this: rightForward

As shown here the quaternion for this rotation is: (0.7071+ i 0.7071)

So using the above result:

sqw = q1.w*q1.w = 0.5
sqx = q1.x*q1.x = 0.5
sqy = q1.y*q1.y = 0
sqz = q1.z*q1.z =0
heading = atan2(2.0 * (q1.x*q1.y + q1.z*q1.w),(sqx - sqy - sqz + sqw)) = atan2(0,2) = 0
bank = atan2(2.0 * (q1.y*q1.z + q1.x*q1.w),(-sqx - sqy + sqz + sqw)) = atan2(0.5,0) = 90 degrees
attitude = asin(-2.0 * (q1.x*q1.z - q1.y*q1.w)/sqx + sqy + sqz + sqw) = asin(0/2) = 0

So this gives the correct result shown here, it is banking by 90 degrees, but we have to be very careful about the following issues:


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover If you are interested in 3D games, this looks like a good book to have on the shelf. If, like me, you want to have know the theory and how it is derived then there is a lot for you here. Including - Graphics pipeline, scenegraph, picking, collision detection, bezier curves, surfaces, key frame animation, level of detail, terrain, quadtrees & octtrees, special effects, numerical methods. Includes CDROM with code.

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to the software project, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

 

cover Dark Basic Professional Edition - It is better to get this professional edition

cover This is a version of basic designed for building games, for example to rotate a cube you might do the following:
make object cube 1,100
for x=1 to 360
rotate object 1,x,x,0
next x

cover Game Programming with Darkbasic - book for above software

Can you help?

Please send me any improvements to here. I would appreciate ideas to make the pages more useful including error correction, ideas for new pages, improvements to wording. It helps if you quote the full URL of the page.

 

progam

I am working on a project which uses these principles, if you would like to help me with this you are welcome to join in, here:

http://sourceforge.net/projects/mjbworld/

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2008 Martin John Baker - All rights reserved - privacy policy.