ezEngine  Release 25.03
ezQuatTemplate< Type > Class Template Reference

Quaternions can be used to represent rotations in 3D space. More...

#include <Quat.h>

Public Types

using ComponentType = Type
 

Public Member Functions

 EZ_DECLARE_POD_TYPE ()
 
 ezQuatTemplate (Type x, Type y, Type z, Type w)
 For internal use. You should never construct quaternions this way.
 
void SetIdentity ()
 Sets the Quaternion to the identity.
 
void ReconstructFromMat3 (const ezMat3Template< Type > &m)
 Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring. More...
 
void ReconstructFromMat4 (const ezMat4Template< Type > &m)
 Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring. More...
 
void Normalize ()
 Normalizes the quaternion to unit length. ALL rotation-quaternions should be normalized at all times (automatically).
 
const Type * GetData () const
 Returns the data as an array.
 
Type * GetData ()
 Returns the data as an array.
 
void GetRotationAxisAndAngle (ezVec3Template< Type > &out_vAxis, ezAngle &out_angle, Type fEpsilon=ezMath::DefaultEpsilon< Type >()) const
 Returns the rotation-axis and angle, that this quaternion rotates around.
 
ezVec3Template< Type > GetVectorPart () const
 Returns the x,y,z components as a vector.
 
const ezMat3Template< Type > GetAsMat3 () const
 Returns the Quaternion as a matrix.
 
const ezMat4Template< Type > GetAsMat4 () const
 Returns the Quaternion as a matrix.
 
bool IsValid (Type fEpsilon=ezMath::DefaultEpsilon< Type >()) const
 Checks whether all components are neither NaN nor infinite and that the quaternion is normalized.
 
bool IsNaN () const
 Checks whether any component is NaN.
 
bool IsEqualRotation (const ezQuatTemplate &qOther, Type fEpsilon) const
 Determines whether this and qOther represent the same rotation. This is a rather slow operation. More...
 
void Invert ()
 Inverts the rotation, so instead of rotating N degrees around an axis, the quaternion will rotate -N degrees around its axis. More...
 
const ezQuatTemplate< Type > GetInverse () const
 Returns a quaternion that represents the negative / inverted rotation. E.g. the one that would rotate back to identity.
 
const ezQuatTemplate< Type > GetNegated () const
 Returns the Quaternion with all 4 components negated. This is not the same as the inverted rotation!
 
Type Dot (const ezQuatTemplate &rhs) const
 Returns the dot-product of the two quaternions (commutative, order does not matter).
 
ezVec3Template< Type > Rotate (const ezVec3Template< Type > &v) const
 Returns v rotated by the quaternion. Same as operator*.
 
void GetAsEulerAngles (ezAngle &out_x, ezAngle &out_y, ezAngle &out_z) const
 Converts the quaternion to Euler angles.
 

Static Public Member Functions

static const ezQuatTemplate< Type > MakeIdentity ()
 Static function that returns a quaternion that represents the identity rotation (none).
 
static ezQuatTemplate< Type > MakeFromElements (Type x, Type y, Type z, Type w)
 Sets the individual elements of the quaternion directly. Note that x,y,z do NOT represent a rotation axis, and w does NOT represent an angle. More...
 
static ezQuatTemplate< Type > MakeFromAxisAndAngle (const ezVec3Template< Type > &vRotationAxis, ezAngle angle)
 Creates a quaternion from a rotation-axis and an angle.
 
static ezQuatTemplate< Type > MakeShortestRotation (const ezVec3Template< Type > &vDirFrom, const ezVec3Template< Type > &vDirTo)
 Creates a quaternion, that rotates through the shortest arc from "vDirFrom" to "vDirTo". More...
 
static ezQuatTemplate< Type > MakeFromMat3 (const ezMat3Template< Type > &m)
 Creates a quaternion from the given matrix.
 
static ezQuatTemplate< Type > MakeSlerp (const ezQuatTemplate &qFrom, const ezQuatTemplate &qTo, Type t)
 Returns a quaternion that is the spherical linear interpolation of the other two.
 
