logo back up home forward   further reading more topics »

Maths - Conversion Axis-Angle to Euler

Prerequisites

Definition of terms:

Equations

heading = atan((x * y * (1 - cos(angle)) + z * sin(angle)) / (1 - (y2 + z2 ) * (1 - cos (angle)))
bank = atan((y * z * (1 - cos(angle)) + x * sin(angle)) / (1 - (x2 + y2)* (1 - cos (angle)))
attitude = asin(- (x * z * (1 - cos(angle)) - y * sin(angle))

Code

x

Derivation of Equations

Euler angles represent 3 rotations about the x,y and z axis in some given order. We can replace any sequence of rotations by one single rotation about some axis.

We need to be very careful about using Euler Angles and it is best to work in terms of quaternions or matricies whenever we can. As explained in euler section there are different types of Euler angles and the result will depend on the Euler definition used. Also the following calculations use a lot of trig functions and therefor will use a lot of CPU time. The accuracy may be low, specially near the singularity of the Euler system being used.

With these warnings in mind here is the calculation.

Euler can be defined in terms of a quaternion as shown here.

heading = atan(2.0 * (qx*qy + qz*qw)/(qx2 - qy2 - qz2 + qw2))
bank = atan(2.0 * (qy*qz + qx*qw)/(-qx2 - qy2 + qz2 + qw2))
attitude = asin(-2.0 * (qx*qz - qy*qw))

This quaternion can be defined in terms of axis-angle as shown here.

qx = x * sin(angle/2);
qy = y * sin(angle/2);
qz = z * sin(angle/2);
qw = cos(angle/2)

So working out some products gives:

qx*qw = x * sin(angle/2) * cos(angle/2) = 0.5 * x * sin(angle) // using trig identity: sin(angle) = 2 sin(angle/2) cos(angle/2) from here

qy*qw = y * sin(angle/2) * cos(angle/2) = 0.5 * y * sin(angle)

qz*qw = z * sin(angle/2) * cos(angle/2) = 0.5 * z * sin(angle)

qx*qy = x * y * sin2(angle/2) = x * y * (0.5 - 0.5*cos(angle)) // using trig identity cos(angle) = 1 - 2 sin2(angle/2) from here

qy*qz = y * z * sin2(angle/2) = y * z * (0.5 - 0.5*cos(angle))

qx*qz = x * z * sin2(angle/2) = x * z * (0.5 - 0.5*cos(angle))

qx2 = x2 * sin2(angle/2) = x2 * (0.5 - 0.5*cos(angle))

qy2 = y2 * sin2(angle/2) = y2 * (0.5 - 0.5*cos(angle))

qz2 = z2 * sin2(angle/2) = z2 * (0.5 - 0.5*cos(angle))

qw2 =cos2(angle/2) = 0.5 * (cos(angle)+1) // using trig identity cos(2A) = 2 cos2(A) - 1 from here

qx2 - qy2 - qz2 + qw2
= qx2 + qy2 + qz2 + qw2 - 2qy2 - 2qz2
= 1 - 2qy2 - 2qz2
= 1 - y2 * (1 - cos(angle)) - z2 * (1 - cos(angle))
= 1 - (y2 + z2 ) * (1 - cos (angle))

-qx2 - qy2 + qz2 + qw2
= qx2 + qy2 + qz2 + qw2 -2qx2 - 2qy2
= 1 - 2qx2 - 2qy2
= 1 - x2 * (1 - cos (angle)) - y2 * (1 - cos (angle))
= 1 - (x2 + y2)* (1 - cos (angle))

So substituting in euler equations gives:

heading = atan(2.0 * (qx*qy + qz*qw)/(qx2 - qy2 - qz2 + qw2))
bank = atan(2.0 * (qy*qz + qx*qw)/(-qx2 - qy2 + qz2 + qw2))
attitude = asin(-2.0 * (qx*qz - qy*qw))

heading = atan(2.0 * (x * y * (0.5 - 0.5*cos(angle)) + 0.5 * z * sin(angle)) / (1 - (y2 + z2 ) * (1 - cos (angle)))
bank = atan(2.0 * (y * z * (0.5 - 0.5*cos(angle)) + 0.5 * x * sin(angle)) / (1 - (x2 + y2)* (1 - cos (angle)))
attitude = asin(-2.0 * (x * z * (0.5 - 0.5*cos(angle)) - 0.5 * y * sin(angle))

heading = atan((x * y * (1 - cos(angle)) + z * sin(angle)) / (1 - (y2 + z2 ) * (1 - cos (angle)))
bank = atan((y * z * (1 - cos(angle)) + x * sin(angle)) / (1 - (x2 + y2)* (1 - cos (angle)))
attitude = asin(- (x * z * (1 - cos(angle)) - y * sin(angle))

Issues

Example

we take the 90 degree rotation from this: up to this: forward

As shown here the axis angle for this rotation is:

angle = 90 degrees
axis = 1,0,0

So using the above result:

heading = atan((x * y * (1 - cos(angle)) + z * sin(angle)) / (1 - (y2 + z2 ) * (1 - cos (angle)))
bank = atan((y * z * (1 - cos(angle)) + x * sin(angle)) / (1 - (x2 + y2)* (1 - cos (angle)))
attitude = asin(- (x * z * (1 - cos(angle)) - y * sin(angle))

we substitute our axis angle: angle=90 degrees, x=1, y=0, z=0

heading = atan(0 / 1 )
bank = atan(1 / 0)
attitude = asin(0 )

which gives:

heading = 0 degrees
bank = 90 degrees (since the tangent of 90 degrees is infinity)
attitude = 0 degrees

So this gives the correct result, 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 Game Design for Teens.

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.