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

Quaternion

Quaternion

open class Quaternion(var w: Float = 1.0f, var x: Float = 0.0f, var y: Float = 0.0f, var z: Float = 0.0f)
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

Constructors

NameSummary
Quaternion
constructor(pitch: Float, yaw: Float, roll: Float)

Constructor for creating a quaternion from pitch, yaw, and roll angles.

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

Types

NameSummary
Companion
object Companion

Properties

NameSummary
w
open var w: Float
x
open var x: Float
y
open var y: Float
z
open var z: Float

Functions

NameSummary
component1
operator fun component1(): Float
component2
operator fun component2(): Float
component3
operator fun component3(): Float
component4
operator fun component4(): Float
conjugate
fun conjugate(): Quaternion

Calculates the conjugate of the quaternion.
copy
fun copy(): Quaternion

Returns a copy of this Quaternion.
dot
fun dot(other: Quaternion): Float

Calculates the dot product of two quaternions.
equals
open operator override fun equals(other: Any?): Boolean
hashCode
open override fun hashCode(): Int
inverse
fun inverse(): Quaternion

Calculates the inverse of the quaternion.
nlerp
fun nlerp(dest: Quaternion, ratio: Float): Quaternion

Performs normalized linear interpolation between the current quaternion and the destination quaternion.
norm
fun norm(): Float

Calculates the squared magnitude (length) of the quaternion.
normalize
fun normalize(): Quaternion

Normalizes the quaternion to have a magnitude of 1.
removePitchAndRoll
fun removePitchAndRoll(): Quaternion

Removes the pitch and roll components from the quaternion, leaving only the yaw component.
slerp
fun slerp(dest: Quaternion, ratio: Float): Quaternion

Performs spherical linear interpolation between the current quaternion and the destination quaternion.
times
inline operator fun times(q: Quaternion): Quaternion

Multiplies this quaternion with another quaternion.



inline operator fun times(v: Vector3): Vector3

Multiplies this quaternion with a vector.
toEuler
fun toEuler(): Vector3

Converts the quaternion to Euler angles (pitch, yaw, roll).
toRotationMatrix44
fun toRotationMatrix44(): Matrix44

Converts the quaternion to a 4x4 rotation matrix.
toString
open override fun toString(): String
toVector4
fun toVector4(): Vector4

Converts the quaternion to a 4D vector.
unaryMinus
inline operator fun unaryMinus(): Quaternion

Negates the quaternion.

Companion

object Companion

Functions

NameSummary
fromRotationMatrix
fun fromRotationMatrix(matrix: Array<FloatArray>): Quaternion

Creates a quaternion from a rotation matrix.
getRandomQuat
fun getRandomQuat(): Quaternion

Formula using "Choosing a Point from the Surface of a Sphere" authored by George Marsaglia Generates uniform random quaternion.
lookRotation
fun lookRotation(forward: Vector3, lookUp: Vector3 = Vector3.Up): Quaternion

Creates a quaternion that represents a rotation from the forward vector to the look-up vector.
lookRotationAroundY
fun lookRotationAroundY(forward: Vector3): Quaternion

Creates a quaternion that represents a rotation around the Y-axis from the forward vector to the look-up vector.
Did you find this page helpful?
Thumbs up icon
Thumbs down icon