Skip to content
Chuck Walbourn edited this page Oct 23, 2021 · 19 revisions

A four by four row-major matrix modeled after the XNA Game Studio 4 (Microsoft.Xna.Framework.Matrix) math library.

A 4x4 matrix is commonly used in 3D graphics for 2D and 3D transformations.

Header

#include <SimpleMath.h>

Initialization

using namespace DirectX::SimpleMath;

Matrix m;                  // Creates an identity matrix
Matrix m(1, 2, 3, 4,
         5, 6, 7, 8,
         9, 10, 11, 12,
         13, 14, 15, 16);  // Creates a matrix [1 2 3 4
                           //                   | 5 6 7 8
                           //                   | 9 10 11 12
                           //                   | 13 14 15 16 ]
Matrix m( Vector3(1,2,3),
          Vector3(4,5,6),
          Vector(7,8,9) ); // Creates a matrix [1 2 3 0
                           //                   | 4 5 6 0
                           //                   | 7 8 9 0
                           //                   | 0 0 0 1 ]
Matrix m( Vector4(1, 2, 3, 4),
          Vector4(5, 6, 7, 8),
          Vector4(9, 10, 11, 12),
          Vector4(13, 14, 15, 16) ); // Creates a matrix [1 2 3 4
                                     //                   | 5 6 7 8
                                     //                   | 9 10 11 12
                                     //                   | 13 14 15 16 ]
float arr[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
Matrix m( arr );           // Creates a matrix [1 2 3 0
                           //                   | 4 5 6 0
                           //                   | 7 8 9 0
                           //                   | 0 0 0 1 ]

Fields

  • _11, _12, _13, _14: First row of matrix
  • _21, _22, _23, _24: Second row of matrix
  • _31, _32, _33, _34: Third row of matrix
  • _41, _42, _43, _44: Fourth row of matrix

Properties

  • Up: The up direction vector from the matrix
  • Down: The down direction vector from the matrix
  • Right: The right direction vector from the matrix
  • Left: The left direction vector from the matrix
  • Forward: The forward direction vector in right-handed (RH) coordinates from the matrix
  • Backward: The backward direction vector in right-handed (RH) coordinates from the matrix
  • Translation: Returns the translation in the matrix

Methods

  • Comparison operators: == and !=

  • Assignment operators: =, +=, -=, *=, /=

  • Unary operators: +, -

  • Binary operators: +, -, *, /

  • Decompose: Decompose the matrix into rotation, scaling, and translation components

  • Transpose

  • Invert

  • Determinant

Statics

  • CreateBillboard: Creates a spherical billboard that rotates around a specified object position

  • CreateConstrainedBillboard: Creates a cylindrical billboard that rotates around a specified axis

  • CreateTranslation

  • CreateScale

  • CreateRotationX, CreateRotationY, CreateRotationZ

  • CreateFromAxisAngle

  • CreatePerspective, CreatePerspectiveFieldOfView, CreatePerspectiveOffCenter: Creates perspective projection in right-handed (RH) coordinates

  • CreateOrthographic, CreateOrthographicOffCenter: Creates orthographic projection in right-handed (RH) coordinates

  • CreateLookAt: Creates a look-at matrix in right-handed (RH) coordinates

  • CreateWorld

  • CreateFromQuaternion

  • CreateFromYawPitchRoll

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).

  • CreateShadow: Creates transform that flattens geometry into a specified Plane as if casting a shadow from a specified light source

  • CreateReflection: Creates transform that reflects the coordinate system about a specified Plane.

  • Lerp: Linearly interpolates two matrices element-wise (useful for blending transformations).

  • Transform

Constants

  • Identity: An identity matrix

Remark

Matrix can freely convert to and from a XMFLOAT4X4 and XMMATRIX.

You can copy or assign a Matrix from a XMFLOAT3X3 or XMFLOAT4X3.

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 - v20
  • MinGW 12.2, 13.2
  • CMake 3.21

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally