-
Notifications
You must be signed in to change notification settings - Fork 512
Quaternion
A rotation represented as a four component vector modeled after the XNA Game Studio 4 (Microsoft.Xna.Framework.Quaternion
) math library.
A quaternion is a very efficient and compact method for working with 3D rotation. A quaternion is a 4-dimensional value and only has physical meaning when it's normalized. In computer graphics, they are used to represent 3D rotations as a 4-vector (i.e. 4 float values) instead of requiring a 3x3 matrix (i.e. 9 float values). They are extremely useful implementing cameras and animation where a quaternion can smoothly interpolate between 3D rotations while avoiding the gimbal lock problem common to Euler angles. They are also more compact the the 9 elements required for a 3D rotation matrix.
#include <SimpleMath.h>
using namespace DirectX::SimpleMath;
Quaternion q; // Creates the identity quaternion [0, 0, 0, 1]
Quaternion q(0, 0, 0, 1); // Creates a quaternion [0, 0, 0, 1]
Quaternion q( Vector3(0,0,0), 1); // Creates a quaternion [0, 0, 0, 1]
Quaternion q( Vector4(0,0,0,1) ); // Creates a quaternion [0, 0, 0, 1]
float arr[4] = { 0, 0, 0, 1 };
Quaternion q(arr); // Creates a quaternion [0, 0, 0, 1]
- x vector component of the quaternion
- y vector component of the quaternion
- z vector component of the quaternion
- w scalar component of the quaternion
-
Comparison operators:
==
and!=
-
Assignment operators:
=
,+=
,-=
,*=
,/=
-
Unary operators:
+
,-
-
Binary operators:
+
,-
,*
,/
-
Length
-
LengthSquared
-
Normalize: Normalizes the quaternion. Note that only normalized quaternions correspond to 3D rotations.
-
Conjugate: Computes the conjugate of a quaternion. This result is
Quaternion(-x, -y, -z, w)
. Note for a normalized quaternion, this is the inverse. -
Inverse: Computes the inverse of a quaternion including normalization.
-
Dot
-
RotateTowards
-
ToEuler: Computes rotation about y-axis (y), then x-axis (x), then z-axis (z). The return value is compatible with one of the overloads of
CreateFromYawPitchRoll
.
-
CreateFromAxisAngle: Creates a quaternion representing a rotation of a given angle (in radians) around an arbitrary axis vector.
-
CreateFromYawPitchRoll: Creates a quaternion representation a rotation about y-axis (yaw), then x-axis (pitch), then z-axis (roll) given in radians.
The original D3DXmath library took the rotations in the the Yaw, Pitch, Roll order and that order was replicated in XNA Game Studio. In DirectXMath, the order was normalized to Roll (X), Pitch (Y), Yaw (Z) for the parameters, but the application of the rotations is in the same order.
-
CreateFromRotationMatrix
-
Concatenate: Concatenates two quaternion rotations. Note:
Concatenate(q1,q2)
is equivalent toq2*q1
. -
Lerp: Linear interpolation
-
Slerp: Spherical linear interpolation
For interpolating between arbitrary 3D rotations, the slerp is the gold-standard. For small differences, however, lerp is much faster and almost identical.
-
FromToRotation
-
LookRotation
-
Angle: Computes the angle (in radians) between two quaternions (assuming the inputs are normalized).
- Identity: The identity quaternion [0, 0, 0, 1]
Quaternion can freely convert to and from a XMFLOAT4
and XMVECTOR
Quaternions and spatial rotation
Jonathan Blow, "Understanding Slerp, Then Not Using It", The Inner Product, April 2004 link
David Eberly, "Quaternion Algebra and Calculus" link
Ken Shoemake, "Quaternions", Department of Computer and Information Science, University of Pennsylvania link
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Xbox One
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- MinGW 12.2, 13.2
- CMake 3.20