Skip to content

Coding guide maths

Péter Kardos edited this page Jan 28, 2018 · 11 revisions

Table of Contents

Maths conventions

Coordinate spaces

Handedness X Y Z
World Right Right Forward Up
Camera Left Right Up Forward
NDC Left Right [-1; 1] Up [-1; 1] Forward [0; 1]

Matrix layout

SSE: row-major
Packed: row-major
Shader: row-major
Never explicitly specify layout in shaders!
Do not index matrices in a way that depends on layout (i.e. vector of first 4 elements in memory, m[0].x is forbidden)!

Multiplication order

The vector is followed by the matrix:

vector' = vector*MATRIX1*MATRIX2*MATRIX3
This way, the order in which transforms are applied aligns with left-to-right reading. Matrix functions are automatically configured to generate such matrices.

Quaternions

Mathematical quaternion multiplication is used, thus q = q2*q1 denotes the rotation q1 followed by the rotation q2.

Our matrices look like...

Scale:

Rotation X, Y and Z:

Translation:

View:

Projection:

DO NOT...

Do not explicitly assign matrix elements if you want to create a matrix that rotates or translates or similar. All these have handy functions in the math library. Something is missing? -> Open an issue on https://github.com/petiaccja/Mathter and contact Péter.