Quaternion

Class for representing a Quaternion.

Constructor

new Quaternion(xopt, yopt, zopt, wopt)

Constructs a new quaternion.

Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0

The x value of this quaternion.

ynumber<optional>
0

The y value of this quaternion.

znumber<optional>
0

The z value of this quaternion.

wnumber<optional>
1

The w value of this quaternion.

Members

(readonly) isQuaternion :boolean

This flag can be used for type testing.

Type:
  • boolean
Default Value
  • true

w :number

The w value of this quaternion.

Type:
  • number
Default Value
  • 1

x :number

The x value of this quaternion.

Type:
  • number
Default Value
  • 0

y :number

The y value of this quaternion.

Type:
  • number
Default Value
  • 0

z :number

The z value of this quaternion.

Type:
  • number
Default Value
  • 0

Methods

angleTo(q) → {number}

Returns the angle between this quaternion and the given one in radians.

Parameters:
NameTypeDescription
qQuaternion

The quaternion to compute the angle with.

Returns:

The angle in radians.

Type: 
number

clone() → {Quaternion}

Returns a new quaternion with copied values from this instance.

Returns:

A clone of this instance.

Type: 
Quaternion

conjugate() → {Quaternion}

Returns the rotational conjugate of this quaternion. The conjugate of a quaternion represents the same rotation in the opposite direction about the rotational axis.

Returns:

A reference to this quaternion.

Type: 
Quaternion

copy(quaternion) → {Quaternion}

Copies the values of the given quaternion to this instance.

Parameters:
NameTypeDescription
quaternionQuaternion

The quaternion to copy.

Returns:

A reference to this quaternion.

Type: 
Quaternion

dot(v) → {number}

Calculates the dot product of this quaternion and the given one.

Parameters:
NameTypeDescription
vQuaternion

The quaternion to compute the dot product with.

Returns:

The result of the dot product.

Type: 
number

equals(quaternion) → {boolean}

Returns true if this quaternion is equal with the given one.

Parameters:
NameTypeDescription
quaternionQuaternion

The quaternion to test for equality.

Returns:

Whether this quaternion is equal with the given one.

Type: 
boolean

fromArray(array, offsetopt, denormalizeopt) → {Quaternion}

Sets this quaternion's components from the given array.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number>

An array holding the quaternion component values.

offsetnumber<optional>
0

The offset into the array.

denormalizeboolean<optional>
false

If true, denormalize the values, and array should be a typed array.

Returns:

A reference to this quaternion.

Type: 
Quaternion

identity() → {Quaternion}

Sets this quaternion to the identity quaternion; that is, to the quaternion that represents "no rotation".

Returns:

A reference to this quaternion.

Type: 
Quaternion

length() → {number}

Computes the Euclidean length (straight-line length) of this quaternion, considered as a 4 dimensional vector.

Returns:

The Euclidean length.

Type: 
number

lengthSq() → {number}

Computes the squared Euclidean length (straight-line length) of this quaternion, considered as a 4 dimensional vector. This can be useful if you are comparing the lengths of two quaternions, as this is a slightly more efficient calculation than Quaternion#length.

Returns:

The squared Euclidean length.

Type: 
number

lerpQuaternions(q1, q2, ratio) → {Quaternion}

Linearly interpolates between two quaternions.

Parameters:
NameTypeDescription
q1Quaternion
q2Quaternion
rationumber
Returns:
Type: 
Quaternion

multiply(q) → {Quaternion}

Multiplies this quaternion by the given one.

Parameters:
NameTypeDescription
qQuaternion

The quaternion.

Returns:

A reference to this quaternion.

Type: 
Quaternion

multiplyQuaternions(a, b) → {Quaternion}

Multiplies the given quaternions and stores the result in this instance.

Parameters:
NameTypeDescription
aQuaternion

The first quaternion.

bQuaternion

The second quaternion.

Returns:

A reference to this quaternion.

Type: 
Quaternion

normalize() → {Quaternion}

Normalizes this quaternion - that is, calculated the quaternion that performs the same rotation as this one, but has a length equal to 1.

Returns:

A reference to this quaternion.

Type: 
Quaternion

onChange(callback) → {Quaternion}

Registers a callback that is called whenever the quaternion's angle value changes.

Parameters:
NameTypeDescription
callbackfunction

When the Quaternion angle value changes, the callback method is triggered

Returns:

A reference to this quaternion.

Type: 
Quaternion

premultiply(q) → {Quaternion}

