Struct anima_engine::math::Quaternion [] [src]

pub struct Quaternion {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}

A simple quaterion struct tailored specifically for graphics.

Examples

use std::f32::consts;

let q1 = Quaternion::new_rot(Vector::up(), consts::PI / 4.0);
let q2 = Quaternion::new_rot(Vector::up(), consts::PI / 2.0);

let q3 = q1 * q1;

const EPSILON: f32 = 0.00001;

assert!((q3.x - q2.x).abs() < EPSILON);
assert!((q3.y - q2.y).abs() < EPSILON);
assert!((q3.z - q2.z).abs() < EPSILON);
assert!((q3.w - q2.w).abs() < EPSILON);

Fields

x

f32 imaginary i value

y

f32 imaginary j value

z

f32 imaginary k value

w

f32 real value

Methods

impl Quaternion

fn new(x: f32, y: f32, z: f32, w: f32) -> Quaternion

Creates a quaternion using 4 values.

Examples

let q = Quaternion::new(0.0, 1.0, 2.0, 3.0);

assert_eq!(q, Quaternion { x: 0.0, y: 1.0, z: 2.0, w: 3.0 });

fn new_rot(direction: Vector, angle: f32) -> Quaternion

Creates a quaternion equivalent to a rotation around a direction. The rotation is measured in radians.

Examples

let q1 = Quaternion::new_rot(Vector::up(), consts::PI / 2.0);
let q2 = Quaternion { x: 0.0, y: 0.70710677, z: 0.0, w: 0.70710677 };

const EPSILON: f32 = 0.00001;

assert!((q1.x - q2.x).abs() < EPSILON);
assert!((q1.y - q2.y).abs() < EPSILON);
assert!((q1.z - q2.z).abs() < EPSILON);
assert!((q1.w - q2.w).abs() < EPSILON);

fn new_sph_rot(start: Vector, finish: Vector) -> Quaternion

Creates a quaternion equivalent to the shortest rotation necessary to move the vector representing the direction start to the one representing finish.

Examples

let q = Quaternion::new_sph_rot(Vector::new(1.0, 1.0, 0.0), Vector::new(1.0, 1.0, 1.0));
let v = Vector::new(-1.0, -1.0, 0.0);

const EPSILON: f32 = 0.00001;

assert!((v.rot(q) - Vector::new_unf(-0.8164966)).len() < EPSILON);

fn ident() -> Quaternion

Creates an identity (0.0, 0.0, 0.0, 1.0) quaternion.

Examples

assert_eq!(Quaternion::ident(), Quaternion { x: 0.0, y: 0.0, z: 0.0, w: 1.0 });

fn conj(&self) -> Quaternion

Computes the conjugate of a quaternion.

Examples

let q = Quaternion::new(1.0, 1.0, 1.0, 1.0);

assert_eq!(q.conj(), Quaternion { x: -1.0, y: -1.0, z: -1.0, w: 1.0 });

fn inv(&self) -> Quaternion

Computes the inverse of a quaternion.

Examples

let q = anima_engine::math::Quaternion::new(0.0, 1.0, 2.0, 3.0);

let result = q * q.inv();
let identity = anima_engine::math::Quaternion::ident();

assert_eq!(result.x, identity.x);

fn dot(&self, other: Quaternion) -> f32

Computes the dot product between two quaternions.

Examples

let q1 = Quaternion::new(1.0, 2.0, 2.0, 1.0);
let q2 = Quaternion::new(3.0, 3.0, 1.0, 1.0);

assert_eq!(q1.dot(q2), 12.0);

fn angle(&self, other: Quaternion) -> f32

Computes the angle in radians between two quaternions.

Examples

let q = Quaternion::new_rot(Vector::up(), consts::PI / 2.0);

assert_eq!(Quaternion::ident().angle(q), consts::PI / 2.0);

Trait Implementations

impl Mul for Quaternion

type Output = Quaternion

fn mul(self, other: Quaternion) -> Quaternion

impl Interpolate for Quaternion

fn interpolate(&self, other: Quaternion, ratio: f32) -> Quaternion

impl MrubyFile for Quaternion

fn require(mruby: MrubyType)

Derived Implementations

impl PartialEq for Quaternion

fn eq(&self, __arg_0: &Quaternion) -> bool

fn ne(&self, __arg_0: &Quaternion) -> bool

impl Debug for Quaternion

fn fmt(&self, __arg_0: &mut Formatter) -> Result

impl Copy for Quaternion

impl Clone for Quaternion

fn clone(&self) -> Quaternion

1.0.0fn clone_from(&mut self, source: &Self)