Dev » Lua API » quaternion
← Back to all classes

quaternion

Math 4 Properties 11 Methods

quaternion

The quaternion class represents 3D orientations (X, Y, Z, W). It provides spherical linear interpolation (slerp), Euler angle conversions in degrees, vector basis transformations, and angle measurements.

Overview

Properties (4)

Property Type Summary
w number The W component of the quaternion.
x number The X component of the quaternion.
y number The Y component of the quaternion.
z number The Z component of the quaternion.

Methods (11)

Method Signature Returns Summary
quaternion:angleTo(other) number Calculates the rotational angle in degrees between two quaternions.
quaternion:basisXform(v) vector3 Rotates a 3D vector by this quaternion.
quaternion:dot(other) number Calculates the dot product with another quaternion.
quaternion.fromEuler(euler) quaternion Creates a quaternion from Euler angles in degrees.
quaternion:getEuler() vector3 Converts the quaternion into Euler angles in degrees.
quaternion.identity() quaternion Returns the identity quaternion.
quaternion:inverse() quaternion Returns the inverse rotation quaternion.
quaternion:length() number Calculates the length of the quaternion.
quaternion.new(x, y, z, w) quaternion Constructs a quaternion from raw components.
quaternion:normalized() quaternion Returns the normalized unit quaternion.
quaternion:slerp(to, weight) quaternion Spherical linear interpolation between two quaternions.

Detailed Reference

# w PROPERTY
quaternion.w
The W component of the quaternion.
Parameters
None
Returns
number

The W component.

Description

Reads or writes the W component.

# x PROPERTY
quaternion.x
The X component of the quaternion.
Parameters
None
Returns
number

The X component.

Description

Reads or writes the X component.

# y PROPERTY
quaternion.y
The Y component of the quaternion.
Parameters
None
Returns
number

The Y component.

Description

Reads or writes the Y component.

# z PROPERTY
quaternion.z
The Z component of the quaternion.
Parameters
None
Returns
number

The Z component.

Description

Reads or writes the Z component.

# angleTo METHOD
quaternion:angleTo(other)
Calculates the rotational angle in degrees between two quaternions.
Parameters
ParameterTypeDefaultDescription
otherquaternion

The target quaternion.

Returns
number

Angle in degrees between orientations.

Description

Returns the minimum rotation angle required to rotate from this to other, in degrees.

# basisXform METHOD
quaternion:basisXform(v)
Rotates a 3D vector by this quaternion.
Parameters
ParameterTypeDefaultDescription
vvector3

Vector to rotate.

Returns
vector3

Rotated 3D vector.

Description

Applies the quaternion orientation to a 3D vector.

# dot METHOD
quaternion:dot(other)
Calculates the dot product with another quaternion.
Parameters
ParameterTypeDefaultDescription
otherquaternion

Other quaternion.

Returns
number

Dot product.

Description

Calculates the 4D dot product between orientations.

# fromEuler METHOD
quaternion.fromEuler(euler)
Creates a quaternion from Euler angles in degrees.
Parameters
ParameterTypeDefaultDescription
eulervector3

Euler rotation angles in degrees.

Returns
quaternion

Resulting quaternion.

Description

Converts Euler rotation angles (in degrees) into a quaternion orientation.

# getEuler METHOD
quaternion:getEuler()
Converts the quaternion into Euler angles in degrees.
Parameters
None
Returns
vector3

Euler angles in degrees.

Description

Extracts Euler rotation angles (X, Y, Z in degrees) representing this orientation.

# identity METHOD
quaternion.identity()
Returns the identity quaternion.
Parameters
None
Returns
quaternion

Identity quaternion (0, 0, 0, 1).

Description

Returns the neutral identity orientation with no rotation.

# inverse METHOD
quaternion:inverse()
Returns the inverse rotation quaternion.
Parameters
None
Returns
quaternion

Inverted quaternion.

Description

Calculates the inverted rotation.

# length METHOD
quaternion:length()
Calculates the length of the quaternion.
Parameters
None
Returns
number

Magnitude of quaternion.

Description

Calculates sqrt(x^2 + y^2 + z^2 + w^2).

# new METHOD
quaternion.new(x, y, z, w)
Constructs a quaternion from raw components.
Parameters
ParameterTypeDefaultDescription
xnumber

X component.

ynumber

Y component.

znumber

Z component.

wnumber

W component.

Returns
quaternion

Newly constructed quaternion.

Description

Creates a quaternion with explicit components.

# normalized METHOD
quaternion:normalized()
Returns the normalized unit quaternion.
Parameters
None
Returns
quaternion

Unit quaternion.

Description

Normalizes the quaternion to length 1.0.

# slerp METHOD
quaternion:slerp(to, weight)
Spherical linear interpolation between two quaternions.
Parameters
ParameterTypeDefaultDescription
toquaternion

Target orientation.

weightnumber

Interpolation factor between 0.0 and 1.0.

Returns
quaternion

Interpolated orientation.

Description

Smoothly interpolates rotation between this and to by weight.

local mid = q1:slerp(q2, 0.5)