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
impl Quat
sourcepub fn identity() -> Quat
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());
sourcepub fn rotation(radians: f32, axis: Vec3<f32>) -> Quat
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());
sourcepub fn rotated(&self, radians: f32, axis: Vec3<f32>) -> Quat
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());
sourcepub fn rotate(&mut self, radians: f32, axis: Vec3<f32>)
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());
sourcepub fn length_squared(&self) -> f32
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);
sourcepub fn length(&self) -> f32
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);
sourcepub fn normalized(&self) -> Quat
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());
sourcepub fn normalize(&mut self)
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());
sourcepub fn extract_matrix(&self) -> Mat4
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 AddAssign for Quat
impl AddAssign for Quat
source§fn add_assign(&mut self, right: Quat)
fn add_assign(&mut self, right: Quat)
+=
operation. Read moresource§impl MulAssign for Quat
impl MulAssign for Quat
source§fn mul_assign(&mut self, right: Quat)
fn mul_assign(&mut self, right: Quat)
*=
operation. Read more