Euler and Quaternion Rotation 11
Euler and Quaternion Rotation 11
Camera rotation for the Flight Simulator - Google Earth (G.E.) swivel cam application can be computed
using either Quaternion or Euler rotation algorithms.
Input data are the same for either method: 1) Euler angle triplet from Flight Simulator (A:PLANE HEADING
DEGREES TRUE, A:ATTITUDE INDICATOR PITCH DEGREES, A:ATTITUDE INDICATOR BANK DEGREES) plus
2) Euler triplet of the camera swivel (Yaw, Tilt, Roll) via input from the HAT or POV switch on a joystick
that returns camera view angles relative to the current aircraft axes (e.g., “look left”).
The required output is the Heading-Tilt-Roll Euler angle triplet needed to aim the Google Earth camera
relative to the fixed world reference frame that G.E. uses.
1) Convert the input Euler angle triplets to either two Quaternions or two Euler Rotation Matrices
2) Calculate the compound (or concatenated or stacked) rotation through a rotation multiplication
process (Quaternion Hamilton multiplication or Euler Rotation Matrix multiplication)
3) Convert the resulting multiplication back to Euler Heading, Tilt, and Roll angles
This paper assumes Euler angles are in Tait-Bryan Yaw, Pitch, and Roll form. The rotations are intrinsic
and active (alibi) and the rotation order is z then y then x.
Equations presented here fit the coordinate system conventions of Flight Simulator (left hand Cartesian
coordinate system and rotations) and Google Earth (mixed coordinate system). The equations will differ
from those published in other sources if different conventions are used.
1
Quaternion Rotation
where:
The compound (concatenated) rotation, Flight Simulator Heading-Pitch-Bank rotation followed by Camera
Yaw-Tilt-Roll swivel, is also represented by a quaternion. The compound rotation quaternion (t) is
calculated by quaternion multiplication:
(t0, t1, t2, t3) = (r0, r1, r2, r3) * (s0, s1, s2, s3)
Quaternion coefficients r0, r1, r2, r3 and s0, s1, s2, s3 are calculated using equations 1 a-d. For Flight
Simulator, rn = qn. For the swivel camera, sn = qn.
2
Convert Quaternion to Euler (Tait-Bryan) Angles
ROLL: atan2[ t02 + t12 − t22 − t32 , 2*( t0* t3 + t1* t2 ) ] (4)
HDG : atan2[ t02 − t12 − t22 + t32 , 2*( t0* t1 + t2* t3 ) ] (5)
To prevent #NUM! Out of Range errors if using Excel, TILT should include a ROUND function, e.g.,
−1*asin[ 2*( ROUND( t0*t2 − t1*t3 , 13 ) ) ] with num_digits greater than zero. 13 is a good choice.
Quaternions are immune from the phenomenon of “gimbal lock”; however, Euler angles are not. The
issue is a mathematical singularity occurring when TILT equals ± 90.0° and the ROLL and HDG equations
both equal atan2( 0, 0 ) and are therefore invalid.
TILT (as derived from t): −1*asin[ 2*( t0*t2 - t1*t3 ) ] (no change)
The singularity can either be ignored*, or equations (4) and (5) modified to conditionally account for it.
Actually, it would be quite rare for equation (3) to yield exactly ±90° in the first place, and if it does, then
on the next Flight Simulator gauge update cycle (~56 ms later), it’s very likely to no longer be ±90° and
everything is back to normal.
Lastly, the Google Earth Tilt range convention is addressed by adding 90° to results of equation (3):
Unlike Flight Simulator Pitch, Google Earth Tilt ranges from 0° to +180°, with no negative values. In
Google Earth, a Tilt of 0° looks down, 90° is in the x-y plane, and +180° is in the ‘sky’. In level flight, Flight
Simulator returns a Pitch of near 0°, but to Google Earth, 0° Tilt points the camera down. Consequently,
to achieve the pilot’s perspective of looking forward rather than down, 90° should be added to TILT from
equation (3).
Equations 4, 5, and 6 are used for the Google Earth view as Flight Simulator is running. The full set of
equations needed is 1a-d for the Flight Sim quaternion, 1a-d for the Camera quaternion, 2a-d for the
Flight Sim-Camera Hamilton product, and 4, 5, and 6 for the final Google Earth Hdg, Tilt, and Roll angles.
3
Euler Rotation
1 0 0
R x (u) = 0 c(u ) −s(u )
0 s(u ) c(u )
c(v ) 0 s(v )
R y (v) = 0 1 0
−s(v ) 0 c(v )
c(w ) −s(w ) 0
R z (w) = s(w ) c(w ) 0
0 0 1
where:
c(v )c(w ) s(u )s(v )c(w ) − c(u )s(w ) s(u )s(w ) + c(u )s(v )c(w )
R z (w) R y (v) R x (u) = c(v )s(w ) c(u )c(w ) + s(u )s(v )s(w ) c(u )s(v )s(w ) − s(u )c(w )
−s(v ) s(u )c(v ) c(u )c(v )
Matrix multiplication order is important: Rz(w) * Ry(v) * Rx(u). In Excel, it would be written
=MMULT(Rz(w),MMULT(Ry(v), Rx(u)))
where Rz(w), Ry(v), and Rx(u) are replaced by the 3 row by 3 column range definitions (e.g., A1:C3) for
each Elemental matrix.
4
Compound Rotation: Rotation Matrix Multiplication
The objective Google Earth camera orientation is a combination of two sequential rotation sets:
or, equivalently, expressed as the product of the individual Elemental rotation matrices:
r 11 r 12 r 13
R FS ×CAM = r 21 r 22 r 23
r 31 r 32 r 33
Matrix multiplication of equation (10) yields the following elements that are provided in the event the
user’s software doesn’t include an adequate matrix or vector library.
NOTE: In the equations below, Hdg, Pitch, Bank, and Yaw, Tilt, Roll are the angles used in the Elemental
rotation matrices, equations (7), (8), and (9). They should not be confused with the compound Google
Earth angles of equations (11), (12), and (13).
5
r11 = cos(Pitch) * cos(Hdg) * cos(Tilt) * cos(Yaw) + cos(Tilt) * sin(Yaw) * sin(Bank) * sin(Pitch) *
cos(Hdg) − cos(Tilt) * sin(Yaw) * cos(Bank) * sin(Hdg) − sin(Tilt) * sin(Bank) * sin(Hdg) − sin(Tilt) *
cos(Bank) * sin(Pitch) * cos(Hdg)
r12 = cos(Pitch) * cos(Hdg) * sin(Roll) * sin(Tilt) * cos(Yaw) − cos(Pitch) * cos(Hdg) * cos(Roll) * sin(Yaw) +
sin(Bank) * sin(Pitch) * cos(Hdg) * cos(Roll) * cos(Yaw) + sin(Bank) * sin(Pitch) * cos(Hdg) * sin(Roll) *
sin(Tilt) * sin(Yaw) − cos(Bank) * sin(Hdg) * cos(Roll) * cos(Yaw) − cos(Bank) * sin(Hdg) * sin(Roll) *
sin(Tilt) * sin(Yaw) + sin(Bank) * sin(Hdg) * sin(Roll) * cos(Tilt) + cos(Bank) * sin(Pitch) * cos(Hdg) *
sin(Roll) * cos(Tilt)
r13 = cos(Pitch) * cos(Hdg) * sin(Roll) * sin(Yaw) + cos(Pitch) * cos(Hdg) * cos(Roll) * sin(Tilt) * cos(Yaw) +
sin(Bank) * sin(Pitch) * cos(Hdg) * cos(Roll) * sin(Tilt) * sin(Yaw) − sin(Bank) * sin(Pitch) * cos(Hdg) *
cos(Roll) * sin(Tilt) * sin(Yaw) − cos(Bank) * sin(Hdg) * cos(Roll) * sin(Tilt) * sin(Yaw) + cos(Bank) *
sin(Hdg) * sin(Roll) * cos(Yaw) + sin(Bank) * sin(Hdg) * cos(Roll) * cos(Tilt) + cos(Bank) * sin(Pitch) *
cos(Hdg) * cos(Roll) * cos(Tilt)
The compound rotation angles required to properly orient the Google Earth swivel camera while the
aircraft is flying are derived from the RFS×CAM rotation matrix:
6
Mathematic Singularity (“Gimbal Lock”)
Rotation Matrices are immune from the phenomenon of “gimbal lock”; however, Euler angles are not.
The issue is a mathematical singularity occurring when r31 = ±1 and r11 = r21 = r32 = r33 = 0 which yields a TILT
value of ± 90.0°, but the ROLL and HDG equations both equal atan2( 0, 0 ) and are therefore invalid.
The singularity can either be ignored*, or equations (12) and (13) modified to conditionally account for it.
Actually, it would be quite rare for equation (11) to yield exactly ±90° in the first place, and if it does, then
on the next Flight Simulator gauge update cycle (~56 ms later), it’s very likely to no longer be ±90° and
everything is back to normal.
* In hundreds of aerobatic Loops recorded at the default 18 hz gauge update cycle rate, never has Flight
Simulator returned ±90.00° for A:ATTITUDE INDICATOR PITCH DEGREES. Naturally in a Loop, the
pitch A:Vars returned by Flight Simulator approach ±90°, within tenths or, at best and very infrequently,
within hundredths of a degree, but never precisely equal ±90.00°.
On the other hand, ROLL and HDG Euler angles calculated from equations (4) and (5) or (12) and (13) are
stable at TILT values extremely close (but not equal) to ±90°, without applying the singularity mitigation
formulas. For example, TILT = 89.99999999999° or 90.00000000001° both yield stable and accurate
values of ROLL and HDG from equations (4) and (5) or (12) and (13) without applying the singularity
solutions.
The conclusion is that Flight Simulator may never return a Pitch value that causes a gimbal lock problem
with the G.E. rotations, even considering the compound rotation operation. This is why it’s probably
acceptable to ignore the singularity issue in this application.
Lastly, the Google Earth Tilt range convention is addressed by adding 90° to results of equation (11):
Unlike Flight Simulator Pitch, Google Earth Tilt ranges from 0° to +180°, with no negative values. In
Google Earth, a Tilt of 0° looks down, 90° is in the x-y plane, and +180° is in the ‘sky’. In level flight, Flight
Simulator returns a Pitch of near 0°, but to Google Earth, 0° Tilt points the camera down. Consequently,
to achieve the pilot’s perspective of looking forward rather than down, 90° should be added to TILT from
equation (11).
Equations 12, 13, and 14 are used for the Google Earth view as Flight Simulator is running.
7
Six Degrees of Freedom
Flight Simulator and Google Earth camera views require definition of all six degrees of freedom (DOF):
three rotational DOF plus three translational DOF. Rotations and compound rotations have been
discussed up to this point.
The remaining DOF are translational: Longitude, Latitude, and Altitude. The translational values are
simply the Flight Simulator variables A:PLANE LATITUDE, A:PLANE LONGITUDE, and A:PLANE ALTITUDE.
<Camera>
<longitude>-155.056566267712</longitude>
<latitude>19.7143216472382</latitude>
<altitude>1124.20057499758</altitude>
<heading>10.5365345501778</heading>
<tilt>100.656068754235</tilt>
<roll>4.53358066926012</roll>
<altitudeMode>absolute</altitudeMode>
</Camera>
where heading, tilt, and roll are the compound rotation Euler angles previously discussed.
The Google Earth kml schema assumes ‘degrees’ units for longitude, latitude, heading, tilt and roll and
‘meters’ units for altitude.
Absolute altitude mode means that the altitude value is measured relative to mean sea level, which is the
same reference datum used by A:PLANE ALTITUDE.
Equations in this paper were evaluated using Microsoft Excel. Spreadsheets such as Excel reverse the
order of arguments in the atan2 function compared to JavaScript. The conventional order, atan2(x, y) is
written as atan2(y, x) in Excel and in this paper.
Consequently, to use the atan2 expressions found here in JavaScript and potentially other languages,
please reverse the order of the arguments.
8
Quaternion Rotation Example
Compound Rotation
Quaternion Quaternion
r0 -0.84625 s0 0.93923
r1 0.15919 s1 -0.29614
r2 0.23749 s2 0.16561
r3 -0.44957 s3 0.05222
QUATERNION MULTIPLICATION
t=r*s
t0 -0.76354
t1 0.31326
t2 -0.04192
t3 -0.56313
9
Euler (Tait-Bryan) Rotation Example
Compound Rotation
1 0 0 1 0 0
R x (u) = 0 0.50000 -0.86603 R x (u) = 0 1.00000 0.00000
0 0.86603 0.50000 0 0.00000 1.00000
GE Tilt 90°
Radians Degrees Degrees Eqn.
ROLL 1.16090 66.51° (12)
TILT -0.42995 -24.63° 65.37° (11), (14)
HDG -0.49422 -28.32° (13)
10
Excel Formulas: QUATERNION
A B C D E F G H
1 AIRCRAFT ATTITUDE CAMERA SWIVEL
2
3 Degrees Radians Degrees Radians
4 Bank u = 60° =RADIANS(C4) Roll u = 0° =RADIANS(G4)
5 Pitch v = -15° =RADIANS(C5) Tilt v = 20° =RADIANS(G5)
6 Hdg w = 330° =RADIANS(C6) Yaw w = -35° =RADIANS(G6)
7
8 Hdg Pitch Bank Yaw Tilt Roll
9 cos(τ /2): =COS(D6/2) =COS(D5/2) =COS(D4/2) =COS(H6/2) =COS(H5/2) =COS(H4/2)
10 sin(τ /2): =SIN(D6/2) =SIN(D5/2) =SIN(D4/2) =SIN(H6/2) =SIN(H5/2) =SIN(H4/2)
11
12 Quaternion Quaternion
13 r0 =B9*C9*D9+B10*C10*D10 s0 =F9*G9*H9+F10*G10*H10
14 r1 =B10*C9*D9-B9*C10*D10 s1 =F10*G9*H9-F9*G10*H10
15 r2 =B9*C10*D9+B10*C9*D10 s2 =F9*G10*H9+F10*G9*H10
16 r3 =B9*C9*D10-B10*C10*D9 s3 =F9*G9*H10-F10*G10*H9
17
18 QUATERNION MULTIPLICATION
19 t=r*s
20 t 0 =C13*G13-C14*G14-C15*G15-C16*G16
21 t 1 =C13*G14+C14*G13-C15*G16+C16*G15
22 t 2 =C13*G15+C14*G16+C15*G13-C16*G14
23 t 3 =C13*G16-C14*G15+C15*G14+C16*G13
24
25 EULER (TAIT-BRYAN) ANGLES FOR GOOGLE EARTH
26
27 GE Tilt 90°
28 Radians Degrees Degrees Eqn.
29 ROLL =ATAN2(E20^2+E21^2-E22^2-E23^2,2*(E20*E23+E21*E22))
30 TILT =-(ASIN(2*(E20*E22-E21*E23)))
31 HDG =(ATAN2(E20^2-E21^2-E22^2+E23^2, 2*(E20*E21+E22*E23)))
24
11
References:
There are scores of articles and forum threads related to 3D rotation on the web, but for the Flight
Simulator – Google Earth application, the most succinct and relevant that I found are listed below.
Rose, David
http://danceswithcode.net/engineeringnotes/index.html
Slabaugh, Gregory
http://staff.city.ac.uk/~sbbh653/publications/euler.pdf
CH Robotics, Inc.
http://www.chrobotics.com/library
Baker, Martin
http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm
McLellan, James
http://www.mclellansys.com/article_2.htm
12