Claire's Game Engine
Full C++ Engine using OpenGL
Loading...
Searching...
No Matches
Quaternion Class Reference

Quaternion represents a rotation in 3D space using four components (x, y, z, w). It supports construction from axis-angle, Euler angles, or component values, and provides utility functions for normalization, interpolation, and conversion to matrices. More...

#include <Quaternion.h>

Public Member Functions

 Quaternion ()
 Default constructor initializes the quaternion to the identity quaternion. The identity quaternion represents no rotation and is defined as (0, 0, 0, 1).
 
 Quaternion (float inX, float inY, float inZ, float inW)
 Constructs a quaternion using four float values representing the components (x, y, z, w).
 
 Quaternion (const Vector3 &axis, float angle)
 Constructs a quaternion from an axis of rotation and an angle in radians. The axis is assumed to be normalized.
 
 Quaternion (float pitch, float yaw, float roll)
 Constructs a quaternion from pitch, yaw, and roll (Euler angles). The pitch is rotation about the X-axis, yaw about the Y-axis, and roll about the Z-axis.
 
void Set (float inX, float inY, float inZ, float inW)
 Sets the quaternion components explicitly. This method is intended for direct initialization, not for converting axis/angle or Euler angles.
 
void Conjugate ()
 Conjugates the quaternion, flipping the sign of the vector part (x, y, z).
 
void Normalize ()
 Normalizes the quaternion, ensuring it has a unit length (i.e., it represents a valid rotation). This method alters the current quaternion.
 
float LengthSqr () const
 Computes and returns the squared length of the quaternion. The squared length is the sum of the squares of the quaternion components.
 
float Length () const
 Computes and returns the length (magnitude) of the quaternion. This is the square root of the squared length.
 
float GetPitch () const
 Computes the pitch (rotation around the X-axis) of the quaternion.
 
float GetYaw () const
 Computes the yaw (rotation around the Y-axis) of the quaternion.
 
float GetRoll () const
 Computes the roll (rotation around the Z-axis) of the quaternion.
 
Quaternion operator+ (const Quaternion &p)
 
class Matrix4 AsMatrix () const
 Converts the quaternion to a 4x4 matrix representation. The resulting matrix can be used for transforming 3D vectors by the quaternion's rotation.
 

Static Public Member Functions

static float Dot (const Quaternion &a, const Quaternion &b)
 Computes the dot product between two quaternions.
 
static Quaternion Normalize (const Quaternion &q)
 Returns a normalized version of the given quaternion. Normalization scales the quaternion so that its length is 1.
 
static Quaternion Lerp (const Quaternion &a, const Quaternion &b, float f)
 Performs linear interpolation (Lerp) between two quaternions.
 
static Quaternion Slerp (const Quaternion &a, const Quaternion &b, float f)
 Performs spherical linear interpolation (Slerp) between two quaternions.
 
static Quaternion Concatenate (const Quaternion &q, const Quaternion &p)
 Concatenates two quaternions (i.e., applies one rotation after the other). This is equivalent to multiplying the two quaternions.
 
static Quaternion LookAt (const Vector3 &forward, const Vector3 &up)
 Creates a quaternion representing the rotation to "look at" a target point. The resulting quaternion aligns the forward vector with the target direction, with the given up vector providing the direction of the "up" axis.
 
static Quaternion LookAt (const Vector3 &origin, const Vector3 &target, const Vector3 &up)
 Creates a quaternion representing the rotation to "look at" a target point from an origin. This is equivalent to creating a "look at" rotation from the origin to the target, with the up vector.
 

Public Attributes

float x
 
float y
 
float z
 
float w
 

Static Public Attributes

static const Quaternion Identity
 Identity quaternion: represents no rotation (used as a reference for default state).
 

Detailed Description

Quaternion represents a rotation in 3D space using four components (x, y, z, w). It supports construction from axis-angle, Euler angles, or component values, and provides utility functions for normalization, interpolation, and conversion to matrices.

Constructor & Destructor Documentation

◆ Quaternion() [1/4]

Quaternion::Quaternion ( )
inline

Default constructor initializes the quaternion to the identity quaternion. The identity quaternion represents no rotation and is defined as (0, 0, 0, 1).

◆ Quaternion() [2/4]

Quaternion::Quaternion ( float inX,
float inY,
float inZ,
float inW )
explicit

Constructs a quaternion using four float values representing the components (x, y, z, w).

Parameters
inXThe x component of the quaternion's vector part.
inYThe y component of the quaternion's vector part.
inZThe z component of the quaternion's vector part.
inWThe scalar component of the quaternion.

◆ Quaternion() [3/4]

Quaternion::Quaternion ( const Vector3 & axis,
float angle )
explicit

Constructs a quaternion from an axis of rotation and an angle in radians. The axis is assumed to be normalized.

Parameters
axisThe normalized axis of rotation.
angleThe rotation angle in radians.

◆ Quaternion() [4/4]

Quaternion::Quaternion ( float pitch,
float yaw,
float roll )
explicit

Constructs a quaternion from pitch, yaw, and roll (Euler angles). The pitch is rotation about the X-axis, yaw about the Y-axis, and roll about the Z-axis.

