Quaternion

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

Represents a quaternion, a mathematical concept used to represent rotations in 3D space. Quaternions are a more compact and efficient way to represent rotations compared to Euler angles.

Parameters

x

The x-component of the quaternion. Default is 0.

y

The y-component of the quaternion. Default is 0.

z

The z-component of the quaternion. Default is 0.

w

The w-component of the quaternion. Default is 0.

Constructors

Link copied to clipboard
constructor(v: FloatArray)

Creates a Quaternion from a FloatArray.

constructor(o: Quaternion)

Creates a copy of the provided Quaternion.

constructor(x: Float = 0.0f, y: Float = 0.0f, z: Float = 0.0f, w: Float = 0.0f)

Creates a Quaternion with the specified components.

Properties

Link copied to clipboard

Checks if the quaternion represents an identity rotation.

Link copied to clipboard

Checks if the quaternion is valid (non-zero).

Link copied to clipboard
var w: Float

The w-component of the quaternion.

Link copied to clipboard
var x: Float

The x-component of the quaternion.

Link copied to clipboard
var y: Float

The y-component of the quaternion.

Link copied to clipboard
var z: Float

The z-component of the quaternion.

Functions

Link copied to clipboard

Calculates the shortest angle between two quaternions in radians.

Link copied to clipboard

Calculates the dot product of this quaternion with another quaternion.

Link copied to clipboard
fun fromEuler(yaw: Float, pitch: Float, roll: Float)

Sets the quaternion based on Euler angles (Yaw, Pitch, Roll) in radians. Converts Euler angles to quaternion representation.

Link copied to clipboard

Creates a quaternion from a 4x4 rotation matrix.

Link copied to clipboard
fun fromRotationVector(angleRad: Float, x: Float, y: Float, z: Float): Quaternion

Creates a quaternion from a rotation vector and an angle.

Link copied to clipboard

Gets the quaternion's components and stores them in the provided array.

Link copied to clipboard
fun identity()

Sets the quaternion to represent the identity rotation (no rotation).

Link copied to clipboard

Calculates the inverse of the quaternion and stores it in the provided quaternion.

Calculates the inverse of the provided quaternion and stores it in the provided quaternion.

Link copied to clipboard
fun length(): Float

Calculates the length (magnitude) of the quaternion.

Link copied to clipboard

Multiplies this quaternion with another quaternion.

Multiplies two quaternions and stores the result in the provided quaternion.

Link copied to clipboard
fun normalize()

Normalizes this quaternion.

Normalizes the provided quaternion and stores the result in the provided quaternion.

Link copied to clipboard
fun rotateVector(v3: FloatArray, out: FloatArray, offset: Int = 0)
fun rotateVector(x: Float, y: Float, z: Float, out: FloatArray, offset: Int = 0)

Rotates a 3D vector in eye position using this quaternion and stores the result in the provided array.

Link copied to clipboard

Sets the quaternion's components to match those of the provided quaternion.

fun setValues(x: Float, y: Float, z: Float, w: Float)

Sets the quaternion's components to the provided values.

Link copied to clipboard
fun Slerp(q1: Quaternion, q2: Quaternion, alpha: Float, res: Quaternion)

Performs spherical linear interpolation (SLERP) between two quaternions.

Link copied to clipboard

Swaps the rotation direction of the quaternion by negating the w-component.

Link copied to clipboard

Convert to glasses quaternion to Yaw, Pitch, Roll in world. Note that glasses coordinates are 'right', 'up', 'back', while the world is defined by 'east', 'north', 'up'.

Link copied to clipboard
fun toMat4(res: FloatArray)

Converts the quaternion to a 4x4 rotation matrix and stores it in the provided array.

Link copied to clipboard

Converts the quaternion to a rotation vector x, y, z, angle.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun zero()

Sets all components of the quaternion to zero.