Skip to main content

quat

Class

Quaternion type for representing rotations in 3D space.

Index

Example

const identity = new quat(0, 0, 0, 1);
console.log('Identity quat:', identity.x, identity.y, identity.z, identity.w);

const rotation = quat.fromEulerAngles(0, Math.PI / 2, 0);
console.log('90-deg Y rotation:', rotation.x, rotation.y, rotation.z, rotation.w);

const euler = rotation.toEulerAngles();
console.log('Back to Euler:', euler.x, euler.y, euler.z);

Constructors

Constructor

new quat(w, x, y, z): quat

Constructs a quaternion with the given w, x, y, z components.

Parameters

ParameterType
wnumber
xnumber
ynumber
znumber

Returns

quat

Properties

w

w: number

The w (scalar) component of the quaternion.


x

x: number

The x component of the quaternion.


y

y: number

The y component of the quaternion.


z

z: number

The z component of the quaternion.

Methods

angleAxis()

static angleAxis(angle, axis): quat

Creates a quaternion from an angle in radians and a rotation axis.

Parameters

ParameterType
anglenumber
axisvec3

Returns

quat


angleBetween()

static angleBetween(a, b): number

Returns the angle in radians between two quaternions.

Parameters

ParameterType
aquat
bquat

Returns

number


dot()

dot(quat): number

Computes the dot product with another quaternion.

Parameters

ParameterType
quatquat

Returns

number


equal()

equal(quat): boolean

Returns true if this quaternion equals another quaternion.

Parameters

ParameterType
quatquat

Returns

boolean


fromEulerAngles()

static fromEulerAngles(x, y, z): quat

Creates a quaternion from Euler angles given as separate x, y, z values.

Parameters

ParameterType
xnumber
ynumber
znumber

Returns

quat


fromEulerVec()

static fromEulerVec(eulerAngles): quat

Creates a quaternion from a vec3 of Euler angles.

Parameters

ParameterType
eulerAnglesvec3

Returns

quat


fromRotationMat()

static fromRotationMat(rotationMat): quat

Creates a quaternion from a 3x3 rotation matrix.

Parameters

ParameterType
rotationMatmat3

Returns

quat


fromRotationMat4()

static fromRotationMat4(rotationMat4): quat

Creates a quaternion from a 4x4 rotation matrix.

Parameters

ParameterType
rotationMat4mat4

Returns

quat


getAngle()

getAngle(): number

Returns the rotation angle in radians represented by this quaternion.

Returns

number


getAxis()

getAxis(): vec3

Returns the rotation axis as a vec3.

Returns

vec3


invert()

invert(): quat

Returns the inverse of this quaternion.

Returns

quat


lerp()

static lerp(a, b, time): quat

Linearly interpolates between two quaternions by factor t.

Parameters

ParameterType
aquat
bquat
timenumber

Returns

quat


lookAt()

static lookAt(forward, up): quat

Creates a quaternion representing a rotation that looks from one point toward another.

Parameters

ParameterType
forwardvec3
upvec3

Returns

quat


multiply()

multiply(quat): quat

Multiplies this quaternion by another, combining rotations.

Parameters

ParameterType
quatquat

Returns

quat


multiplyVec3()

multiplyVec3(vec3): vec3

Rotates a vec3 by this quaternion.

Parameters

ParameterType
vec3vec3

Returns

vec3


normalize()

normalize(): void

Returns a normalized copy of this quaternion.

Returns

void


quatIdentity()

static quatIdentity(): quat

Returns the identity quaternion representing no rotation.

Returns

quat


rotationFromTo()

static rotationFromTo(from, to): quat

Creates a quaternion representing the rotation from one vector to another.

Parameters

ParameterType
fromvec3
tovec3

Returns

quat


slerp()

static slerp(a, b, time): quat

Spherically interpolates between two quaternions by factor t.

Parameters

ParameterType
aquat
bquat
timenumber

Returns

quat


toEulerAngles()

toEulerAngles(): vec3

Converts this quaternion to Euler angles in radians.

Returns

vec3


toString()

toString(): string

Returns a string representation of this quaternion.

Returns

string