AboutQuaternions

From X-Plane SDK
Jump to: navigation, search

NOTE: The contents of this tech note are no longer the most recent or authoritative. Please see MovingThePlane.

The Euler angles suffer gimbal lock
Contrarily quaternions rule the world
--Jonathan van Tujil

X-Plane's "master" rotation for the aircraft is a quaternion, 4 floats in sim/flightmodel/position/q.  (Note the lower-case; upper-case Q is different...I'll take the slap on the wrist for this horrible naming.)

But X-Plane draws the plane from phi, the, and psi, three Eulers, also found in the sim/flightmodel/position/ group.

The logic is roughly this:

on flight model computation time:
   if (!override_flightpos)
               modify 'q' quaternion based on current angular
            momentum, torques, etc.
               copy q to phi, the, and psi
on draw time:
   set up OGL matrix rotating by phi, the, psi
   draw the plane

So what this means is: if you override the flight model, you must specify the plane rotation via psi, the, and psi.

But if you release the flight model, you must write to 'q' to make the rotation permanent.

If your goal is to move the plane around, you may not need to overright the flight model at all; writes to the quaternion q and to the local position x, y, and z will permanently move the plane.  (Two warnings: writing to x, y and z only works in 7.30 and newer.  Also, writing to Y is broken in the 7.40 beta series.)

If your goal is to do your own flight model, you'll want to write to phi, the, and psi; writing to q won't be copied to phi, the, and psi when the flight model is off.

Anyway, hope this helps people who want to mess with the plane as a physical object.  A quaternion is four floats that represents a rotation around an arbitrary axis.  I have no idea how or why it works, it's one of those things that I use sample code for but haven't bothered to learn the mathematics for yet.  I have one or two quat routines borrowed from OGL samples I found; email me off list if you need some code for this.