#[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
impl Quaternion
Sourcepub const IDENTITY: Quaternion
pub const IDENTITY: Quaternion
The identity quaternion, representing no rotation.
Sourcepub fn new(x: f32, y: f32, z: f32, w: f32) -> Self
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.
Sourcepub fn from_axis_angle(axis: Vec3, angle_radians: f32) -> Self
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.
Sourcepub fn from_rotation_matrix(m: &Mat4) -> Self
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.
Sourcepub fn magnitude_squared(&self) -> f32
pub fn magnitude_squared(&self) -> f32
Calculates the squared length (magnitude) of the quaternion.
Sourcepub fn normalize(&self) -> Self
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.
Sourcepub fn conjugate(&self) -> Self
pub fn conjugate(&self) -> Self
Computes the conjugate of the quaternion, which negates the vector part.
Sourcepub fn inverse(&self) -> Self
pub fn inverse(&self) -> Self
Computes the inverse of the quaternion. For a unit quaternion, the inverse is equal to its conjugate.
Sourcepub fn rotate_vec3(&self, v: Vec3) -> Vec3
pub fn rotate_vec3(&self, v: Vec3) -> Vec3
Rotates a 3D vector by this quaternion.
Sourcepub fn slerp(start: Self, end: Self, t: f32) -> Self
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
impl Add for Quaternion
Source§impl Clone for Quaternion
impl Clone for Quaternion
Source§fn clone(&self) -> Quaternion
fn clone(&self) -> Quaternion
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Quaternion
impl Debug for Quaternion
Source§impl Default for Quaternion
impl Default for Quaternion
Source§impl Mul<Vec3> for Quaternion
impl Mul<Vec3> for Quaternion
Source§impl Mul<f32> for Quaternion
impl Mul<f32> for Quaternion
Source§impl Mul for Quaternion
impl Mul for Quaternion
Source§impl MulAssign for Quaternion
impl MulAssign for Quaternion
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Combines this rotation with another.