Parameters
pitchThe rotation angle around the X-axis in radians.
yawThe rotation angle around the Y-axis in radians.
rollThe rotation angle around the Z-axis in radians.

Member Function Documentation

◆ AsMatrix()

Matrix4 Quaternion::AsMatrix ( ) const

Converts the quaternion to a 4x4 matrix representation. The resulting matrix can be used for transforming 3D vectors by the quaternion's rotation.

Returns
A Matrix4 object representing the quaternion as a rotation matrix.

◆ Concatenate()

Quaternion Quaternion::Concatenate ( const Quaternion & q,
const Quaternion & p )
static

Concatenates two quaternions (i.e., applies one rotation after the other). This is equivalent to multiplying the two quaternions.

Parameters
qThe first quaternion (rotation to apply first).
pThe second quaternion (rotation to apply after q).
Returns
A new quaternion that represents the combined rotation.

◆ Conjugate()

void Quaternion::Conjugate ( )

Conjugates the quaternion, flipping the sign of the vector part (x, y, z).

◆ Dot()

float Quaternion::Dot ( const Quaternion & a,
const Quaternion & b )
static

Computes the dot product between two quaternions.

Parameters
aThe first quaternion.
bThe second quaternion.
Returns
The dot product between quaternions a and b.

◆ GetPitch()

float Quaternion::GetPitch ( ) const

Computes the pitch (rotation around the X-axis) of the quaternion.

Returns
The pitch angle in radians.

◆ GetRoll()

float Quaternion::GetRoll ( ) const

Computes the roll (rotation around the Z-axis) of the quaternion.

Returns
The roll angle in radians.

◆ GetYaw()

float Quaternion::GetYaw ( ) const

Computes the yaw (rotation around the Y-axis) of the quaternion.

Returns
The yaw angle in radians.

◆ Length()

float Quaternion::Length ( ) const

Computes and returns the length (magnitude) of the quaternion. This is the square root of the squared length.

Returns
The length of the quaternion.

◆ LengthSqr()

float Quaternion::LengthSqr ( ) const

Computes and returns the squared length of the quaternion. The squared length is the sum of the squares of the quaternion components.

Returns
The squared length of the quaternion.

◆ Lerp()

Quaternion Quaternion::Lerp ( const Quaternion & a,
const Quaternion & b,
float f )
static

Performs linear interpolation (Lerp) between two quaternions.

Parameters
aThe starting quaternion.
bThe ending quaternion.
fThe interpolation factor (from 0 to 1).
Returns
The interpolated quaternion.

◆ LookAt() [1/2]

Quaternion Quaternion::LookAt ( const Vector3 & forward,
const Vector3 & up )
static

Creates a quaternion representing the rotation to "look at" a target point. The resulting quaternion aligns the forward vector with the target direction, with the given up vector providing the direction of the "up" axis.

Parameters
forwardThe forward vector to align the quaternion to.
upThe up vector, typically (0, 1, 0).
Returns
A quaternion that rotates an object to face the target direction.

◆ LookAt() [2/2]

Quaternion Quaternion::LookAt ( const Vector3 & origin,
const Vector3 & target,
const Vector3 & up )
static

Creates a quaternion representing the rotation to "look at" a target point from an origin. This is equivalent to creating a "look at" rotation from the origin to the target, with the up vector.

Parameters
originThe starting point (usually the camera or object position).
targetThe point to look at (the target position).
upThe up vector (usually (0, 1, 0)).
Returns
A quaternion representing the look-at rotation.

◆ Normalize() [1/2]

void Quaternion::Normalize ( )

Normalizes the quaternion, ensuring it has a unit length (i.e., it represents a valid rotation). This method alters the current quaternion.

◆ Normalize() [2/2]

Quaternion Quaternion::Normalize ( const Quaternion & q)
static

Returns a normalized version of the given quaternion. Normalization scales the quaternion so that its length is 1.

Parameters
qThe quaternion to normalize.
Returns
A new quaternion that is the normalized version of q.

◆ operator+()

Quaternion Quaternion::operator+ ( const Quaternion & p)
inline

◆ Set()

void Quaternion::Set ( float inX,
float inY,
float inZ,
float inW )

Sets the quaternion components explicitly. This method is intended for direct initialization, not for converting axis/angle or Euler angles.

Parameters
inXThe x component of the quaternion.
inYThe y component of the quaternion.
inZThe z component of the quaternion.
inWThe scalar component of the quaternion.

◆ Slerp()

Quaternion Quaternion::Slerp ( const Quaternion & a,
const Quaternion & b,
float f )
static

Performs spherical linear interpolation (Slerp) between two quaternions.

Parameters
aThe starting quaternion.
bThe ending quaternion.
fThe interpolation factor (from 0 to 1).
Returns
The interpolated quaternion.

Member Data Documentation

◆ Identity

const Quaternion Quaternion::Identity
static

Identity quaternion: represents no rotation (used as a reference for default state).

◆ w

float Quaternion::w

◆ x

float Quaternion::x

◆ y

float Quaternion::y

◆ z

float Quaternion::z

The documentation for this class was generated from the following files: