Skip to content

Documentation / base / Quaternion

Class: Quaternion

Defined in: libs/base/src/math/vector.ts:1802

Quaternion

Extends

Extended by

Indexable

[index: number]: number

Constructors

Constructor

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

Defined in: libs/base/src/math/vector.ts:1810

Creates a new Quaternion initialized with values.

Parameters

x

number

The x component.

y

number

The y component.

z

number

The z component.

w

number

The w component.

Returns

Quaternion

Overrides

VectorBase.constructor

Constructor

new Quaternion(elements): Quaternion

Defined in: libs/base/src/math/vector.ts:1815

Creates a new Quaternion initialized with values in an array.

Parameters

elements

number[]

Array that contains the x, y, z, w values.

Returns

Quaternion

Overrides

VectorBase.constructor

Constructor

new Quaternion(array): Quaternion

Defined in: libs/base/src/math/vector.ts:1820

Creates a new Quaternion initialized with values in a Float32Array.

Parameters

array

Float32Array

Float32Array object that contains the x, y, z, w values.

Returns

Quaternion

Overrides

VectorBase.constructor

Constructor

new Quaternion(buffer, offset): Quaternion

Defined in: libs/base/src/math/vector.ts:1826

Creates a new Quaternion placed on a given ArrayBuffer object.

Parameters

buffer

ArrayBuffer

The array buffer object.

offset

number

The byte offset of the buffer where the vector placed at.

Returns

Quaternion

Overrides

VectorBase.constructor

Constructor

new Quaternion(): Quaternion

Defined in: libs/base/src/math/vector.ts:1830

Creates a new identity Quaternion.

Returns

Quaternion

Overrides

VectorBase.constructor

Accessors

x

Get Signature

get x(): number

Defined in: libs/base/src/math/vector.ts:1876

Get the x component value.

Returns

number

Set Signature

set x(v): void

Defined in: libs/base/src/math/vector.ts:1879

Parameters
v

number

Returns

void


y

Get Signature

get y(): number

Defined in: libs/base/src/math/vector.ts:1883

Get the y component value.

Returns

number

Set Signature

set y(v): void

Defined in: libs/base/src/math/vector.ts:1886

Parameters
v

number

Returns

void


z

Get Signature

get z(): number

Defined in: libs/base/src/math/vector.ts:1890

Get the z component value.

Returns

number

Set Signature

set z(v): void

Defined in: libs/base/src/math/vector.ts:1893

Parameters
v

number

Returns

void


w

Get Signature

get w(): number

Defined in: libs/base/src/math/vector.ts:1897

Get the w component value.

Returns

number

Set Signature

set w(v): void

Defined in: libs/base/src/math/vector.ts:1900

Parameters
v

number

Returns

void


magnitude

Get Signature

get magnitude(): number

Defined in: libs/base/src/math/vector.ts:1940

Get the length of the quaternion.

Returns

number


magnitudeSq

Get Signature

get magnitudeSq(): number

Defined in: libs/base/src/math/vector.ts:1944

Get the squared length of the quaternion.

Returns

number

Methods

equalsTo()

equalsTo(other, epsl?): boolean

Defined in: libs/base/src/math/vector.ts:32

Check if all data is close enough to another

Parameters

other

Float32Array<ArrayBuffer>

The data to be compared with.

epsl?

number

Returns

boolean

true if close enough, otherwise false.

Inherited from

VectorBase.equalsTo


toString()

toString(): string

Defined in: libs/base/src/math/vector.ts:52

Convert this to string object.

Returns

string

Inherited from

VectorBase.toString


isNaN()

isNaN(): boolean

Defined in: libs/base/src/math/vector.ts:61

Check the data for the presence of NaN.

Returns

boolean

true if NaN is present, otherwise false.

Inherited from

VectorBase.isNaN


setRandom()

setRandom(minValue, maxValue): void

Defined in: libs/base/src/math/vector.ts:75

Generate random vector

Parameters

minValue

number

Minimum value of any component of the vector

maxValue

number

Maximum value of any component of the vector

Returns

void

Inherited from

VectorBase.setRandom


clone()

clone(): Quaternion

Defined in: libs/base/src/math/vector.ts:1872

Creates a new Quaternion initialized with values from this quaternion.

Returns

Quaternion

The new quaternion.


setXYZW()

setXYZW(x, y, z, w): Quaternion

Defined in: libs/base/src/math/vector.ts:1911

Set component values.

Parameters

x

number

The x component value.

y

number

The y component value.

z

number

The z component value.

w

number

The w component value.

Returns

Quaternion

self


scaleBy()

scaleBy(f): Quaternion

Defined in: libs/base/src/math/vector.ts:1923

Scale this quaternion by a scalar number.

Parameters

f

number

amount to scale this quaternion by.

Returns

Quaternion

self


setAndNormalize()

setAndNormalize(x, y, z, w): Quaternion

Defined in: libs/base/src/math/vector.ts:1935

Set component values and then normalize the quaternion.

Parameters

x

number

The x component value.

y

number

The y component value.

z

number

The z component value.

w

number

The w component value.

Returns

Quaternion

self


identity()

identity(): Quaternion

Defined in: libs/base/src/math/vector.ts:1948

Make this quaternion an identity quaternion

Returns

Quaternion


inplaceNormalize()

inplaceNormalize(): Quaternion

Defined in: libs/base/src/math/vector.ts:1956

Normalize this quaternion inplace.

Returns

Quaternion

self


inplaceConjugate()

inplaceConjugate(): Quaternion

Defined in: libs/base/src/math/vector.ts:1964

Calculates the conjugate of this quaternion inplace.

Returns

Quaternion

self


inplaceInverse()

inplaceInverse(): Quaternion

Defined in: libs/base/src/math/vector.ts:1972

Calculates the inverse of this quaternion inplace.

Returns

Quaternion

self


multiplyRight()

multiplyRight(other): Quaternion

Defined in: libs/base/src/math/vector.ts:1981

Multiply this quaternion by another quaternion at the right side inplace.

Parameters

other

Quaternion

The quaternion that to be multiplied by.

Returns

Quaternion

self


multiplyLeft()

multiplyLeft(other): Quaternion

Defined in: libs/base/src/math/vector.ts:1990

Multiply this quaternion by another quaternion at the left side inplace.

Parameters

other

Quaternion

The quaternion that to be multiplied by.

Returns

Quaternion

self


unitVectorToUnitVector()

unitVectorToUnitVector(from, to): Quaternion

Defined in: libs/base/src/math/vector.ts:2000

Make a quaternion used to rotate a unit vector to another inplace.

Parameters

from

Vector3

The unit vector to be rotated.

to

Vector3

The destination unit vector.

Returns

Quaternion

self


fromEulerAngle()

fromEulerAngle(x, y, z, order?): Quaternion

Defined in: libs/base/src/math/vector.ts:2012

Calculates the quaternion from an euler angle in specific order inplace.

Parameters

x

number

Angle to rotate around X axis in radians.

y

number

Angle to rotate around Y axis in radians.

z

number

Angle to rotate around Z axis in radians.

order?

EulerAngleOrder = 'ZYX'

Intrinsic order for conversion.

Returns

Quaternion

self


fromAxisAngle()

fromAxisAngle(axis, angle): Quaternion

Defined in: libs/base/src/math/vector.ts:2022

Calculates the quaternion from the given angle and rotation axis inplace.

Parameters

axis

Vector3

The rotation axis.

angle

number

The rotate angle.

Returns

Quaternion

self


toAxisAngle()

toAxisAngle(axis): number

Defined in: libs/base/src/math/vector.ts:2031

Calculates the rotation axis and angle for this quaternion

Parameters

axis

Vector3

A vector that receives the rotation axis.

Returns

number

  • The rotation angle

toEulerAngles()

toEulerAngles(angles?): Vector3

Defined in: libs/base/src/math/vector.ts:2046

Convert this rotation to euler angles in ZYX order

Parameters

angles?

Vector3

A vector that receives the euler angles. If not given, a new vector will be created.

Returns

Vector3

The vector that holds the euler angles.


fromRotationMatrix()

fromRotationMatrix(matrix): Quaternion

Defined in: libs/base/src/math/vector.ts:2063

Calculates the quaternion from a rotation matrix inplace.

Parameters

matrix

Matrix3x3 | Matrix4x4

The rotation matrix.

Returns

Quaternion

self


decomposeSwingTwist()

decomposeSwingTwist(axis, outSwing?, outTwist?): void

Defined in: libs/base/src/math/vector.ts:2085

Decompose the quaternion into swing and twist components around a given axis.

Parameters

axis

Vector3

The twist axis (must be normalized)

outSwing?

Quaternion

Output swing rotation

outTwist?

Quaternion

Output twist rotation

Returns

void

Remarks

Given a rotation Q and an axis A, decompose Q into:

  • Twist: rotation around A
  • Swing: rotation perpendicular to A Such that Q = Twist * Swing

Algorithm:

  1. Project Q's imaginary part onto axis to get twist component
  2. Normalize to get pure twist rotation
  3. Calculate swing = twist^-1 * Q

getTwistAngle()

getTwistAngle(axis): number

Defined in: libs/base/src/math/vector.ts:2133

Get the twist angle (in radians) around an axis.

Parameters

axis

Vector3

The twist axis (must be normalized)

Returns

number

Twist angle in radians, range [-PI, PI]


toMatrix3x3()

toMatrix3x3(matrix?): Matrix3x3

Defined in: libs/base/src/math/vector.ts:2158

Convert this quaternion to a 3x3 rotation matrix.

Parameters

matrix?

Matrix3x3

The output matrix, if not specified, a new matrix will be created.

Returns

Matrix3x3

The output matrix.


toMatrix4x4()

toMatrix4x4(matrix?): Matrix4x4

Defined in: libs/base/src/math/vector.ts:2172

Convert this quaternion to a 4x4 rotation matrix.

Parameters

