Difference between revisions of "Quaternion"

From Elite Wiki
(Fancier typography, noted cotangent simplification.)
(script example from Oolite -> scripting page)
Line 145: Line 145:
  
 
--------------------------------------------------------------------------------
 
--------------------------------------------------------------------------------
[[Category:Oolite]]
+
[[Category:Oolite scripting]]

Revision as of 12:24, 2 January 2011

Overview

A quaternion is a set of four values (W X Y Z) that are used in Oolite to specify a rotation in 3D space. To specify a particular rotation you need to think about the axis about which the rotation is made and the angle or amount by which the model is to be rotated.

For a given axis (x y z) and angle (α), the quaternion representing a rotation of a degrees around the axis from the origin (0,0,0) to (x,y,z) is:


W = cos (0.5 × α)

X = x × sin (0.5 × α)

Y = y × sin (0.5 × α)

Z = z × sin (0.5 × α)


So a rotation of 90 degrees about the z axis (0 0 1) would be:


W = cos 45 ° = 0.707…

X = 0 × sin 45 ° = 0

Y = 0 × sin 45 ° = 0

Z = 1 × sin 45 ° = 0.707…


Now because quaternions are normalised (adjusted so that W × W + X × X + Y × Y + Z × Z = 1) when Oolite reads them in, you can multiply each part of a quaternion by the same value and still have it represent the same angle. So this rotation can also be represented as W = 1, X = 0, Y = 0, Z = 1 (let's use [1 0 0 1] as shorthand).


Examples

90 ° turns about the z-axis

Oolite uses quaternions to specify rotations in some parts of shipdata.plist, most notably in the subentities part of an entry, like here in the relevant sub-entity entry for the Coriolis station:


Code:

<key>subentities</key> 
<array> 
   <string>arc-detail 0 0 0 1 0 0 0</string> 
   <string>arc-detail 0 0 0 1 0 0 1</string> 
   <string>arc-detail 0 0 0 0 0 0 1</string> 
   <string>arc-detail 0 0 0 1 0 0 -1</string> 
<array>


The last four numbers after the four 'arc-detail' lines are the W X Y and Z of quaternions representing rotations about the z-axis of 0 degrees, 90 degrees, 180 degrees, and 270 degrees (the first three numbers are the subentity's position relative to the station, in this case all are at the same place at the station's origin).

120 ° turns about the z-axis

Similarly, the shipdata of the Weeviloid 2 illustrates how to place sub-entities at 3 equilateral points:


Code:

<key>subentities</key>
<array>
   <string>weeviloid2-spine	0 0 0 1 0 0 0</string>
   <string>weeviloid2-spine	0 0 0 0.5 0 0 0.8660254</string>
   <string>weeviloid2-spine	0 0 0 0.5 0 0 -0.8660254</string>
<array>


Given that the first entry (0 0 0 1 0 0 0) will place the sub-entity at the exact place that the model is situated (0 degrees), the next two lines reproduce it at 120 degrees and 240 degrees.


22.5 ° turns about the z-axis

To display 16 sub-entities that join to make a ring, as done in the Ringpod and Torus shipdata, these are the relevant lines:


Code:

<key>subentities</key>
<array>
   <string>torus_pod  0 0 0 1 0 0 0</string>
   <string>torus_pod  0 0 0 0.9808 0.0 0.0 0.1951</string>
   <string>torus_pod  0 0 0 0.9239 0.0 0.0 0.3827</string>
   <string>torus_pod  0 0 0 0.8315 0.0 0.0 0.5556</string>
   <string>torus_pod  0 0 0 0.7071 0.0 0.0 0.7071</string>
   <string>torus_pod  0 0 0 0.5556 0.0 0.0 0.83110</string>
   <string>torus_pod  0 0 0 0.3827 0.0 0.0 0.9239</string>
   <string>torus_pod  0 0 0 0.1951 0.0 0.0 0.9808</string>
   <string>torus_pod  0 0 0 0 0 0 1</string>
   <string>torus_pod  0 0 0 -0.1951 0.0 0.0 0.9808</string>
   <string>torus_pod  0 0 0 -0.3827 0.0 0.0 0.9239</string>
   <string>torus_pod  0 0 0 -0.5556 0.0 0.0 0.83110</string>
   <string>torus_pod  0 0 0 -0.7071 0.0 0.0 0.7071</string>
   <string>torus_pod  0 0 0 -0.8315 0.0 0.0 0.5556</string>
   <string>torus_pod  0 0 0 -0.9239 0.0 0.0 0.3827</string>
   <string>torus_pod  0 0 0 -0.9808 0.0 0.0 0.1951</string>
<array>


Again the 0 ° sub-entity will appear with the 0 0 0 1 0 0 0, and the next 15 lines place it at 22.5 °, 45 °, 67.5 °, 90 °, 112.5 °, 135 °, 157.5 °, 180 ° (with 0 0 0 0 0 0 1), 202.5 °, 225 °, 247.5 °, 270 °, 292.5 °, 315 ° and 337.5 °.


Quaternion calculus

To perform one rotation, and then another, one needs to multiply two quaternions together.

Multiplying quaternions isn't commutative: Qa × Qb does not equal Qb × Qa

To multiply Q₁ (w₁ , x₁ , y₁ , z₁ ) by Q₂ (w₂, x₂, y₂, z₂):

W = w₁ × w₂ - x₁ × x₂ - y₁ × y₂ - z₁ × z₂

X = w₁ × x₂ + x₁ × w₂ + y₁ × z₂ - z₁ × y₂

Y = w₁ × y₂ + y₁ × w₂ + z₁ × x₂ - x₁ × z₂

Z = w₁ × z₂ + z₁ × w₂ + x₁ × y₂ - y₁ × x₂


To determine the quaternion for a rotation of α degrees/radians around an axis defined by a vector (x, y, z):

W = cos (0.5 × α)

X = x × sin (0.5 × α)

Y = y × sin (0.5 × α)

Z = z × sin (0.5 × α)

or:

W = 1 / tan (0.5 × α)

X = x

Y = y

Z = z

These are equivalent because of the trigonometric identity sin x/cos x = tan x, and the fact that quaternion normalization means that multiplying each component by the same value (in this case, 1 / sin (0.5 × α)) does not affect the resulting orientation.


See Also