A quaternion is a way to represent rotation matrices in 3d with 4 values.

This documentation will not integrate all the publicly available documentation.

From wikipedia : In 3-dimensional space, according to Euler rotation theorem, any rotation or sequence of rotations of a rigid body or coordinate system about a fixed point is equivalent to a single rotation by a given angle θ about a fixed axis (called the Euler axis) that runs through the fixed point. The Euler axis is typically represented by a unit vector u. Therefore, any rotation in three dimensions can be represented as a combination of a vector u and a scalar θ. Quaternions give a simple way to encode this axis-angle representation in four numbers, and can be used to apply the corresponding rotation to a position vector, representing a point relative to the origin in R3.

Such a quaternion q, with axis u (ux, uy, uz) and angle θ is expressed as : q = cos(θ/2) + sin(θ/2) * u. The scalar is the w coordinate of the quaternion.


Maths

See

https://en.wikipedia.org/wiki/Quaternion

Hierarchy

Constructors

  • Builds a new Quaternion with directional vector (px,py,pz) (at sin(θ/2) diff) and an angle θ/2 such as cos(θ/2) = pW.

    Returns

    The newly created quaternion.

    Parameters

    • Optional pX: number
      in
      x coordinate of the directional vector of the rotation (at sin(θ/2) diff).
    • Optional pY: number
      in
      y coordinate of the directional vector of the rotation (at sin(θ/2) diff).
    • Optional pZ: number
      in
      z coordinate of the directional vector of the rotation (at sin(θ/2) diff).
    • Optional pW: number
      in
      Angle of the rotation such as cos(θ/2) = pW.

    Returns Quaternion

Properties

w: number

w value of the vector, i.e. fourth component.

x: number

x value of the vector, i.e. first component.

y: number

y value of the vector, i.e. second component.

z: number

z value of the vector, i.e. third component.

