Skip to content

Quaternion

Chuck Walbourn edited this page Oct 23, 2021 · 26 revisions

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.

Header

#include <SimpleMath.h>

Initialization

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]

Fields

  • x vector component of the quaternion
  • y vector component of the quaternion
  • z vector component of the quaternion
  • w scalar component of the quaternion

Methods

  • 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

Statics

  • 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 to q2*q1.

For interpolating between arbitrary 3D rotations, the slerp is the gold-standard. For small differences, however, lerp is much faster and almost identical.

Constants

  • Identity: The identity quaternion [0, 0, 0, 1]

Remark

Quaternion can freely convert to and from a XMFLOAT4 and XMVECTOR

Further Reading

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

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v19
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally