API reference
API reference
Select your platform
No SDKs available
No versions available

Quaternion Class

Modifiers: open
Quaternion class for representing 3D rotations.
This class provides methods for creating quaternions from various sources, such as pitch, yaw, and roll angles, rotation matrices, and random values. Provides operator overloading for unary minus and multiplication with other quaternions and vectors. Example Usage:
Creating a quaternion from pitch, yaw, and roll: ---
val quaternion = Quaternion(pitch = 30f, yaw = 45f, roll = 60f) println(quaternion) // Outputs the quaternion representation
Multiplying two quaternions: ---
val q1 = Quaternion(1f, 0f, 0f, 0f) val q2 = Quaternion(0f, 1f, 0f, 0f) val result = q1 * q2 println(result) // Outputs the resulting quaternion
Converting a quaternion to Euler angles: ---
val eulerAngles = quaternion.toEuler() println(eulerAngles) // Outputs the Euler angles (pitch, yaw, roll)
Normalizing a quaternion: ---
val normalizedQuaternion = quaternion.normalize() println(normalizedQuaternion) // Outputs the normalized quaternion

Signature

open class Quaternion(var w: Float = 1.0f, var x: Float = 0.0f, var y: Float = 0.0f, var z: Float = 0.0f)

Constructors

Quaternion ( pitch , yaw , roll )
Constructor for creating a quaternion from pitch, yaw, and roll angles.
Signature
constructor(pitch: Float, yaw: Float, roll: Float)
Parameters
pitch: Float
The pitch angle in degrees.
yaw: Float
The yaw angle in degrees.
roll: Float
The roll angle in degrees.
Returns
Quaternion ( w , x , y , z )
Signature
constructor(w: Float = 1.0f, x: Float = 0.0f, y: Float = 0.0f, z: Float = 0.0f)
Parameters
w: Float
w component of the quaternion.
x: Float
x component of the quaternion.
y: Float
y component of the quaternion.
z: Float
z component of the quaternion.
Returns

Properties

w : Float
[Get][Set]
w component of the quaternion.
Signature
open var w: Float
x : Float
[Get][Set]
x component of the quaternion.
Signature
open var x: Float
y : Float
[Get][Set]
y component of the quaternion.
Signature
open var y: Float
z : Float
[Get][Set]
z component of the quaternion.
Signature
open var z: Float

Functions

component1 ()
Signature
operator fun component1(): Float
Returns
Float
component2 ()
Signature
operator fun component2(): Float
Returns
Float
component3 ()
Signature
operator fun component3(): Float
Returns
Float
component4 ()
Signature
operator fun component4(): Float
Returns
Float
conjugate ()
Calculates the conjugate of the quaternion.
Signature
fun conjugate(): Quaternion
Returns
The conjugate of the quaternion.
copy ()
Returns a copy of this Quaternion.
Signature
fun copy(): Quaternion
Returns
A copy of this Quaternion.
dot ( other )
Calculates the dot product of two quaternions.
Signature
fun dot(other: Quaternion): Float
Parameters
other: Quaternion
The other quaternion to calculate the dot product with.
Returns
Float
The dot product of the two quaternions.
equals ( other )
Signature
open operator override fun equals(other: Any?): Boolean
Parameters
other: Any?
Returns
Boolean
hashCode ()
Signature
open override fun hashCode(): Int
Returns
Int
inverse ()
Calculates the inverse of the quaternion.
Signature
fun inverse(): Quaternion
Returns
The inverse of the quaternion.
nlerp ( dest , ratio )
Performs normalized linear interpolation between the current quaternion and the destination quaternion.
Signature
fun nlerp(dest: Quaternion, ratio: Float): Quaternion
Parameters
dest: Quaternion
The destination quaternion to interpolate towards.
ratio: Float
The interpolation ratio, where 0 means no interpolation (i.e., the current quaternion) and 1 means full interpolation (i.e., the destination quaternion).
Returns
A new quaternion that is the result of interpolating between the current quaternion and the destination quaternion by the given ratio.
norm ()
Calculates the squared magnitude (length) of the quaternion.
Signature
fun norm(): Float
Returns
Float
The squared magnitude of the quaternion.
normalize ()
Normalizes the quaternion to have a magnitude of 1.
Signature
fun normalize(): Quaternion
Returns
The normalized quaternion.
removePitchAndRoll ()
Removes the pitch and roll components from the quaternion, leaving only the yaw component.
Signature
fun removePitchAndRoll(): Quaternion
Returns
A new quaternion with the pitch and roll components removed.
slerp ( dest , ratio )
Performs spherical linear interpolation between the current quaternion and the destination quaternion.
Signature
fun slerp(dest: Quaternion, ratio: Float): Quaternion
Parameters
dest: Quaternion
The destination quaternion to interpolate towards.
ratio: Float
The interpolation ratio, where 0 means no interpolation (i.e., the current quaternion) and 1 means full interpolation (i.e., the destination quaternion).
Returns
A new quaternion that is the result of interpolating between the current quaternion and the destination quaternion by the given ratio.
times ( q )
Multiplies this quaternion with another quaternion.
Signature
inline operator fun times(q: Quaternion): Quaternion
Parameters
The other quaternion to multiply with.
Returns
The resulting quaternion.
times ( v )
Multiplies this quaternion with a vector.
Signature
inline operator fun times(v: Vector3): Vector3
Parameters
The vector to multiply with.
Returns
The resulting vector.
toEuler ()
Converts the quaternion to Euler angles (pitch, yaw, roll).
Signature
fun toEuler(): Vector3
Returns
A 3D vector representing the Euler angles.
toRotationMatrix44 ()
Converts the quaternion to a 4x4 rotation matrix.
Signature
fun toRotationMatrix44(): Matrix44
Returns
A 4x4 rotation matrix representing the quaternion.
toString ()
Signature
open override fun toString(): String
Returns
String
toVector4 ()
Converts the quaternion to a 4D vector.
Signature
fun toVector4(): Vector4
Returns
A 4D vector representing the quaternion.
unaryMinus ()
Negates the quaternion.
Signature
inline operator fun unaryMinus(): Quaternion
Returns

Companion Object

Companion Object Functions

fromRotationMatrix ( matrix )
Creates a quaternion from a rotation matrix.
Signature
fun fromRotationMatrix(matrix: Array<FloatArray>): Quaternion
Parameters
matrix: Array
The rotation matrix to create the quaternion from.
Returns
The resulting quaternion.
getRandomQuat ()
Formula using "Choosing a Point from the Surface of a Sphere" authored by George Marsaglia Generates uniform random quaternion.
Signature
fun getRandomQuat(): Quaternion
Returns
A random quaternion.
lookRotation ( forward , lookUp )
Creates a quaternion that represents a rotation from the forward vector to the look-up vector.
Signature
fun lookRotation(forward: Vector3, lookUp: Vector3 = Vector3.Up): Quaternion
Parameters
forward: Vector3
The forward vector.
lookUp: Vector3
The look-up vector. Defaults to Vector3.Up.
Returns
A quaternion representing the rotation from the forward vector to the look-up vector.
lookRotationAroundY ( forward )
Creates a quaternion that represents a rotation around the Y-axis from the forward vector to the look-up vector.
Signature
fun lookRotationAroundY(forward: Vector3): Quaternion
Parameters
forward: Vector3
The forward vector.
Returns
A quaternion representing the rotation around the Y-axis from the forward vector to the look-up vector.
Did you find this page helpful?