-
Notifications
You must be signed in to change notification settings - Fork 526
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 in animation where a quaternion can smoothly interpolate between 3D rotations.
#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)
. - Inverse
- Dot
-
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 from a yaw/pitch/roll.
The original D3DXmath library took the rotations in the the Yaw, Pitch, Roll order and was replicated in XNA Game Studio. In DirectXMath, the order was normalized to Roll (X), Pitch (Y), Yaw (Z).
-
CreateFromRotationMatrix
-
Lerp: Linear interpolation
-
Slerp: Spherical linear interpolation
-
Concatenate: Concatenates two quaternion rotations. Note:
Concatenate(q1,q2)
is equivalent toq2*q1
.
For interpolating between arbitrary 3D rotations, the slerp is the gold-standard. For small differences, however, lerp is much faster and almost identical.
- 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 opencode@microsoft.com 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 - v19
- MinGW 12.2, 13.2
- CMake 3.20