melonJS
    Preparing search index...

    Class Quaternion

    An orientation in 3D: a rotation about an axis, stored as a unit quaternion (x, y, z, w). What a 3D physics body's rotation is read and written as (PhysicsAdapter.getRotation, PhysicsAdapter.setRotation), and handy wherever turns are composed or blended, as it has none of the gimbal lock of three Euler angles.

    (0, 0, 0, 1) is no rotation. Rotations follow the right-hand rule in the engine's own space (Y-down, +Z forward, the Camera3d convention), as Matrix3d#rotate does.

    Its methods read as a Matrix3d's do: a new one is no rotation, rotate(angle, axis) turns it further, multiply composes, invert, apply(v) / applyInverse(v) turn a vector, and fromMat3d / toMatrix3d convert, as Matrix2d#fromMat3d and Matrix3d#toMatrix2d do.

    import { Quaternion, Vector3d } from "melonjs";

    // a quarter turn about the vertical axis, as a Matrix3d would be turned
    const turn = new Quaternion().rotate(Math.PI / 2, new Vector3d(0, 1, 0));
    // where it takes +X
    const v = turn.apply(new Vector3d(1, 0, 0));
    // and a renderable turned by it
    mesh.currentTransform.identity().rotate(turn);
    Index
    • Parameters

      • Optionalx: number = 0

        x of the vector part

      • Optionaly: number = 0

        y of the vector part

      • Optionalz: number = 0

        z of the vector part

      • Optionalw: number = 1

        the scalar part; 1 (with the rest 0) is no rotation

      Returns Quaternion

    w: number
    x: number
    y: number
    z: number
    • Turn this rotation further by angle radians about an axis, as Matrix3d#rotate turns a matrix: this = this × R(axis, angle), so the new turn is applied first, then this one. On a new quaternion, that is simply the turn. The axis defaults to Z and need not be of unit length; an axis of length 0, or an angle or axis that is not a finite number, turns nothing.

      Parameters

      • angle: number

        the angle, in radians

      • Optionalv: Vector3d

        the axis to turn about (defaults to Z)

      Returns Quaternion

      this, for chaining

      // yaw, then pitch, as a camera turns
      const q = new Quaternion()
      .rotate(yaw, new Vector3d(0, 1, 0))
      .rotate(pitch, new Vector3d(1, 0, 0));
    • The shortest turn that takes the direction from onto the direction to. Neither needs to be of unit length. Two opposite directions are joined by a half turn about an axis square to them; a direction of length 0, or one that is not a number, gives no rotation.

      Parameters

      • from: Vector3d

        the direction to turn from

      • to: Vector3d

        the direction to turn onto

      Returns Quaternion

      this, for chaining

      // a band modeled along +X, laid along the line from a to b
      const along = b.clone().sub(a);
      band.rotate(new Quaternion().setFromUnitVectors(new Vector3d(1, 0, 0), along));
    • The components as an array, [x, y, z, w], the order glTF stores rotations in.

      Returns [number, number, number, number]

      the array