matrix?

Matrix4x4

The output matrix, if not specified, a new matrix will be created.

Returns

Matrix4x4

The output matrix.

Remarks

Only left top 3x3 part of the matrix will be changed.


getDirectionX()

getDirectionX(result?): Vector3

Defined in: libs/base/src/math/vector.ts:2189

Get the direction of axis x

Parameters

result?

Vector3

The output vector, if not specified, a new vector will be created.

Returns

Vector3

The direction of axis x


getDirectionY()

getDirectionY(result?): Vector3

Defined in: libs/base/src/math/vector.ts:2202

Get the direction of axis y

Parameters

result?

Vector3

The output vector, if not specified, a new vector will be created.

Returns

Vector3

The direction of axis y


getDirectionZ()

getDirectionZ(result?): Vector3

Defined in: libs/base/src/math/vector.ts:2215

Get the direction of axis z

Parameters

result?

Vector3

The output vector, if not specified, a new vector will be created.

Returns

Vector3

The direction of axis z


getAxisAngle()

getAxisAngle(result?): Vector4

Defined in: libs/base/src/math/vector.ts:2228

Get the rotate angle and the rotation axis for this quaternion.

Parameters

result?

Vector4

The output vector, if not specified, a new vector will be created.

Returns

Vector4

An vector4 that the x, y, z component presents the axis and the w component presents the angle.


transform()

transform(v, result?): Vector3

Defined in: libs/base/src/math/vector.ts:2245

Rotate a vector

Parameters

v

Vector3

The vector to be rotated.

result?

Vector3

The output vector, if not specified, a new vector will be created.

Returns

Vector3

The rotation result.


scale()

static scale(q, t, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2272

Scale a Quaternion by a scalar number.

Parameters

q

Quaternion

t

number

result?

Quaternion

The output quaternion (can be the same quaternion as a). if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


dot()

static dot(a, b): number

Defined in: libs/base/src/math/vector.ts:2282

Calculates the dot product of two Quaternion's.

Parameters

a

Quaternion

The first operand.

b

Quaternion

The second operand.

Returns

number

dot product of a and b


identity()

static identity(q?): Quaternion

Defined in: libs/base/src/math/vector.ts:2290

Create an identity quaternion

Parameters

q?

Quaternion

The output quaternion, if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


normalize()

static normalize(q, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2299

Normalize a quaternion

Parameters

q

Quaternion

The input quaternion

result?

Quaternion

The output quaternion (can be the same as q), if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


conjugate()

static conjugate(q, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2309

Gets the conjugate of a quaternion

Parameters

q

Quaternion

The input quaternion

result?

Quaternion

The output quaternion (can be the same as q), if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


inverse()

static inverse(q, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2318

Gets the inverse of a quaternion

Parameters

q

Quaternion

The input quaternion

result?

Quaternion

The output quaternion (can be the same as q), if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


multiply()

static multiply(a, b, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2336

Multiply two Quaternion's.

Parameters

a

Quaternion

The first operand.

b

Quaternion

The second operand.

result?

Quaternion

The output quaternion (can be the same as a or b). if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


slerp()

static slerp(a, b, t, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2352

Performs a spherical linear interpolation between two quat.

Parameters

a

Quaternion

The first operand.

b

Quaternion

The second operand.

t

number

The interpolation amount, in the range [0-1].

result?

Quaternion

The output quaternion (can be the same as a or b), if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


angleBetween()

static angleBetween(a, b): number

Defined in: libs/base/src/math/vector.ts:2401

Gets the angular distance between two unit quaternions.

Parameters

a

Quaternion

The origin quaternion

b

Quaternion

The destination quaternion

Returns

number

  • The angle in radians

unitVectorToUnitVector()

static unitVectorToUnitVector(from, to, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2413

Creates a quaternion used to rotate a unit vector to another.

Parameters

from

Vector3

The unit vector to be rotated.

to

Vector3

The destination unit vector.

result?

Quaternion

The output quaternion, if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


fromEulerAngle()

static fromEulerAngle(a, b, c, order?, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2442

Creates a quaternion from an euler angle in specific order.

Parameters

a

number

b

number

c

number

order?

EulerAngleOrder = 'ZYX'

Intrinsic order for conversion.

result?

Quaternion

The output quaternion, if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


fromAxisAngle()

static fromAxisAngle(axis, angle, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2508

Creates a quaternion from the given angle and rotation axis.

Parameters

axis

Vector3

The rotation axis.

angle

number

The rotate angle.

result?

Quaternion

The output quaternion, if not specified, a new quaternion will be created.

Returns

Quaternion

The output quaternion


fromRotationMatrix()

static fromRotationMatrix(matrix, result?): Quaternion

Defined in: libs/base/src/math/vector.ts:2521

Creates a quaternion from a rotation matrix.

Parameters

matrix

Matrix3x3 | Matrix4x4

The rotation matrix.

result?

Quaternion

The output quaternion, if not specified, a new quaternion will be created.

Returns

Quaternion

self

Released under the MIT License.