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)); }
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:
so that
Likewise,
so
and
so
These transformations are fully selfconsistent and can be verified by substituting the Euler angle expressions for the quaternion components. For example, take the expression for q:
Similar, but messier, verifications can be done for the other two angles:
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.
we take the 90 degree rotation from this:  to this: 
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. 
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. 
This site may have errors. Don't use for critical systems.
Copyright (c) 19982017 Martin John Baker  All rights reserved  privacy policy.