Pre-multiplies this quaternion by the given one.

Parameters:
NameTypeDescription
qQuaternion

The quaternion.

Returns:

A reference to this quaternion.

Type: 
Quaternion

set(x, y, z, w) → {Quaternion}

Sets the quaternion components.

Parameters:
NameTypeDescription
xnumber

The x value of this quaternion.

ynumber

The y value of this quaternion.

znumber

The z value of this quaternion.

wnumber

The w value of this quaternion.

Returns:

A reference to this quaternion.

Type: 
Quaternion

setFromAxisAngle(axis, angle) → {Quaternion}

Sets this quaternion from the given axis and angle.

Parameters:
NameTypeDescription
axisVector3

The normalized axis.

anglenumber

The angle in radians.

Returns:

A reference to this quaternion.

Type: 
Quaternion

setFromEuler(euler, updateopt) → {Quaternion}

Sets this quaternion from the rotation specified by the given Euler angles.

Parameters:
NameTypeAttributesDefaultDescription
eulerEuler

The Euler angles.

updateboolean<optional>
true

Whether the internal onChange callback should be executed or not.

Returns:

A reference to this quaternion.

Type: 
Quaternion

setFromRotationMatrix(m) → {Quaternion}

Sets this quaternion from the given rotation matrix.

Parameters:
NameTypeDescription
mMatrix4

A 4x4 matrix of which the upper 3x3 of matrix is a pure rotation matrix (i.e. unscaled).

Returns:

A reference to this quaternion.

Type: 
Quaternion

setFromUnitVectors(vFrom, vTo) → {Quaternion}

Sets this quaternion to the rotation required to rotate the direction vector vFrom to the direction vector vTo.

Parameters:
NameTypeDescription
vFromVector3

The first (normalized) direction vector.

vToVector3

The second (normalized) direction vector.

Returns:

A reference to this quaternion.

Type: 
Quaternion

slerp(qb, t) → {Quaternion}

Performs a spherical linear interpolation between quaternions.

Parameters:
NameTypeDescription
qbQuaternion

The target quaternion.

tnumber

The interpolation factor in the closed interval [0, 1].

Returns:

A reference to this quaternion.

Type: 
Quaternion

slerpQuaternions(qa, qb, t) → {Quaternion}

Performs a spherical linear interpolation between the given quaternions and stores the result in this quaternion.

Parameters:
NameTypeDescription
qaQuaternion

The source quaternion.

qbQuaternion

The target quaternion.

tnumber

The interpolation factor in the closed interval [0, 1].

Returns:

A reference to this quaternion.

Type: 
Quaternion

toArray(arrayopt, offsetopt, normalizeopt) → {Quaternion}

Writes the components of this quaternion to the given array. If no array is provided, the method returns a new instance.

Parameters:
NameTypeAttributesDefaultDescription
arrayArray.<number><optional>
[]

The target array holding the quaternion components.

offsetnumber<optional>
0

Index of the first element in the array.

normalizeboolean<optional>
false

If true, normalize the values, and array should be a typed array.

Returns:

The quaternion components.

Type: 
Quaternion

toMatrix4(target) → {Matrix4}

Convert the current quaternion to a matrix4.

Parameters:
NameTypeDescription
targetMatrix4

The target matrix to write the quaternion data to.

Returns:

The target matrix with the quaternion data written to it.

Type: 
Matrix4

(static) multiplyQuaternionsFlat(dst, dstOffset, src0, srcOffset0, src1, srcOffset1) → {Array.<number>}

Multiplies two quaternions. This implementation assumes the quaternion data are managed in flat arrays.

Parameters:
NameTypeDescription
dstArray.<number>

The destination array.

dstOffsetnumber

An offset into the destination array.

src0Array.<number>

The source array of the first quaternion.

srcOffset0number

An offset into the first source array.

src1Array.<number>

The source array of the second quaternion.

srcOffset1number

An offset into the second source array.

Returns:

The destination array.

Type: 
Array.<number>

(static) slerpFlat(dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t)

Interpolates between two quaternions via SLERP. This implementation assumes the quaternion data are managed in flat arrays.

Parameters:
NameTypeDescription
dstArray.<number>

The destination array.

dstOffsetnumber

An offset into the destination array.

src0Array.<number>

The source array of the first quaternion.

srcOffset0number

An offset into the first source array.

src1Array.<number>

The source array of the second quaternion.

srcOffset1number

An offset into the second source array.

tnumber

The interpolation factor in the range [0,1].