Cogito, ergo sum
Difference between revisions of "Quaternion"
(Created page with 'wp:Quaternions are a mathematical construct that could be seen as an extension of the wp:complex numbers with three different imaginary units or as a combination of a s…') 
(No difference)

Revision as of 11:24, 21 September 2013
Quaternions are a mathematical construct that could be seen as an extension of the complex numbers with three different imaginary units or as a combination of a scalar value and a threedimensional vector. A special subset of the quaternions, the unit quaternions, can be used to represent rotations in 3D space.
Definition
Generally a quaternion is a fourdimensional value that is written as either x = x_{0}+ x_{1}i + x_{2}j + x_{3}k or as x = (x_{0}, x_{1}, x_{2}, x_{3}), where x_{0} is called the real or scalar part Re(x) and (x_{1}, x_{2}, x_{3}) is the imaginary or vector part Im(x). If the vector part is zero, the quaternion is called real, while a quaternion with a zero scalar part is called a pure imaginary quaternion. Real quaternions (x_{0}, 0, 0, 0) could also be written as x_{0}+ 0i + 0j + 0k = x_{0}.
Addition, subtraction and negation of quaternions is defined the same way as for vectors:
 x + y = y + x = (x_{0}+ y_{0}, x_{1}+ y_{1}, x_{2}+ y_{2}, x_{3}+ y_{3})
 x  y = y + x = (x_{0} y_{0}, x_{1} y_{1}, x_{2} y_{2}, x_{3} y_{3})
 x = (x_{0}, x_{1}, x_{2}, x_{3})
The same goes for multiplication with a scalar value and scalar multiplication of two quaternions:
 a · x = x · a = (a · x_{0}, a · x_{1}, a · x_{2}, a · x_{3})
 x · y = y · x = x_{0}· y_{0} + x_{1}· y_{1} + x_{2}· y_{2} + x_{3}· y_{3}
Like complex numbers, quaternions define the conjugate quaternion x or x^{*}:
 x = (x_{0}, x_{1}, x_{2}, x_{3})
A quite unique operation of quaternions is the Hamilton product, i.e. the multiplication of two quaternions that yields a quaternion again. This one is special, because unlike addition or scalar product, it is not commutative.
 x y = (x_{0}· y_{0}  x_{1}· y_{1}  x_{2}· y_{2}  x_{3}· y_{3}, x_{0}· y_{1} + x_{1}· y_{0} + x_{2}· y_{3}  x_{3}· y_{2}, x_{0}· y_{2}  x_{1}· y_{3} + x_{2}· y_{0} + x_{3}· y_{1}, x_{0}· y_{3} + x_{1}· y_{2}  x_{2}· y_{1} + x_{3}· y_{0})
Now that looks vile. But if you express the quaternions as a combination of a scalar and a vector part, i.e. x = (x_{0}, x_{V}), you can express quaternion multiplication using the 3D vector scalar and cross products:
 x y = (x_{0}, x_{V})(y_{0}, y_{V}) = (x_{0}y_{0} x_{V}· y_{V}, x_{0}y_{V} + x_{V}y_{0}+ x_{V}⨯ y_{V})
This representation also easily shows that x y ≠ y x, because x_{V}⨯ y_{V} = (y_{V}⨯ x_{V}).
A quaternion's norm x^{2} is defined as:
 x^{2} = x x = x x = x · x = x_{0}^{2}+ x_{1}^{2}+ x_{2}^{2}+ x_{3}^{2}
The value x is called the length or absolute value of the quaternion. Its square is equal to the scalar product of the quaternion with itself or the Hamilton product with its conjugate quaternion. Multiplying a quaternion by its conjugate results in the vector part becoming zero, which is why the resulting quaternion in the above equation can be considered a scalar number. (Remember that quaternions are hypercomplex numbers, not euclidean vectors. Real numbers are valid quaternion values with the vector part being zero.)
A quaternion's multiplicative inverse, the reciprocal, x^{1} is defined as the value that satisfies the equation x x^{1} = 1:
 x^{1} = x / (x x) = x / x^{2}
