# base/quaternion (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/quaternion/

Quaternion. (the original base/quaternion.js; a 3D rotation and the basic unit of skeletal animation interpolation)
- The component order is (x, y, z, w) as in the original, with w the real part. The default is the identity quaternion (0, 0, 0, 1).
- multiply is the Hamilton product this x other. As matrices, R(this x other) = R(this) x R(other), so
  the other rotation applies first to column vectors. (in the original's words, "accumulates the other rotation locally after the this rotation")
- create_from_euler(x, y, z) is in radians and equals qX x qY x qZ. As matrices it is Rx x Ry x Rz, so it applies to points in Z, Y, X order.
  (the same formula as three.js's 'XYZ' order)
- The original keeps x, y, z, w as hidden fields with properties, but the Rust version is a Copy value with public fields. The original clone is replaced by copying.
- Numbers are f64 like the original JavaScript numbers.
- Conversion from a matrix to a quaternion and back to Euler angles, which the original lacks, were not added. (for a matrix, Matrix4::create_rotation_from_quaternion)

## Quaternion (struct) (Clone, Copy, Debug, PartialEq, Default)

Quaternion. (a 3D rotation, a Copy value with x, y, z, w as public fields, defaults to the identity quaternion)

Example: Spherically interpolate between two rotations

```rust
use vanilla::{math, Matrix4, Quaternion};

let start = Quaternion::identity();
let end = Quaternion::create_from_euler(0.0, math::PI * 0.5, 0.0);
let middle = Quaternion::slerp(start, end, 0.5);
let expected = Quaternion::create_from_euler(0.0, math::PI * 0.25, 0.0);
assert!((middle.y - expected.y).abs() < 1e-5);
assert!((middle.w - expected.w).abs() < 1e-5);

let rotation_matrix = Matrix4::create_rotation_from_quaternion(middle);
let forward = rotation_matrix.transform_direction(0.0, 0.0, 1.0);
assert!((forward.x - forward.z).abs() < 1e-5);
```

- `pub x: f64` (field, since 0.3.2): Vector part x. (rotation axis x multiplied by sin(angle / 2))
  - returns f64
- `pub y: f64` (field, since 0.3.2): Vector part y. (rotation axis y multiplied by sin(angle / 2))
  - returns f64
- `pub z: f64` (field, since 0.3.2): Vector part z. (rotation axis z multiplied by sin(angle / 2))
  - returns f64
- `pub w: f64` (field, since 0.3.2): Real part. (cos(angle / 2))
  - returns f64
- `pub const fn create(x: f64, y: f64, z: f64, w: f64) -> Quaternion` (static, since 0.3.2): Constructor.
  - `x` f64: Vector part x.
  - `y` f64: Vector part y.
  - `z` f64: Vector part z.
  - `w` f64: Real part.
  - returns Quaternion: The quaternion (x, y, z, w). (not normalized)
- `pub const fn identity() -> Quaternion` (static, since 0.3.2): Identity quaternion. (0, 0, 0, 1)
  - returns Quaternion: A quaternion with no rotation.
- `pub fn set(&mut self, x: f64, y: f64, z: f64, w: f64)` (method, since 0.3.2): Assignment.
  - `x` f64: Vector part x to set.
  - `y` f64: Vector part y to set.
  - `z` f64: Vector part z to set.
  - `w` f64: Real part to set.
- `pub fn normalize(&self) -> Quaternion` (method, since 0.3.2): Normalization. (the identity quaternion if the length is 0 or less)
  - returns Quaternion: A new quaternion of length 1. The identity quaternion if the length is 0 or less.
- `pub fn multiply(&self, other: Quaternion) -> Quaternion` (method, since 0.3.2): Multiplication. (Hamilton product this x other; accumulates the other rotation locally after the this rotation)
  - `other` Quaternion: Quaternion to multiply on the right. (the other rotation applies to points first)
  - returns Quaternion: A new quaternion equal to self x other. (not normalized)

  Example: Multiplying 90 degrees about Z twice gives 180 degrees

  ```rust
  use vanilla::{math, Quaternion};
  
  let quarter_turn = Quaternion::create_from_euler(0.0, 0.0, math::PI * 0.5);
  let half_turn = quarter_turn.multiply(quarter_turn);
  assert!((half_turn.z - 1.0).abs() < 1e-6);
  assert!(half_turn.w.abs() < 1e-6);
  ```

- `pub fn create_from_euler(x_radian: f64, y_radian: f64, z_radian: f64) -> Quaternion` (static, since 0.3.2): Creates from Euler angles. (XYZ order in radians, equals qX x qY x qZ)
  - `x_radian` f64: X axis rotation angle. (radians)
  - `y_radian` f64: Y axis rotation angle. (radians)
  - `z_radian` f64: Z axis rotation angle. (radians)
  - returns Quaternion: Unit quaternion combining the three axis rotations. (applies to points in Z, Y, X order)

  Example: Equals multiplying the per-axis quaternions in X, Y, Z order

  ```rust
  use vanilla::Quaternion;
  
  let from_euler = Quaternion::create_from_euler(0.3, -0.7, 1.1);
  let quaternion_x = Quaternion::create_from_euler(0.3, 0.0, 0.0);
  let quaternion_y = Quaternion::create_from_euler(0.0, -0.7, 0.0);
  let quaternion_z = Quaternion::create_from_euler(0.0, 0.0, 1.1);
  let multiplied = quaternion_x.multiply(quaternion_y).multiply(quaternion_z);
  assert!((from_euler.x - multiplied.x).abs() < 1e-5);
  assert!((from_euler.y - multiplied.y).abs() < 1e-5);
  assert!((from_euler.z - multiplied.z).abs() < 1e-5);
  assert!((from_euler.w - multiplied.w).abs() < 1e-5);
  ```

- `pub fn slerp(from: Quaternion, to: Quaternion, normalized_time: f64) -> Quaternion` (static, since 0.3.2): Spherical linear interpolation. (shortest path; if nearly the same direction, linear interpolation followed by normalization)
  - `from` Quaternion: Start rotation.
  - `to` Quaternion: End rotation. (if its dot product with from is negative, the sign is flipped to take the short way)
  - `normalized_time` f64: Interpolation ratio. (0 gives the same rotation as from, 1 the same as to)
  - returns Quaternion: The interpolated rotation. (returned normalized)

  Example: An end rotation with the opposite sign also takes the short way

  ```rust
  use vanilla::Quaternion;
  
  let diagonal_component = std::f64::consts::FRAC_1_SQRT_2;
  let to = Quaternion::create(0.0, 0.0, -diagonal_component, -diagonal_component);
  let middle = Quaternion::slerp(Quaternion::identity(), to, 0.5);
  assert!((middle.z - 0.38268343).abs() < 1e-5);
  assert!((middle.w - 0.9238795).abs() < 1e-5);
  ```

