You are on page 1of 1

3d Maths Cheat Sheet

Vectors
Vector Addition
The sum of 2 vectors completes the triangle.

v 0 = vRT (change facing and position)


v 0 = vT R (rotate around a point)

Matrix * Matrix
Each cell (row, col) in AB is:
P
n

A(row, 1) B(1, col) + + A(row, n) B(n, col)


i=1
Wherehn is dimensionality
ih
iof matrix.
h
i
a b
e f
ae + bg af + bh
AB =
=
c d
g h
ce + dg cf + dh

also a = c b and b = c a

Unit Vectors - Normalised Vectors


Used to represent a direction or normal. Length of 1.
~
= A
A
~
||A||

~ is the length or magnitude of A.


~
Where ||A||

Dot Product of 2 Vectors


Can be used
Pn to get the angle between 2 vectors.
~B
~ =
A
A B = A1 B1 + A2 B2 + + An Bn
i=1 i i

B)

The dot product returns a single scalar value. = arccos(A


= arccos(

~ B
~
A
~
~ )
||A||||
B||

Where arccos is inverse cosine cos1 .

Cross Product of 2 Vectors


Produces a vector perpendicular to the plane containing the 2
vectors.

"

"

a1
b1
a2 b3 a3 b2
a2
b2
a3 b1 a1 b3

=
a3
b3
a1 b2 a2 b1
To compute surface normals from 2 edges:
N = normalize (cross (A, B));

Matrices
Identity Matrix
All 0,"except the top-left
to bottom-right diagonal.
#
1 0 0
0 1 0
I3 =
0 0 1
if AB = I then A is the inverse of B and vice versa.

Matrix * Vector
"
#" #
a
d
g

b
e
h

c
f
i

x
y
z

"
=

ax + by + cz
dx + ey + f z
gx + hy + iz

(rows of A with columns of B)

Matrix Determinant
For a 2x2 or 3x3 matrix use the Rule of Sarrus; add products of
top-left to bottom-right diagonals, subtract products of opposite
diagonals.
#
"
a b c
d e f
Its determinant |M | is:
M =
g h i
|M | = aei + bf g + cdh ceg bdi af h
For 4x4 use Laplace Expansion; each top-row value * the 3x3 matrix
made of all other rows and columns:
|M | = aM1 bM2 + cM3 dM4
See http://www.euclideanspace.com/maths/algebra/matrix/
functions/determinant/fourD/index.htm

Matrix Transpose
Flip matrix over its main diagonal. In special case of orthonormal
xyz matrix then inverse is the transpose. Can use to switch
between
" row-major
# and column-major
"
# matrices.
a b c
a d g
d e f
b e h
M =
MT =
g h i
c f i
Use an inverse matrix to reverse its transformation, or to
transform relative to another object.
M M 1 = I Where I is the identity matrix.
If the determinant of a matrix is 0, then there is no inverse. The
inverse can be found by multiplying the determinant with a large
matrix of cofactors. For the long formula see
http://www.cg.info.hiroshima-cu.ac.jp/~miyazaki/
knowledge/teche23.html
Use the transpose of an inverse model matrix to transform
normals: n0 = n(M 1 )T

Homogeneous Matrices
Multiply affine transformations into a 4d matrix. Order is important.

Row-Order Homogeneous Matrix


Commonly used in Direct3D
maths libraries
Xx Xy Xz 0

 Y
Yy
Yz 0
v 0 = Vx Vy Vz 1 x
Zx Zy Zz 0
Tx
Ty
Tz 1

Commonly
used in OpenGLmaths
libraries

Xx Yx Zx Tx
Vx
Xy Yy Zy Ty Vy
v0 =
Xz Yz Zz Tz
Vz
0
0
0
1
1
v 0 = T Rv (change facing and position)
v 0 = RT v (rotate around a point)

Translation, Scaling, and Rotation

Matrix Inverse
"

Column-Order Homogeneous Matrix

1
0
0
0

0 0
1 0
column order T =
0 1
0 0

1
0
0
0 cos() sin()
Rx =
0 sin()
cos()
0
0
0

cos()
0 sin()
0
1
0
Ry =
sin() 0 cos()
0
0
0

cos() sin() 0
sin()
cos()
0
Rz =
0
0
1
0
0
0

View
Matrix

Tx
Sx
0
Ty
0
Sy
S=
0
0
Tz
0
0
1

0
0
(column-order)
0
1

0
0
(column-order)
0
1

0
0
(column-order)
0
1

0
0
Sz
0

0
0
0
1

Rx
Ry
Rz
Px
Ux
Uy
Uz
Py
V =
(column-order)
Fx Fy Fz Pz
0
0
0
1
Where U is a vector pointing up, F forward, and P is world position
of camera. Remember
transformations.
to multiply to combine

1 0
0
0
0 0 1 0
Birds-eye view V =
(multipy T 1 to move)
0 1
0
0
0 0
0
1
V = T 1 R1 (column order: orbit camera around a point)
V = R1 T 1 (column order: orient camera and change position)

Projection Matrix

Sx
0
0
0
0
Sy
0
0

P =
(column-order)
0
0
Sz Pz
0
0
1
0
Sx = (2 near)/(range aspect + range aspect)
Sy = near/range
Sz = (f ar + near)/(f ar near)
Pz = (2 f ar near)/(f ar near)
range = tan(f ov/2) near

revision 6. 10 Jun 2014


Dr Anton Gerdelan, gerdela@scss.tcd.ie, Trinity College Dublin, Ireland.
LATEX template from http://www.stdout.org/winston/latex/
Thanks Micha
el, Ken, Amoss, Veronica, and Stefan!

You might also like