Struct gamemath::Quat

source ·
pub struct Quat {
    pub x: f32,
    pub y: f32,
    pub z: f32,
    pub w: f32,
}
Expand description

A quaternion data type used for representing spatial rotation in a 3D environment.

Fields§

§x: f32

The X/first component of the quaternion.

§y: f32

The Y/second component of the quaternion.

§z: f32

The Z/third component of the quaternion.

§w: f32

The W/fourth component of the quaternion.

Implementations§

source§

impl Quat

source

pub fn identity() -> Quat

Constructs an identity quaternion.

Examples
use gamemath::Quat;

let q = Quat::identity();

assert_eq!(q, (0.0, 0.0, 0.0, 1.0).into());
source

pub fn rotation(radians: f32, axis: Vec3<f32>) -> Quat

Constructs a rotation quaternion from an angle and an axis.

Examples
use gamemath::{Vec3, Quat};

let q = Quat::rotation(1.0, Vec3::new(1.0, 2.0, 3.0));

assert_eq!(q, (0.12813187, 0.25626373, 0.38439557, 0.87758255).into());
source

pub fn rotated(&self, radians: f32, axis: Vec3<f32>) -> Quat

Calculate and returns a quaternion representing the calling object rotated by an angle around an axis.

Examples
use gamemath::{Vec3, Quat};

let q = Quat::identity();

assert_eq!(q.rotated(1.0, Vec3::new(1.0, 2.0, 3.0)), (0.12813187, 0.25626373, 0.38439557, 0.87758255).into());
source

pub fn rotate(&mut self, radians: f32, axis: Vec3<f32>)

Applies a rotation around and axis by an angle on the calling Quat object.

Examples
use gamemath::{Vec3, Quat};

let mut q = Quat::identity();

q.rotate(1.0, Vec3::new(1.0, 2.0, 3.0));

assert_eq!(q, (0.12813187, 0.25626373, 0.38439557, 0.87758255).into());
source

pub fn length_squared(&self) -> f32

Calculates the squared length/magnitude/norm of a Quat. This saves an expensive square root calculation compared to calculating the actual length, and comparing two squared lengths can therefore often be cheaper than, and yield the same result as, computing two real lengths.

Examples
use gamemath::Quat;

let q: Quat = (1.0, 2.0, 3.0, 4.0).into();

assert_eq!(q.length_squared(), 30.0);
source

pub fn length(&self) -> f32

Calculates the real length/magnitude/norm of a Quat. This results in an expensive square root calculation, and you might want to consider using a squared length instead when possible.

Examples
use gamemath::Quat;

let q: Quat = (1.0, 4.0, 4.0, 16.0).into();

assert_eq!(q.length(), 17.0);
source

pub fn normalized(&self) -> Quat

Calculates and returns the unit quaternion representation of a Quat. This results in an an expensive square root calculation.

Examples
use gamemath::Quat;

let q: Quat = (1.0, 2.0, 2.0, 4.0).into();

assert_eq!(q.normalized(), (0.2, 0.4, 0.4, 0.8).into());
source

pub fn normalize(&mut self)

Normalizes a Quat into its unit quaternion representation. This results in an an expensive square root calculation.

Examples
use gamemath::Quat;

let mut q: Quat = (1.0, 2.0, 2.0, 4.0).into();

q.normalize();

assert_eq!(q, (0.2, 0.4, 0.4, 0.8).into());
source

pub fn extract_matrix(&self) -> Mat4

Calculates and returns a Mat4 object representing the rotation of the calling Quat object.

Examples
use gamemath::{Vec3, Mat4, Quat};

let q = Quat::rotation(1.0, Vec3::new(1.0, 2.0, 3.0));

assert_eq!(q.extract_matrix(), (( 0.5731379,  0.74034876, -0.35127854, 0.0),
                                (-0.6090066,  0.67164457,  0.42190588, 0.0),
                                ( 0.5482918, -0.027879298, 0.8358222,  0.0),
                                ( 0.0,        0.0,         0.0,        1.0)).into());

Trait Implementations§

source§

impl Add for Quat

§

type Output = Quat

The resulting type after applying the + operator.
source§

fn add(self, right: Quat) -> Quat

Performs the + operation. Read more
source§

impl AddAssign for Quat

source§

fn add_assign(&mut self, right: Quat)

Performs the += operation. Read more
source§

impl Clone for Quat

source§

fn clone(&self) -> Quat

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Quat

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Quat

source§

fn default() -> Quat

Returns the “default value” for a type. Read more
source§

impl From<[f32; 4]> for Quat

source§

fn from(slice: [f32; 4]) -> Quat

Converts to this type from the input type.
source§

impl From<(f32, f32, f32, f32)> for Quat

source§

fn from(tuple: (f32, f32, f32, f32)) -> Quat

Converts to this type from the input type.
source§

impl From<Quat> for Mat4

source§

fn from(quat: Quat) -> Mat4

Converts to this type from the input type.
source§

impl From<Quat> for Vec4<f32>

source§

fn from(quat: Quat) -> Vec4<f32>

Converts to this type from the input type.
source§

impl From<Vec4<f32>> for Quat

source§

fn from(vec: Vec4<f32>) -> Quat

Converts to this type from the input type.
source§

impl From<f32> for Quat

source§

fn from(value: f32) -> Quat

Converts to this type from the input type.
source§

impl Mul for Quat

§

type Output = Quat

The resulting type after applying the * operator.
source§

fn mul(self, right: Quat) -> Quat

Performs the * operation. Read more
source§

impl MulAssign for Quat

source§

fn mul_assign(&mut self, right: Quat)

Performs the *= operation. Read more
source§

impl PartialEq for Quat

source§

fn eq(&self, other: &Quat) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Quat

source§

impl StructuralPartialEq for Quat

Auto Trait Implementations§

§

impl RefUnwindSafe for Quat

§

impl Send for Quat

§

impl Sync for Quat

§

impl Unpin for Quat

§

impl UnwindSafe for Quat

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.