Class Quaternion



A Quaternion consists of four floating-point values that represent a rotation amount. Their primary advantage is that they can calculate smooth rotations with Spherical Linear intERPolation (SLERP). You will need to convert Quaternions into rotation matrices before you can actually rotate a point.

Member Functions

Member Variables


Quaternion(void)

The default constructor, creates a Quaternion with w, x, y and z set to 0.


Quaternion(const Quaternion &source)

Copy constructor, creates a Quaternion that's a copy of source.


Quaternion conjugate(void)

Returns this Quaternion's conjugate, where x, y and z are all multiplied by -1.


Quaternion difference(const Quaternion &quatTwo)

The return value is the quaternion that will rotate this quaternion to quatTwo.


MHFloat distance(Quaternion quatTwo) const

Returns the rotational distance to quatTwo in degrees. quatTwo must be normalized.


void fromEuler(const EulerAngle &eulerAngles)

Sets this Quaternion to the rotation given by eulerAngles, which should be in degrees.


void fromEuler(MHFloat pitch, MHFloat yaw, MHFloat roll)

Sets this Quaternion to the rotation given by yaw, pitch, and roll, which should be in degrees.


Matrix3x3 getRotationMatrix(void) const

Convert this Quaternion into a rotation matrix that we can use to rotate 3D points.


Quaternion inverse(void)

Returns this quaternion's inverse. It's calculated by taking the Quaternion's conjugate and dividing by the normal. Throws an exception if the normal is zero.


Quaternion multiply(const Quaternion &second) const

Multiplies this Quaternion by second and returns the result. The effect is to rotate this Quaternion by second. This function is non-commutative, so the order of multiplications matter.


MHFloat magnitude(void) const

Returns the Quaternion's magnitude, which is the square root of the normal. We can use the magnitude to check if the Quaternion needs to be normalized.


MHFloat normal(void) const

Returns the Quaternion's normal, equal to w^2 + x^2 + y^2 + z^2.


Quaternion normalize(void)

Returns the normalized version of this quaternion. After many calculations, Quaternions may eventually need to be normalized due to rounding errors on floating point numbers. You can test if the Quaternion needs normalizing if the magnitude is beyond some threshold away from 1.


Quaternion slerp(const Quaternion &quatTwo, MHFloat amount, SlerpMethod method)

Performs Spherical Linear intERPolation (SLERP) between this quaternion and quatTwo. Amount should be a value between zero (returns this quaternion) and 1 (returns quatTwo). If amount were 0.5, slerp would return a Quaternion that's midway between them.

method specifies the type of rotation to produce. It can take one of the following values from the SlerpMethod enumeration:

    Q_SHORT              We'll use the shortest path. Rotation will be <= 180 degrees.
    Q_LONG               We'll use the longest path. Rotation will be >= 180 degrees.
    Q_CLOCKWISE          The rotation will be clockwise when viewed from above.
    Q_COUNTER_CLOCKWISE  The rotation will be counter-clockwise when viewed from above.
    Q_USER               The quaternions are interpolated exactly as given.


MHFloat w

MHFloat x

MHFloat y

MHFloat z

These are the four values making up the Quaternion. They're publicly exposed for convenience.











Back to Contents