logo back up home forward   further reading more topics »

Maths - Conversion Euler to Quaternion

Definition of terms:

This depends on what conventions are used for the Euler Angles. The following assumes NASA Standard Airplane.

The components of the quaternion (w,x,y,z) are:

w = c1c2c3 + s1s2s3
x = c1c2s3 - s1s2c3
y = c1s2c3 + s1c2s3
z = s1c2c3 - c1s2s3

where:

An alternative form is:

w = Math.sqrt(1.0 + C1 * C2 + C1*C3 + S1 * S2 * S3 + C2*C3) / 2
x = (C2 * S3 + C1 * S3 -S1 * S2 * C3) / (4.0 * w)
y = (S1 * S3 + C1 * S2 * C3 +S2) /(4.0 * w)
z = (S1 * C2 + S1 * C3 - C1 * S2 * S3) / (4.0 * w)

where:

Thanks to Mark for letting me know that I originally omitted to point out that the angles are divided by 2 in the first case but not the second. So now the second form is shown upper case (C1) to make it clear that the heading is used directly and not divided by 2. Mark has also included some other useful references.

I don't know which of these forms is most stable? However, as William points out the first is better because it requires the same number of trig operations, no square root, no worry about dividing by zero, uses familiar formulae, and is fairly clearly normalised.

William has proved here that the methods are equivalent.

Derivation

Thanks to Andy for the following:

From the basic definition of a quaternion, we can easily express the quaternion form for rotation along the X, Y, and Z axes, representing bank, attitude, and heading, respectively. Recall that the rotation quaternion (w, x, y, z) for the angle q is encoded as image  and (x, y, z) represents a vector of length image  along the axis of rotation. Note also that the handedness of the rotation must agree with the handedness of the coordinate system for the quaternion to be valid. Then

image

image

image

To express the rotation of an arbitrary set of Euler angles φ, θ, and ψ we compose the rotations about the three axes by multiplying the quaternions. In the NASA standard aeroplane, the rotations are applied in the order heading, attitude, and bank. However, because each rotation is expressed in the frame of reference of the aircraft, from an outside frame of reference they must be composed in the reverse order. Remember that multiple quaternions are applied right to left:

image

Substituting and multiplying out, we get

image

Code

First method (Euler parameter method)

 public final void rotate(double heading, double attitude, double bank) {
// Assuming the angles are in radians.
double c1 = Math.cos(heading/2);
double s1 = Math.sin(heading/2);
double c2 = Math.cos(attitude/2);
double s2 = Math.sin(attitude/2);
double c3 = Math.cos(bank/2);
double s3 = Math.sin(bank/2); double c1c2 = c1*c2;
double s1s2 = s1*s2;
w =c1c2*c3 + s1s2*s3; x =c1c2*s3 - s1s2*c3; y =c1*s2*c3 + s1*c2*s3;
z =s1*c2*c3 - c1*s2*s3; }

Second method

 public final void rotate(double heading, double attitude, double bank) {
// Assuming the angles are in radians.
Double c1 = Math.cos(heading);
double s1 = Math.sin(heading);
double c2 = Math.cos(attitude);
double s2 = Math.sin(attitude);
double c3 = Math.cos(bank);
double s3 = Math.sin(bank); w = Math.sqrt(1.0 + c1 * c2 + c1*c3 + s1 * s2 * s3 + c2*c3) / 2.0;
double w4 = (4.0 * w);
x = (c2 * s3 + c1 * s3 -s1 * s2 * c3) / w4 ;
y = (s1 * s3 + c1 * s2 * c3 +s2) / w4 ;
z = (s1 * c2 + s1 * c3 - c1 * s2 * s3) / w4 ; }

Example

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

As shown here the axis angle for this rotation is:

heading = 0 degrees
bank = 90 degrees
attitude = 0 degrees

so substituteing this in the above formula gives:

w = c1c2c3 + s1s2s3 = 0.7071
x = c1c2s3 - s1s2c3 = 0.7071
y = c1s2c3 + s1c2s3 = 0
z = s1c2c3 - c1s2s3 = 0

which gives the quaternion 0.7071 + i 0.7071

This agrees with the quaternions here.


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 us uk de jp fr ca Quaternions and Rotation Sequences.

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.