Methods

  • Computes this + (pX,pY,pZ,pW) and stores it into pOut.

    If pOut is omitted, the result is stored in "this".

    Returns pOut.

    Returns

    pOut.

    Parameters

    • pX: number
      in
      x value of the vector to add.
    • pY: number
      in
      y value of the vector to add.
    • pZ: number
      in
      z value of the vector to add.
    • pW: number
      in
      w value of the vector to add.
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Computes this + pOther and stores it into pOut.

    If pOut is omitted, the result is stored in "this".

    Returns pOut.

    Returns

    pOut.

    Parameters

    • pOther: Vector4
      in
      The vector4 to add.
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Computes the rotation that aligns pSource onto pDestination.

    The cross product this.rotate(pSource) and pDestination should be a zero vector.

    Returns

    this.

    Parameters

    • pSource: Vector3
      in
      The start vector (not necessarily unit).
    • pDestination: Vector3
      in
      The vector to align to (not necessarily unit).

    Returns Quaternion

  • Computes the conjugate (i.e. inverse of the rotation) of this quaternion.

    Returns

    The conjugate of this quaternion.

    Parameters

    • Optional pOut: Quaternion
      out
      The computed inverse of the rotation (stored in "this" if omitted).

    Returns Quaternion

  • Copies this quaternion onto another quaternion.

    Defaults to allocating a new quaternion.

    Returns

    A copy of the quaternion (allocated by the library if pOther is omitted).

    Parameters

    • Optional pOut: Quaternion
      out
      The copy of this quaternion (allocated if omitted).

    Returns Quaternion

  • Computes the euclidean distance between this and pVec.

    Warning, this function does not assume that w=1, and does not assume the vector4 as a vector3. The w component is used in the calculation.

    Returns

    The distance between this and pVec.

    Parameters

    • pVec: Vector4
      in
      The other vector to compute the distance to.

    Returns number

  • Computes the dot product between this and (pX,pY,pZ,pW).

    Returns

    The dot product ( dot(this, (pX,pY,pZ,pW)) ).

    Parameters

    • pX: number
      in
      x value of the vector to compute the dot with.
    • pY: number
      in
      y value of the vector to compute the dot with.
    • pZ: number
      in
      z value of the vector to compute the dot with.
    • pW: number
      in
      w value of the vector to compute the dot with.

    Returns number

  • Computes the dot product between this and pOther.

    Returns

    The dot product ( dot(this, pOther) ).

    Parameters

    • pOther: Vector4
      in
      The other vector4 to compute the dot with.

    Returns number

  • Tells if the two quaternions represent the same rotation.

    Reminder : for two quaternions q1 and q2, q1 and q2 represent the same rotation if either one of these two conditions hold:

    • q1 is component wise strictly equal to q2.
    • q1 is component wise strictly equal to -q2.

    Returns

    true if the two quaternions represent the same rotation.

    Parameters

    • pOtherQuaternion: Quaternion
      in
      The other quaternion to compare to.

    Returns boolean

  • Computes the rotation quaternion from the given axis/angle representation.

    No normalization is computed on the directional vector of the axis, thus make sure pAxis is normalized to avoid undesired effect (or keep a non normalized directional vector if you know what you are doing).

    Returns

    this.

    Parameters

    • pAxis: Vector3
      in
      Directional vector of the axis (likely normalized).
    • pAngle: number
      in
      Angle in radian of the rotation.

    Returns Quaternion

  • Tells if the two vectors are homogeneously equivalent.

    This function tells if the two Vector4 represent the same 3D cartesian point.

    Returns

    true if the two vectors are homogeneously equivalent.

    Parameters

    • pOtherVector: Vector4
      in
      The other vector to compare to.

    Returns boolean

  • Gets the euclidean length of "this".

    Warning, this function does not assume that w=1, and does not assume the vector4 as a vector3. The w component is used in the calculation.

    Returns

    The length of this.

    Returns number

  • Gets the squared euclidean length of "this".

    Warning, this function does not assume that w=1, and does not assume the vector4 as a vector3. The w component is used in the calculation.

    Returns

    The squared length of this.

    Returns number

  • Computes the linear interpolation between this and (pX,pY,pZ,pW) and stores the result in pOut.

    pOut = this + pTime x ( (pX,pY,pZ,pW) - this).
    If pTime=0, pOut = this.
    If pTime=1, pOut = (pX,pY,pZ,pW).

    If pOut is omitted, the result of the interpolation is stored in this.

    No check is done to ensure pTime is in the range [0,1].

    Returns

    pOut.

    Parameters

    • pX: number
      in
      x value of the destination vector.
    • pY: number
      in
      y value of the destination vector.
    • pZ: number
      in
      z value of the destination vector.
    • pW: number
      in
      w value of the destination vector.
    • pTime: number
      in
      Interpolation parameter in the range [0,1].
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Computes the linear interpolation between this and pOther and stores the result in pOut.

    pOut = this + pTime x ( pOther - this).
    If pTime=0, pOut = this.
    If pTime=1, pOut = pOther.

    If pOut is omitted, the result of the interpolation is stored in this.

    No check is done to ensure pTime is in the range [0,1].

    Returns

    pOut.

    Parameters

    • pOther: Vector4
      in
      The destination vector.
    • pTime: number
      in
      Interpolation parameter in the range [0,1].
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Computes the composition (multiplication) of the two transformations "this" and the quaternion expressed by the values pX, pY, pZ, pW.

    Any point P will be transformed into P' such that P' = pOther x this x P (or P x this x pOther = P').

    Returns

    The composition (multiplication) of the two transformations, stored in "this" if pOut is undefined.

    Parameters

    • pX: number
      in
      x value of the quaternion rotation to compose with.
    • pY: number
      in
      y value of the quaternion rotation to compose with.
    • pZ: number
      in
      z value of the quaternion rotation to compose with.
    • pW: number
      in
      w value of the quaternion rotation to compose with.
    • Optional pOut: Quaternion
      out
      The resulting transformation ("this" will be applied first, then Quat(pX,pY,pZ,pW)).

    Returns Quaternion

  • Computes the composition (multiplication) of the two transformations this and other.

    Any point P will be transformed into P' such that P' = pOther x this x P (or P x this x pOther = P').

    Returns

    The composition (multiplication) of the two transformations, stored in "this" if pOut is omitted.

    Parameters

    • pOther: Quaternion
      in
      The rotation to compose with.
    • Optional pOut: Quaternion
      out
      The resulting transformation ("this" will be applied first, then pOther).

    Returns Quaternion

  • Computes the opposite of this (-this) and stores the result in pOut.

    If pOut is omitted, the result is stored in "this".

    Returns

    pOut.

    Parameters

    • Optional pOut: Vector4
      out
      The resulting vector.

    Returns Vector4

  • Computes the normalization of this (i.e. scales "this" such that pOut.length() == 1, a zero vector is left unchanged) and stores the result in pOut.

    If pOut is omitted, "this" is normalized.

    Warning, this function does not assume that w=1, and does not assume the vector4 as a vector3. The w component is used in the calculation.

    Returns

    pOut.

    Parameters

    • Optional pOut: Vector4
      out
      The resulting vector.

    Returns Vector4

  • Computes the euclidean distance from the plane represented by this to pPoint.

    The Vector4 can be the parametric equation of a plane in the form P: {t,u,v} / at + bu + cv = d.
    So, with the components x,y,z,w of the vector 4 : {t,u,v} / xt + yu + zv = w where (x,y,z) is the normal vector of the plane.

    Returns

    The distance between the plane represented by this and pPoint.

    Parameters

    • pPoint: Vector3
      in
      The distance to compute the distance to.

    Returns number

  • Rotates (i.e computes the transformation) the given vector by the rotation representation of this quaternion.

    Returns

    The result of the rotation of pVec (pOut is returned).

    Parameters

    • pVec: Vector3
      in
      The input vector to rotate.
    • Optional pOut: Vector3
      out
      The result of the rotation of the vector pVec by this quaternion. If omitted, the input vector pVec is transformed.

    Returns Vector3

  • Computes the scale of this by the scalar pScalar, and stores the result in pOut.

    pOut = pScalar x this.

    If pOut is omitted, the result is stored in this.

    Returns

    pOut.

    Parameters

    • pScalar: number
      in
      The scale factor.
    • Optional pOut: Vector4
      out
      The resulting vector.

    Returns Vector4

  • Sets the vector components.

    Returns

    this.

    Parameters

    • pX: number
      in
      x value.
    • pY: number
      in
      y value.
    • pZ: number
      in
      z value.
    • pW: number
      in
      w value.

    Returns Vector4

  • Sets this Vector4 to be the representation of the plane defined by its normal and a point of this plane.

    No check is done to ensure that pNormal is normalized.

    The Vector4 can be the parametric equation of a plane in the form P: {t,u,v} / at + bu + cv = d.
    So, with the components x,y,z,w of the vector 4 : {t,u,v} / xt + yu + zv = w where (x,y,z) is the normal vector of the plane.

    Returns

    this.

    Parameters

    • pNormal: Vector3
      in
      The normal of the plane.
    • pPlanePoint: Vector3
      in
      A point that belongs to the plane.

    Returns Vector4

  • Sets this quaternion from the given rotation matrix.

    Returns

    this.

    Parameters

    • pMatrix: Matrix4
      in
      The rotation matrix to copy data from.

    Returns Quaternion

  • Computes the spherical linear interpolation between the starting quaternion "this" and the end quaternion (pEndX,pEndY,pEndZ,pEndW).

    If pTime=0, pOut stores "this". If pTime=1, pOut stores pEnd. pTime is in the range [0,1].

    No check is done to ensure pTime is in the correct range.

    The resulting transformation is stored in pOut.

    Returns

    The result of the interpolation (pOut). "this" is returned if pOut is omitted.

    Parameters

    • pEndX: number
      in
      x component of the end quaternion of the interpolation.
    • pEndY: number
      in
      y component of the end quaternion of the interpolation.
    • pEndZ: number
      in
      z component of the end quaternion of the interpolation.
    • pEndW: number
      in
      w component of the end quaternion of the interpolation.
    • pTime: number
      in
      Number in the range [0,1].
    • Optional pOut: Quaternion
      out
      The result of the interpolation.

    Returns Quaternion

  • Computes the spherical linear interpolation between the starting quaternion "this" and the end quaternion pEnd.

    If pTime=0, pOut stores "this". If pTime=1, pOut stores pEnd. pTime is in the range [0,1].

    No check is done to ensure pTime is in the correct range.

    The resulting transformation is stored in pOut.

    Returns

    The result of the interpolation (pOut). "this" is returned if pOut is omitted.

    Parameters

    • pEnd: Quaternion
      in
      The end quaternion of the interpolation.
    • pTime: number
      in
      Number in the range [0,1].
    • Optional pOut: Quaternion
      out
      The result of the interpolation.

    Returns Quaternion

  • Computes the squared euclidean distance between this and pVec.

    Warning, this function does not assume that w=1, and does not assume the vector4 as a vector3. The w component is used in the calculation.

    Returns

    The squared distance between this and pVec.

    Parameters

    • pVec: Vector4
      in
      The other vector to compute the distance to.

    Returns number

  • Computes this - (pX,pY,pZ,pW) and stores it into pOut.

    If pOut is omitted, the result is stored in "this".

    Returns pOut.

    Returns

    pOut.

    Parameters

    • pX: number
      in
      x value of the vector to subtract.
    • pY: number
      in
      y value of the vector to subtract.
    • pZ: number
      in
      z value of the vector to subtract.
    • pW: number
      in
      w value of the vector to subtract.
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Computes this - pOther and stores it into pOut.

    If pOut is omitted, the result is stored in "this".

    Returns pOut.

    Returns

    pOut.

    Parameters

    • pOther: Vector4
      in
      The vector4 to subtract.
    • Optional pOut: Vector4
      out
      The resulting vector4.

    Returns Vector4

  • Extracts this rotation quaternion as an axis/angle representation in the resulting Vector4.

    The directional vector of the axis is the x,y,z components of the Vector4, the angle in radian is the w component.

    Parameters

    • pAxisAngle: Vector4
      out
      The resulting axis/angle representation.

    Returns void

  • Gets the string representation of the vector.

    Returns

    The string representation of the vector.

    Returns string