Quaternion(x, y, z, w)

Class representing a quaternion.

new Quaternion(x, y, z, w)

Constructs a new quaternion with the given values.

Parameters:
Name Type Default Description
x Number 0

The x component.

y Number 0

The y component.

z Number 0

The z component.

w Number 1

The w component.

Author:

Members

w :Number

The w component.

x :Number

The x component.

y :Number

The y component.

z :Number

The z component.

Methods

angleTo(q) → {Number}

Computes the shortest angle between two rotation defined by this quaternion and the given one.

Parameters:
Name Type Description
q Quaternion

The given quaternion.

Returns:
Number -

The angle in radians.

clone() → {Quaternion}

Creates a new quaternion and copies all values from this quaternion.

Returns:
Quaternion -

A new quaternion.

conjugate() → {Quaternion}

Computes the conjugate of this quaternion.

Returns:
Quaternion -

A reference to this quaternion.

copy(q) → {Quaternion}

Copies all values from the given quaternion to this quaternion.

Parameters:
Name Type Description
q Quaternion

The quaternion to copy.

Returns:
Quaternion -

A reference to this quaternion.

dot(q) → {Quaternion}

Computes the dot product of this and the given quaternion.

Parameters:
Name Type Description
q Quaternion

The given quaternion.

Returns:
Quaternion -

A reference to this quaternion.

equals(q) → {Boolean}

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

Parameters:
Name Type Description
q Quaternion

The quaternion to test.

Returns:
Boolean -

The result of the equality test.

extractRotationFromMatrix(m) → {Quaternion}

Extracts the rotation of the given 4x4 matrix and stores it in this quaternion.

Parameters:
Name Type Description
m Matrix4

A 4x4 matrix.

Returns:
Quaternion -

A reference to this quaternion.

fromArray(array, offset) → {Quaternion}

Sets the components of this quaternion from an array.

Parameters:
Name Type Default Description
array Array.<Number>

An array.

offset Number 0

An optional offset.

Returns:
Quaternion -

A reference to this quaternion.

fromEuler(x, y, z) → {Quaternion}

Sets the components of this quaternion from the given euler angle (YXZ order).

Parameters:
Name Type Description
x Number

Rotation around x axis in radians.

y Number

Rotation around y axis in radians.

z Number

Rotation around z axis in radians.

Returns:
Quaternion -

A reference to this quaternion.

fromMatrix3(m) → {Quaternion}

Sets the components of this quaternion from the given 3x3 rotation matrix.

Parameters:
Name Type Description
m Matrix3

The rotation matrix.

Returns:
Quaternion -

A reference to this quaternion.

inverse() → {Quaternion}

Computes the inverse of this quaternion.

Returns:
Quaternion -

A reference to this quaternion.

length() → {Number}

Computes the length of this quaternion.

Returns:
Number -

The length of this quaternion.

lookAt(localForward, targetDirection, localUp) → {Quaternion}

Creates a quaternion that orients an object to face towards a specified target direction.

Parameters:
Name Type Description
localForward Vector3

Specifies the forward direction in the local space of the object.

targetDirection Vector3

Specifies the desired world space direction the object should look at.

localUp Vector3

Specifies the up direction in the local space of the object.

Returns:
Quaternion -

A reference to this quaternion.

multiply(q) → {Quaternion}

Multiplies this quaternion with the given quaternion.

Parameters:
Name Type Description
q Quaternion

The quaternion to multiply.

Returns:
Quaternion -

A reference to this quaternion.

multiplyQuaternions(a, b) → {Quaternion}

Multiplies two given quaternions and stores the result in this quaternion.

Parameters:
Name Type Description
a Quaternion

The first quaternion of the operation.

b Quaternion

The second quaternion of the operation.

Returns:
Quaternion -

A reference to this quaternion.

normalize() → {Quaternion}

Normalizes this quaternion.

Returns:
Quaternion -

A reference to this quaternion.

premultiply(q) → {Quaternion}

Multiplies the given quaternion with this quaternion. So the order of the multiplication is switched compared to Quaternion#multiply.

Parameters:
Name Type Description
q Quaternion

The quaternion to multiply.

Returns:
Quaternion -

A reference to this quaternion.

rotateTo(q, step, tolerance) → {Boolean}

Transforms this rotation defined by this quaternion towards the target rotation defined by the given quaternion by the given angular step. The rotation will not overshoot.

Parameters:
Name Type Default Description
q Quaternion

The target rotation.

step Number

The maximum step in radians.

tolerance Number 0.0001

A tolerance value in radians to tweak the result when both rotations are considered to be equal.

Returns:
Boolean -

Whether the given quaternion already represents the target rotation.

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

Sets the given values to this quaternion.

Parameters:
Name Type Description
x Number

The x component.

y Number

The y component.

z Number

The z component.

w Number

The w component.

Returns:
Quaternion -

A reference to this quaternion.

slerp(q, t) → {Quaternion}

Spherically interpolates between this quaternion and the given quaternion by t. The parameter t is clamped to the range [0, 1].

Parameters:
Name Type Description
q Quaternion

The target rotation.

t Number

The interpolation parameter.

Returns:
Quaternion -

A reference to this quaternion.

squaredLength() → {Number}

Computes the squared length of this quaternion.

Returns:
Number -

The squared length of this quaternion.

toArray(array, offset) → {Array.<Number>}

Copies all values of this quaternion to the given array.

Parameters:
Name Type Default Description
array Array.<Number>

An array.

offset Number 0

An optional offset.

Returns:
Array.<Number> -

The array with the quaternion components.

toEuler(euler) → {Object}

Returns an euler angel (YXZ order) representation of this quaternion.

Parameters:
Name Type Description
euler Object

The resulting euler angles.

Returns:
Object -

The resulting euler angles.