class Quaternion extends MathArray extends Array
A class to handle Quaternions. More information on quternions can be found here. The quaternion will be represented by an instance with x
, y
, z
, w
components that make a quaternion like: xi + yj + zk + w
.
import {Quaternion} from '@math.gl/core';
Gets or sets element 0, 1, 2 or 3 respectively
Many of the most commonly used methods are inherited from MathArray
:
quaternion.clone()
quaternion.copy(array)
quaternion.set(...args)
quaternion.fromArray(array, offset = 0)
quaternion.toString()
quaternion.toArray(array = [], offset = 0)
quaternion.equals(array)
quaternion.exactEquals(array)
quaternion.validate(array = this)
quaternion.check(array = this)
quaternion.normalize()
Note that Quaternion
is a subclass of the built in JavaScript Array
and can thus technically be supplied as a parameter to any function expecting an Array
.
constructor(x = 0, y = 0, z = 0, w = 1)
Creates a quaternion from the given 3x3 rotation matrix. NOTE: The resultant quaternion is not normalized, so you should be sure to renormalize the quaternion yourself where necessary.
fromMatrix3(m)
Creates a new quat initialized with the given values
fromValues(x, y, z, w)
Set a quat to the identity quaternion
identity()
Calculates the length of a quaternion
length()
Calculates the squared length of a quaternion
squaredLength(a)
@returnNumber}
Calculates the dot product of two quat's
quaternion.dot(a, b)
Gets the rotation axis and angle for a given quaternion.
quaternion.getAxisAngle()
If a quaternion is created with setAxisAngle, this method will return the same values as providied in the original parameter list OR functionally equivalent values.
Example: The quaternion formed by axis [0, 0, 1] and angle -90 is the same as the quaternion formed by [0, 0, 1] and 270. This method favors the latter.
Sets a quaternion to represent the shortest rotation from one vector to another. Both vectors are assumed to be unit length.
quaternion.rotationTo(vectorA, vectorB)
Adds two quaternions
quaternion.add(a, b)
Calculates the W component of a quat from the X, Y, and Z components. Any existing W component will be ignored.
quaternion.calculateW()
Calculates the conjugate of a quat If the quaternion is normalized, this function is faster than quat_inverse and produces the same result.
quaternion.conjugate()
Calculates the inverse of a quat
quaternion.invert()
Performs a linear interpolation between two quat's
quaternion.lerp(a, b, t)
Multiplies two quat's
multiply(a, b)
Normalize a quat
Rotates a quaternion by the given angle about the X axis
rotateX(rad)
Rotates a quaternion by the given angle about the Y axis
rotateY(rad)
Rotates a quaternion by the given angle about the Z axis
rotateZ(rad)
Scales a quat by a scalar number
scale(b)
Set the components of a quat to the given values
set(i, j, k, l)
Sets a quat from the given angle and rotation axis, then returns it.
setAxisAngle(axis, rad)
Performs a spherical linear interpolation between two quaternions
slerp({start = [0, 0, 0, 1], target, ratio})
s