static ezQuatTemplate< Type > MakeFromEulerAngles (const ezAngle &x, const ezAngle &y, const ezAngle &z)
 Sets the quaternion from Euler angles. More...
 

Public Attributes

Type x
 
Type y
 
Type z
 
Type w
 

Detailed Description

template<typename Type>
class ezQuatTemplate< Type >

Quaternions can be used to represent rotations in 3D space.

Quaternions are useful to represent 3D rotations, as they are smaller and more efficient than matrices and can be concatenated easily, without having the 'Gimbal Lock' problem of Euler Angles. Either use a full blown transformation (e.g. a 4x4 matrix) to represent a object, or use a Quaternion bundled with a position vector, if (non-uniform) scale is not required. Quaternions can also easily be interpolated (via Slerp). This implementation also allows to convert back and forth between Quaternions and Matrices easily.

Quaternions have no 'IsIdentical' or 'IsEqual' function, as there can be different representations for the same rotation, and it is rather difficult to check this. So to not convey any false notion of being equal (or rather unequal), those functions are not provided.

Member Function Documentation

◆ Invert()

template<typename Type >
EZ_FORCE_INLINE void ezQuatTemplate< Type >::Invert

Inverts the rotation, so instead of rotating N degrees around an axis, the quaternion will rotate -N degrees around its axis.

This modifies the quaternion in place. If you want to get the inverse as a copy, use GetInverse().

◆ IsEqualRotation()

template<typename Type >
bool ezQuatTemplate< Type >::IsEqualRotation ( const ezQuatTemplate< Type > &  qOther,
Type  fEpsilon 
) const

Determines whether this and qOther represent the same rotation. This is a rather slow operation.

Currently it fails when one of the given quaternions is identity (so no rotation, at all), as it tries to compare rotation axis' and angles, which is undefined for the identity quaternion (also there are infinite representations for 'identity', so it's difficult to check for it).

◆ MakeFromElements()

template<typename Type >
EZ_ALWAYS_INLINE ezQuatTemplate< Type > ezQuatTemplate< Type >::MakeFromElements ( Type  x,
Type  y,
Type  z,
Type  w 
)
static

Sets the individual elements of the quaternion directly. Note that x,y,z do NOT represent a rotation axis, and w does NOT represent an angle.

Use this function only if you have good understanding of quaternion math and know exactly what you are doing.

◆ MakeFromEulerAngles()

template<typename Type >
ezQuatTemplate< Type > ezQuatTemplate< Type >::MakeFromEulerAngles ( const ezAngle x,
const ezAngle y,
const ezAngle z 
)
static

Sets the quaternion from Euler angles.

Taken from here (yaw->pitch->roll, ZYX order or 3-2-1 order): https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles

◆ MakeShortestRotation()

template<typename Type >
ezQuatTemplate< Type > ezQuatTemplate< Type >::MakeShortestRotation ( const ezVec3Template< Type > &  vDirFrom,
const ezVec3Template< Type > &  vDirTo 
)
static

Creates a quaternion, that rotates through the shortest arc from "vDirFrom" to "vDirTo".

Note
This function will ALWAYS return a quaternion that rotates from one direction to another. If both directions are identical, it is the unit rotation (none). If they are exactly opposing, this will be ANY 180.0 degree rotation. That means the vectors will align perfectly, but there is no determine rotation for other points that might be rotated with this quaternion. If a main / fallback axis is needed to rotate points, you need to calculate such a rotation with other means.

◆ ReconstructFromMat3()

template<typename Type >
void ezQuatTemplate< Type >::ReconstructFromMat3 ( const ezMat3Template< Type > &  m)

Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring.

In skeletal animation it is possible that matrices with mirroring are used, that need to be converted to a proper quaternion, even though a rotation with mirroring can't be represented by a quaternion. This function reconstructs a valid quaternion from such matrices. Obviously the mirroring information gets lost, but it is typically not needed any further anway.

◆ ReconstructFromMat4()

template<typename Type >
void ezQuatTemplate< Type >::ReconstructFromMat4 ( const ezMat4Template< Type > &  m)

Reconstructs a rotation quaternion from a matrix that may contain scaling and mirroring.

See also
ReconstructFromMat3()

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