Class Quaternion

Quaternions represent rotation around an axis, defined by quaternion components x, y, z and w. Quaternions are compact, do not suffer from gimbal lock and can be easily interpolated. Kanzi Engine uses quaternions internally to represent all rotations. Quaternions are based on complex numbers and are not easy to understand intuitively. You almost never access or modify the individual components. In most cases you take existing rotations and use them to construct new rotations (e.g. to smoothly interpolate between two rotations). You can use the Quaternion operator * to rotate one rotation by another, or to rotate a vector by a rotation.

Quaternion constructors

Functions which can be used to create new instances of Quaternion.

Synopsis

Methods
Quaternion()

Default constructor, initializes the quaternion components to identity, representing no rotation (x=0, y=0, z=0, w=1)

Quaternion()

Constructor, initializes the quaternion components to x, y, z and w

Quaternion()

Default constructor, initializes the quaternion components to identity, representing no rotation (x=0, y=0, z=0, w=1).

Example
local quaternion = Quaternion()
Quaternion(x, y, z, w)

Constructor, initializes the quaternion components to x, y, z and w.

Example
local quaternion = Quaternion(x, y, z, w)
Parameters
x (float)

x component of the quaternion.

y (float)

y component of the quaternion.

z (float)

z component of the quaternion.

w (float)

w component of the quaternion.

Quaternion methods

Methods of Quaternion class.

Synopsis

Methods
Quaternion.createFromVector()

Creates quaternion from axis and angle

Quaternion.createFromEulerAngles()

Creates a quaternion from Euler angles

Quaternion.createFromEulerAngles()

Creates a quaternion from Euler angles

Quaternion:normalized()

Get normalized copy of this quaternion

Quaternion:conjugated()

Get conjugated copy of this quaternion

Quaternion:getX()

Get the x component of the quaternion

Quaternion:getY()

Get the y component of the quaternion

Quaternion:getZ()

Get the z component of the quaternion

Quaternion:getW()

Get the w component of the quaternion

Quaternion:setX()

Sets the x component of the quaternion

Quaternion:setY()

Sets the y component of the quaternion

Quaternion:setZ()

Sets the z component of the quaternion

Quaternion:setW()

Sets the w component of the quaternion

Quaternion.createFromVector(axis, angleInRadians)

Creates quaternion from axis and angle. Use this to create a quaternion from axis of rotation and angle in radians.

Parameters
axis (Vector3)

Axis components in Vector3(x, y, z).

angleInRadians (float)

Angle in radians.

Return Values
(Quaternion)

Rotated quaternion.

Quaternion.createFromEulerAngles(anglesInRadians)

Creates a quaternion from Euler angles. Kanzi Engine represents Euler angles as Tait-Bryan angles where the rotations are applied in the order x, y and z.

Parameters
anglesInRadians (Vector3)

Euler angles in radians.

Return Values
(Quaternion)

Rotated quaternion.

Quaternion.createFromEulerAngles(rotationX, rotationY, rotationZ)

Creates a quaternion from Euler angles. Kanzi Engine represents Euler angles as Tait-Bryan angles where the rotations are applied in the order x, y and z.

Parameters
rotationX (float)

Rotation angle around the x axis in radians.

rotationY (float)

Rotation angle around the y axis in radians.

rotationZ (float)

Rotation angle around the z axis in radians.

Return Values
(Quaternion)

Rotated quaternion.

Quaternion:normalized()

Get normalized copy of this quaternion. Normalized quaternion is called an unit quaternion. Calculates the magnitude from the dot product of the quaternion components, and divides all the quaternion components with the magnitude. Use this when rotating or transforming a vector with a quaternion, and when generating a rotation or transformation matrix from quaternion. Using unnormalized quaternion in calculations can lead to floating point rounding errors. The errors that result from using an unnormalized quaternion in calculations are proportional to the square of the quaternion's magnitude.

Return Values
(Quaternion)

Normalized quaternion.

Quaternion:conjugated()

Get conjugated copy of this quaternion. Conjugation inverses the quaternion axis components x, y and z. Use this to get the inverse rotation of a quaternion.

Return Values
(Quaternion)

Conjugated quaternion.

Quaternion:getX()

Get the x component of the quaternion.

Return Values
(float)

The x component.

Quaternion:getY()

Get the y component of the quaternion.

Return Values
(float)

The y component.

Quaternion:getZ()

Get the z component of the quaternion.

Return Values
(float)

The z component.

Quaternion:getW()

Get the w component of the quaternion.

Return Values
(float)

The w component.

Quaternion:setX(x)

Sets the x component of the quaternion.

Parameters
x (float)

The new value of the x component.

Quaternion:setY(y)

Sets the y component of the quaternion.

Parameters
y (float)

The new value of the y component.

Quaternion:setZ(z)

Sets the z component of the quaternion.

Parameters
z (float)

The new value of the z component.

Quaternion:setW(w)

Sets the w component of the quaternion.

Parameters
w (float)

The new value of the w component.

Quaternion functions

Quaternion specific standalone functions, which are not a part of the Quaternion class.

Synopsis

Functions
componentWiseMultiply()

Multiplies quaternion components component-wise and returns the resulting quaternion

dotProduct()

Calculates the dot product of two quaternions

inverse()

Returns an inverse quaternion for a quaternion

rotateVector()

Applies quaternion rotation to a vector

quaternionToMatrix4x4()

Converts a quaternion rotation to a 4x4 rotation matrix

matrix4x4ToQuaternion()

Creates a quaternion from Matrix4x4

calculateEulerAngles()

Calculates Euler angles from a quaternion

quaternionSlerp()

Applies spherical linear interpolation (slerp) to two quaternions using the shortest path

quaternionSlerpLongerPath()

Applies spherical linear interpolation (slerp) to two quaternion using the longer path

componentWiseMultiply(q1, q2)

Multiplies quaternion components component-wise and returns the resulting quaternion.

Parameters
q1 (Quaternion)

The first quaternion.

q2 (Quaternion)

The second quaternion.

Return Values
(Quaternion)

The result quaternion.

dotProduct(q1, q2)

Calculates the dot product of two quaternions.

Parameters
q1 (Quaternion)

The first quaternion.

q2 (Quaternion)

The second quaternion.

Return Values
(float)

The dot product of the quaternions.

inverse(q)

Returns an inverse quaternion for a quaternion.

Parameters
q (Quaternion)

Quaternion to invert.

Return Values
(Quaternion)

Inverse quaternion.

rotateVector(quaternion, vector)

Applies quaternion rotation to a vector. Calculates q * v * q.conjugated(), for a quaternion q and a vector v.

Parameters
quaternion (Quaternion)

Quaternion rotation.

vector (Vector3)

Vector that you want to rotate.

Return Values
(Vector3)

The rotated vector.

quaternionToMatrix4x4(quaternion)

Converts a quaternion rotation to a 4x4 rotation matrix.

Parameters
quaternion (Quaternion)

Quaternion rotation.

Return Values
(Matrix4x4)

Matrix4x4 with rotation values extracted from quaternion.

matrix4x4ToQuaternion(matrix)

Creates a quaternion from Matrix4x4. Extracts and calculates the quaternion components from passed in Matrix4x4. Use this when you already have the rotation stored in a 4x4 matrix and want to extract the rotation angles to a quaternion.

Parameters
matrix (Matrix4x4)

Matrix4x4 from which to extract and calculate the quaternion components.

Return Values
(Quaternion)

Rotated and normalized quaternion.

calculateEulerAngles(quaternion)

Calculates Euler angles from a quaternion. Kanzi Engine represents Euler angles as Tait-Bryan angles where the rotations are applied in the order x, y, and z.

Return Values
(Vector3)

Euler angles in radians.

quaternionSlerp(q1, q2, interpolationTime)

Applies spherical linear interpolation (slerp) to two quaternions using the shortest path. Use this to evaluate interpolation between two rotations at given interpolation time. Takes the shortest path of interpolation between two degrees, which amounts to maximum of 180 degrees of rotation in any direction.

Parameters
q1 (Quaternion)

First quaternion.

q2 (Quaternion)

Second quaternion.

interpolationTime (float)

Interpolation parameter in range [0 .. 1].

Return Values
(Quaternion)

Interpolated rotated quaternion.

quaternionSlerpLongerPath(q1, q2, interpolationTime)

Applies spherical linear interpolation (slerp) to two quaternion using the longer path. Use this to evaluate interpolation between two rotations at given interpolation time. Takes the longer path to the result. Interpolates the whole angle difference between the two quaternion rotations. For example, from 0 to 270 rotates the whole 270 degrees.

Parameters
q1 (Quaternion)

First quaternion.

q2 (Quaternion)

Second quaternion.

interpolationTime (float)

Interpolation parameter in range [0 .. 1].

Return Values
(Quaternion)

Interpolated rotated quaternion.