Note that the fractional notation is avoided if the denominator is not a real number because the notation itself is ambiguous since it does not specify if the denominator's reciprocal must be multiplied to the left or to the right of the numerator and quaternion multiplication is not commutative.
Quaternion rotation
The basis for expression rotations using quaternions are unit quaternions, i.e. quaternions with a length of 1, and pure imaginary quaternions, i.e. quaternions of the form (0, x_{1}, x_{2}, x_{3}). Pure imaginary quaternions are used to represent vectors that will be rotated around the origin in some way, while unit quaternions define the rotation axis and angle. To apply a rotation defined by quaternion r to a vector defined by quaternion v, all you need to do is calculate the quaternion product r v r^{1}.
A property of quaternion multiplication is that the length of the resulting quaternion is the same as the product of the lengths of the two original quaternions. Consequently the product of two unit quaternions will be another unit quaternions. If those two unit quaternions represent 3D rotations, the resulting unit quaternion will represent the combination of these two rotations in the reverse order. The product r_{1} r_{2} represents a rotation achieved by first applying r_{2}, then r_{1}.
The vector to rotate can be converted to a quaternion simply by using the vector as the quaternion's vector part and zero as the scalar part. A rotation quaternion is calculated from a rotation axis and an angle. Given the rotation axis as unit vector u and the rotation angle α between 0 and 2π, the rotation quaternion r is calculated as r = (cos α/2, u sin α/2).
UnrealScript functions
The Object^{(U2, U2XMP, UE2Runtime, UT2003, UT2004, UDK, UT3)} class provides several functions and (in UE3) operators for working with quaternions. It defines the struct Quat with the components W, X, Y and Z as data type for a quaternion value. Unreal Engine 3 also defines the binary operators +
and 
, and like with every struct you can compare Quat values for equality and inequality with the ==
and !=
operators.
The Quat struct type is subject to data reduction during replication. The engine assumes that you replicate only unit quaternions and only replicates the X, Y and Z components. The W component will be calculated on the remote side to get a unit quaternion again.
Quaternion rotation is used in rigid body physics calculations both in Unreal Engine 2 (PHYS_Karma) and Unreal Engine 3 (PHYS_RigidBody). The rigid body physics state of an actor is composed of a vector for the location, a quaternion for the rotation and two vectors for linear and angular velocity.
QuatProduct(Quat, Quat)
Calculates the Hamilton product of the two quaternions and returns the resulting quaternion value.
QuatDot(Quat, Quat)
Calculates the scalar product of the two quaternions and returns the resulting float value. This function is not available in Unreal Engine 2.
QuatInvert(Quat)
Returns the conjugate quaternion. Note that due to x^{1} = x / x^{2} the conjugate quaternion of a unit quaternion also is its reciprocal.
QuatRotateVector(Quat, Vector)
Rotates the specified vector by the given rotation quaternion and returns the resulting vector value. Note that the result will only be actually the rotated vector if the quaternion is a unit quaternion.
QuatFindBetween(Vector, Vector)
Calculates the quaternion that will rotate the first vector onto the second. It does so by calculating the rotation axis and angle between the vectors using the cross product and scalar product. Note that it shortcircuits if the cross product is zero. In that case, whether the vectors point in the same or opposite directions, it always returns the identity quaternion (1,0,0,0), even though opposite direction vectors would require a rotation by 180° around an arbitrary rotation axis perpendicular to the vectors.
QuatFromAxisAndAngle(Vector, float)
Creates a rotation quaternion from the specifies axis vector and rotation angle in radians. The vector will be normalized for this calculation but must not be zero.
QuatFromRotator(Rotator)
Calculates a rotation quaternion that represents the same rotation as the specified Rotator value.
QuatToRotator(Quat)
Calculates a Rotator value that represents the same rotation as the specified rotation quaternion. This basically applies the quaternion rotation to the X, Y and Z axis vectors and uses the OrthoRotation function on the resulting vectors to calculate the Rotator value.
QuatSlerp(Quat, Quat, float)
Performs spherical linear interpolation on the two rotation quaternions, using the float value as blend alpha. A value of 0.0 maps to the first quaternion, a value of 1.0 to the second, while 0.5 is halfway between them.
Declarations  Preprocessor • Classes • Interfaces • Cpptext • Constants • Enums • Structs • Variables (Metadata) • Replication block • Operators • Delegates • Functions • States • Defaultproperties (Subobjects) 

Types  bool • byte • float • int • name • string • Object • Class • Enums • Structs (Vector ⋅ Rotator ⋅ Quat ⋅ Color) • Static arrays • Dynamic arrays • Delegates • Typecasting 
Literals  Boolean • Float • Integer • Names • Objects (None ⋅ Self) • Vectors • Rotators • Strings 
Flow  GoTo • If • Assert • Return • Stop • Switch • While • Do...Until • For • ForEach • Break • Continue 
Specifiers  Super • Global • Static • Default • Const 
UnrealScript  Syntax • .UC • .UCI • .UPKG • Comments • #directives • Native 