Struct Quaternion

Source
#[repr(C)]
pub struct Quaternion { pub x: f32, pub y: f32, pub z: f32, pub w: f32, }
Expand description

Represents a quaternion for efficient 3D rotations.

Quaternions are a four-dimensional complex number system that can represent rotations in 3D space. They are generally more efficient and numerically stable than rotation matrices, avoiding issues like gimbal lock.

A quaternion is stored as (x, y, z, w), where [x, y, z] is the “vector” part and w is the “scalar” part. For representing rotations, it should be a “unit quaternion” where x² + y² + z² + w² = 1.

Fields§

§x: f32

The x component of the vector part.

§y: f32

The y component of the vector part.

§z: f32

The z component of the vector part.

§w: f32

The scalar (real) part.

Implementations§

Source§

impl Quaternion

Source

pub const IDENTITY: Quaternion

The identity quaternion, representing no rotation.

Source

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

Creates a new quaternion from its raw components.

Note: This does not guarantee a unit quaternion. For creating rotations, prefer using from_axis_angle or other rotation-specific constructors.

Source

pub fn from_axis_angle(axis: Vec3, angle_radians: f32) -> Self

Creates a quaternion representing a rotation around a given axis by a given angle.

§Arguments
  • axis: The axis of rotation. It is recommended to pass a normalized vector.
  • angle_radians: The angle of rotation in radians.
Source

pub fn from_rotation_matrix(m: &Mat4) -> Self

Creates a quaternion from a 4x4 rotation matrix.

This method only considers the upper 3x3 part of the matrix for the conversion.

Source

pub fn magnitude_squared(&self) -> f32

Calculates the squared length (magnitude) of the quaternion.

Source

pub fn magnitude(&self) -> f32

Calculates the length (magnitude) of the quaternion.

Source

pub fn normalize(&self) -> Self

Returns a normalized version of the quaternion with a length of 1. If the quaternion has a near-zero magnitude, it returns the identity quaternion.

Source

pub fn conjugate(&self) -> Self

Computes the conjugate of the quaternion, which negates the vector part.

Source

pub fn inverse(&self) -> Self

Computes the inverse of the quaternion. For a unit quaternion, the inverse is equal to its conjugate.

Source

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

Computes the dot product of two quaternions.

Source

pub fn rotate_vec3(&self, v: Vec3) -> Vec3

Rotates a 3D vector by this quaternion.

Source

pub fn slerp(start: Self, end: Self, t: f32) -> Self

Performs a Spherical Linear Interpolation (Slerp) between two quaternions.

Slerp provides a smooth, constant-speed interpolation between two rotations, following the shortest path on the surface of a 4D sphere.

  • t - The interpolation factor, clamped to the [0.0, 1.0] range.

Trait Implementations§

Source§

impl Add for Quaternion

Source§

fn add(self, rhs: Self) -> Self::Output

Adds two quaternions component-wise. Note: This is not a standard rotation operation.

Source§

type Output = Quaternion

The resulting type after applying the + operator.
Source§

impl Clone for Quaternion

Source§

fn clone(&self) -> Quaternion

Returns a duplicate 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 Quaternion

Source§

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

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

impl Default for Quaternion

Source§

fn default() -> Self

Returns the identity quaternion, representing no rotation.

Source§

impl Mul<Vec3> for Quaternion

Source§

fn mul(self, rhs: Vec3) -> Self::Output

Rotates a Vec3 by this quaternion.

Source§

type Output = Vec3

The resulting type after applying the * operator.
Source§

impl Mul<f32> for Quaternion

Source§

fn mul(self, scalar: f32) -> Self::Output

Scales all components of the quaternion by a scalar.

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

impl Mul for Quaternion

Source§

fn mul(self, rhs: Self) -> Self::Output

Combines two rotations using the Hamilton product. Note that quaternion multiplication is not commutative.

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

impl MulAssign for Quaternion

Source§

fn mul_assign(&mut self, rhs: Self)

Combines this rotation with another.

Source§

impl Neg for Quaternion

Source§

fn neg(self) -> Self::Output

Negates all components of the quaternion.

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

impl PartialEq for Quaternion

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Quaternion

Source§

fn sub(self, rhs: Self) -> Self::Output

Subtracts two quaternions component-wise.

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

impl Copy for Quaternion

Source§

impl StructuralPartialEq for Quaternion

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.