Vectors

||\vec{v}|| = \sqrt{x^2 + y^2 + z^2}Euclidean Length/Magnitude
\vec{v}_1\cdot\vec{v}_2 = x_1x_2 + y_1y_2 + z_1z_2
= ||\vec{v}_1|| ||\vec{v}_2||\cos\theta
= 0 if \vec{v}_1,\vec{v}_2 perpindicular/90 degrees
Dot Product
\vec{v}_1\times\vec{v}_2 = (y_1z_2 - z_1y_2, -(x_1z_2 - z_1x_2), x_1y_2 - y_1x_2)
||\vec{v}\times\vec{w}|| = ||\vec{v}|| ||\vec{w}||\sin\theta
\vec{v}\times\vec{w} = (0,0,0) if \vec{v},\vec{w} are parallel/colinear
Cross Product
proj_\vec{w}\vec{v} = \frac{\vec{v}\cdot\vec{w}}{\vec{w}\cdot\vec{w}}\vec{w}Projection of v onto w
perp_\vec{w}\vec{v} = \vec{v} - proj_\vec{w}\vec{v}Perpindicular of v onto w
(x,y,z,1)Point
(x,y,z,0)Vector

Lines & Planes

L(t) = P + tv or S + tv
= L(t) = P + t(Q-P) for 2 points
Line equation
d = ||perp_\vec{v}(S-P)|| = ||(S-P)-\frac{(S-P) \cdot v}{v \cdot v}v||Distance of point S to line L(t)
(P-P_0)\cdot\vec{n} = 0
\vec{n} = (P_1-P_0)\times(P_2-P_0) if given 3 points
Plane Equation
d = abs(\frac{(P_1 - P_0) \cdot \vec{n}}{||\vec{n}||})
without abs, if d>0, p1 on same side, if d<0, opposite side
Distance of point P1 to plane
\vec{v}\cdot\vec{n} = 0Line is parallel to plane (v from line, n from plane)
(S-P_0)\cdot\vec{n} = 0Line in plane if parallel and satisfies plane equation
t' = \frac{(P_0-S)\cdot\vec{n}}{\vec{v}\cdot\vec{n}}Intersection point t of line L(t) which intersects with plane (if not parallel)
\vec{w}=P_1-P_0,\vec{n}'=\vec{v}\times\vec{w}=k\vec{n}
k < 0: P1 right of L(t), k > 0: P1 left of L(t), else P1 in L(t)
Left-right of point P1, parallel vec v, and plane (P0,n)

Transforms

T(u+v) = T(u)+T(v)
T(au) = aT(u)
Linear transformation definition
\begin{pmatrix} 1 & 0 & 0 & x \\ 0 & 1 & 0 & y \\ 0 & 0 & 1 & z \\ 0 & 0 & 0 & 1 \end{pmatrix}Translation matrix
\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Rotation matrix about x axis
\begin{pmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Rotation matrix about y axis
\begin{pmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Rotation matrix about z axis
\begin{pmatrix} tx^2+c & txy-sz & txz+sy & 0 \\ txy+sz & ty^2+c & tyz-sx & 0 \\ txz-sy & tyz+sx & tz^2+c & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Rotation matrix about
normalized r=(x,y,z), c=cos(θ), s=sin(θ), t=1-cos(θ)
R_{\vec{r},\theta}(\vec{v}) = \vec{v}\cos\theta + (1-\cos\theta)(\vec{v}\cdot\vec{r})\vec{r} + (\vec{r}\times\vec{v})\sin\thetaRodriguez rotation formaula about normalized axis r=(x,y,z)
\begin{pmatrix} a & 0 & 0 & 0 \\ 0 & b & 0 & 0 \\ 0 & 0 & c & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Scale Matrix
\begin{pmatrix} 1-2x^2 & -2xy & -2xz & 0 \\ -2xy & 1-2y^2 & -2yz & 0 \\ -2xz & -2yz & 1-2z^2 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}Reflection matrix about plane normalized normal n=(x,y,z)
Rf_{\vec{n}}(\vec{v}) = \vec{v} - 2(\vec{v}\cdot\vec{n})\vec{n}Reflection function about normalized n=(x,y,z)
M = \begin{pmatrix}I & C \\ 0^T & 1\end{pmatrix} \begin{pmatrix}R_{r,\theta} & 0 \\ 0^T & 1\end{pmatrix} \begin{pmatrix}I & -C \\ 0^T & 1\end{pmatrix}Transform when not at origin, relative to point C
W = TRS
W^{-1} = (TRS)^{-1} = S^{-1}R^{-1}T^{-1}
Transformation order

Matrices

det(A) = |A| = a_{11}\begin{bmatrix} a_{22} & a_{23} \\ a_{32} & a_{33} \end{bmatrix} - a_{12}\begin{bmatrix} a_{21} & a_{23} \\ a_{31} & a_{33} \end{bmatrix} + a_{13}\begin{bmatrix} a_{21} & a_{22} \\ a_{31} & a_{32} \end{bmatrix}Determinant of 3x3 matrix A
|A| = a_{11}\begin{bmatrix} a_{22} & a_{23} & a_{24} \\ a_{32} & a_{33} & a_{34} \\ a_{42} & a_{43} & a_{44} \end{bmatrix} - a_{12}\begin{bmatrix} a_{21} & a_{23} & a_{24} \\ a_{31} & a_{33} & a_{34} \\ a_{41} & a_{43} & a_{44} \end{bmatrix} + a_{13}\begin{bmatrix} a_{21} & a_{22} & a_{24} \\ a_{31} & a_{32} & a_{34} \\ a_{41} & a_{42} & a_{44} \end{bmatrix} - a_{14}\begin{bmatrix} a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ a_{41} & a_{42} & a_{43} \end{bmatrix}Determinant of 4x4 matrix A
\begin{pmatrix} a\begin{pmatrix}r_{00}\\r_{10}\\r_{20}\end{pmatrix} & b\begin{pmatrix}r_{01}\\r_{11}\\r_{21}\end{pmatrix} & c\begin{pmatrix}r_{02}\\r_{12}\\r_{22}\end{pmatrix} & \begin{pmatrix}t_x\\t_y\\t_z\end{pmatrix} \\ 0 & 0 & 0 & 1 \end{pmatrix}Combined TRS matrix form
W'_B = (W_A)^{-1}W_BWorld matrix Wb relative to A's local space

Euler Angles & Quaternions

R = R_xR_yR_z = \begin{pmatrix} \cos\theta_y\cos\theta_z & -\cos\theta_y\sin\theta_z & \sin\theta_y \\ ... & ... & -\sin\theta_x\cos\theta_y \\ ... & ... & \cos\theta_x\cos\theta_y \end{pmatrix}Euler Angle Rotation Matrix
\vec{q}=(w,\vec{v})=(w,x,y,z)
\vec{q}=(\cos\frac{\theta}{2},\vec{r}\sin\frac{\theta}{2}), r = rotation axis
Quaternion Representation
\vec{q}_1\cdot\vec{q}_2 = w_1w_2 + \vec{v}_1\cdot\vec{v}_2Quaternion dot product
||\vec{q}||=\sqrt{\vec{q}\cdot\vec{q}}Quaternion Length/Magnitude
\vec{q}' = \frac{\vec{q}}{||\vec{q}||}Normalize Quaternion
\vec{q}_1\vec{q}_2 = (w_1w_2 - \vec{v}_1\cdot\vec{v}_2, w_1\vec{v}_2 + w_2\vec{v}_1 + \vec{v}_1\times\vec{v}_2)Quaternion Multiplication
\vec{q}^{-1} = \frac{1}{||\vec{q}||^2}(w,-\vec{v})Quaternion Inverse
\vec{q}=(0,\vec{v})Quaternion form of vector v
R_q(p) = \vec{q}\vec{p}\vec{q}^{-1}Rotating vector p=(0,v) using quaternion
\theta=2\cos^{-1}(w), ||\vec{v}||=\sqrt{1-w^2},\vec{r}=\frac{\vec{v}}{||\vec{v}||}Quaternion to axis-angle
M_q = \begin{pmatrix} 1-2y^2-2z^2 & 2xy-2wz & 2xz-2wy \\ 2xy+2wz & 1-2x^2-2z^2 & 2yz-2wx \\ 2xz-2wy & 2yz+2wx & 1-2x^2-2y^2 \end{pmatrix}Quaternion to rotation matrix

Cameras/Rendering pipeline matrices

Cpos = position
Tpos = target position/lookat position
vdir = normalize(Tpos - Cpos)
up dir given
left-hand camera: local z vec = vfwd = vdir
right-hand camera: local z vec = vfwd = -vdir
Camera vectors/view direction
C_{view-to-world} = \begin{pmatrix} v_{side} & v_{up} & v_{fwd} & Cpos \\ 0 & 0 & 0 & 1 \end{pmatrix}
v_{side} = normalize(up \times v_{fwd})
v_{up} = normalize(v_{fwd} \times v_{side})
Camera view-to-world matrix
C_{world-to-view} = C_{view-to-world}^{-1} = \begin{pmatrix} R^T & -(R^T C_{pos}) \\ 0^T & 1 \end{pmatrix} Camera world-to-view matrix
M_{ortho} = \begin{pmatrix} 2/(r-l) & 0 & 0 & -(r+l)/(r-l) \\ 0 & 2/(t-b) & 0 & -(t+b)/(t-b) \\ 0 & 0 & -2/(f-n) & -(f+n)/(f-n) \\ 0 & 0 & 0 & 1 \end{pmatrix} Orthographic projection matrix (right-hand camera using ndc -1,1)
M_{persp} = \begin{pmatrix} d/a & 0 & 0 & 0 \\ 0 & d & 0 & 0 \\ 0 & 0 & -(f+n)/(f-n) & (-2fn)/(f-n) \\ 0 & 0 & -1 & 0 \end{pmatrix} Perspective projection matrix (right-hand camera, left-hand ndc -1,1). d = cot(theta/2), a = aspect ratio = width/height
After multiplying camera-space point by this, remember to divide by w to get final 3d point in NDC coordinates
M_{ndc-to-screen} = \begin{pmatrix} w/2 & 0 & 0 & w/2 \\ 0 & -h/2 & 0 & h/2 \\ 0 & 0 & d_z/2 & d_z/2 \\ 0 & 0 & 0 & 1 \end{pmatrix} ndc to screen matrix, dz = depth used

Backface Culling

v_{los} \cdot n < 0 \rightarrow visible, else culled Culling given face normal n and line of sight direction vlos
n = (b-a)\times(c-a) Normal n calculation given triangle with points a,b,c
v_{los} = p1 - C_{pos} vlos calc using perspective projection camera, triangle world space points (p1,p2,p3)
v_{los} = v_{dir} vlos calc using orthographic camera

Object Picking

Near plane p0 = C_{view-to-world}M_{persp}^{-1}M_{screen-to-ndc}\begin{pmatrix}x\\y\\0\\1\end{pmatrix}
Far plane p1 = C_{view-to-world}M_{persp}^{-1}M_{screen-to-ndc}\begin{pmatrix}x\\y\\d_z\\1\end{pmatrix}
Screen space to world space for object picking.
Divide by w as the last step (after multiplying by Mview-to-world)

Collision Detection (Bsphere,AABB,OBB)

length(P-C) < r then P inside, else outside Point P and Bsphere=(C,r) collision, Bsphere C and r both in world space
length(C-C') < r + r' then intersects, else no intersection Bsphere(C,r) and BSphere(C',r') collision, both Bspheres C/r in world space
min.x < P.x < max.x AND
min.y < P.y < max.y AND
min.z < P.z < max.z then P inside A, else not
AABB-point P intersection
Test if intervals (a,b) and (c,d) overlap:
if (b<c) or (d<a) then return false (no overlap)
else return true (overlap)
Interval overlap test
Clamping c to interval (a,b):
if c < a return a
else if b < c return b
else return c
Clamping function on scalar to interval.
To clamp point between (min,max) apply clamp to x,y,z
p' = clamp(C',(min,max))
test p' point intersection with Bsphere
Bsphere-AABB collision test, using world-space bpshere C'/r'
Compute W^{-1} and L'_N = W^{-1} C'_N
Compute Q = L'_N clamped to OBB local space (min,max)
Compute Q' = WQ clamped point in world space
Test if point Q' is in Bsphere
OBB using matrix W and Bsphere(C'_N,r_N) intersection (C'_N world space)
calc \vec{v}' = W^{-1} \vec{v}
calc proj_max = (|v'_x a| + |v'_y b| + |v'_z c|)/||\vec{v}||
return s2 proj_max
Max obb projection on axis v. OBB has tform W, (a,b,c) = max-min,
s = length of first column of W (assuming uniform scale).
Proj1 = Max OBB1 projection on Axis v
Proj2 = Max OBB2 projection on Axis v
D = |(C2-C1) \cdot \vec{v}| / ||\vec{v}||
if D ≤ Proj1 + Proj2 then return true (overlap), else false (no overlap)
OBB,OBB axis overlap along axis v. If any axis returns no overlap,
then no intersection; else intersection. C1,C2 in world space
Treat AABB as OBB then do OBB/OBB intersection AABB vs OBB intersection

Collision Detection (Plane,triangle,point,line,ray,segment)

Q' = P + \frac{v \cdot (Q-P)}{v \cdot v}v Point Q' on line (L(t) = P + tv) that is closest to given P given Q
d = ||Q-P||^2 - \frac{(\vec{v} \cdot (Q-P))^2}{\vec{v} \cdot \vec{v}} Distance from point Q to line L(t) = P + tv
S(t) = P_0 + t(P_1 - P_0) Line segment definition, 0 ≤ t ≤ 1
t = \frac{\vec{v} \cdot (Q - P_0)}{\vec{v} \cdot \vec{v}}
if t < 0 then return Q' = S(0) = P_0
if t > 1 then return Q' = S(1) = P_1
else return Q' = S(t)
Closest point Q' on segment S(t) to point Q.
distance from point Q to S(t) is ||Q' - Q||.
R(t) = P_0 + t\vec{v} ray definition, t ≥ 0
t = \frac{\vec{v} \cdot (Q - P_0)}{\vec{v} \cdot \vec{v}}
if t < 0 return R(0) = P_0
else return R(t)
Closest point on ray R(t) to point Q.
t = (\vec{v} \cdot (C-P)) / (\vec{v} \cdot \vec{v})
if t < 0 then t = 0
Q = P + t\vec{v}
return ||C - Q||^2 \less r^2
Ray(P,v) Bsphere(C,r) intersection
C_1 = P_1
C_2 = P_2 + \frac{\vec{v}_2\cdot(P_1-P_2)}{\vec{v}_2 \cdot \vec{v}_2}\vec{v}_2
Line-line closest points if L_1(t), L_2(t) are parallel (b^2 - ac == 0 from below)
if C_1 = C_2 then lines are the same (coincident)
a = \vec{v}_1 \cdot \vec{v}_1, b = \vec{v}_1 \cdot \vec{v}_2, c = \vec{v}_2 \cdot \vec{v}_2
d = (P_1 - P_2) \cdot \vec{v}_1, e = (P_1 - P_2) \cdot \vec{v}_2
t1 = (cd-be)/(b^2 - ac), t2 = (db - ae)/(b^2 - ac)
Variables for line-line intersection
C_1 = L_1(t_1) = P_1 + t1\vec{v}_1, C_2 = L_2(t_2) = P_2 + t_2\vec{v}_2 Line-line closest points if L_1(t), L_2(t) NOT parallel
Distance d = ||C_1 - C_2|| Line-line intersection/distance between closest points C_1, C_2 from above
if v_x = 0:
if (P_0x \less min_x || P_0x > max_x) then return false
else [s_x,t_x] = [-inf,inf].
else if v_x != 0:
a = (min_x - P_0x)/v_x, b = (max_x - P_0x)/v_x
s_x = smallest of (a,b) t_x = largest of (a,b)
(repeat above for y,z axis)
if [s_x,t_x] overlaps [s_y,t_y] AND
[s_y,t_y] overlaps [s_z,t_z] AND
[s_z,t_z] overlaps [s_x,t_x] return true
Line-AABB intersection fro L(P_0,v) and AABB(Max,Min)
Convert line to OBB's local space where the OBB is AABB and use above method Line-OBB intersection
d = |\frac{(C-P_0) \cdot \vec{n}}{||\vec{n}||}|
if d < r then intersection, else no intersection
Plane-Bsphere intersection. Without abs, if = 0 then C in plane,
if d > 0 then C above plane (same side as n),
if d < 0 then C below plane (opposite side as n).
t' = ((P_0 - S_1) \cdot n) / ((S_2 - S_1) \cdot n)
if 0 ≤ t' ≤ 1 then intersection, else no intersection
Plane P0,n segment S(t) = S1 + t(S2 - S1) collision method 1
d_1 = (S_1 - P_0) \cdot \vec{n}, d_2 = (S_2 - P_0) \cdot \vec{n}
if (d_1 < 0 and d_2 > 0) OR (d_2 < 0 and d_1 > 0) then intersection, else no intersection
Plane segmend collision method 2 (only gives yes/no)
h = OBB half projection on \vec{n}
S1 = C - h(\vec{n}/||\vec{n}||) (C in world space)
S2 = C + h(\vec{n}/||\vec{n}||)
d1 = (S1 - P0) \cdot \vec{n}
d2 = (S2 - P0) \cdot \vec{n}
return d1 < 0 AND d2 > 0
OBB(max,min) and Plane(P0,n) collision.
OBB half = (max-min)*0.5. Projection axis v = n

Interpolation

u = (t - t_i)/(t_{i+1} - t_i)
U = (u,1)
M_{lin} = \begin{pmatrix} -1 & 1 \\ 1 & 0 \end{pmatrix}
G = \begin{pmatrix}P_i \\ P_{i+1}\end{pmatrix}
Q(u) = U M_{lin} G
Linear interpolation at time t given points P and times
u = (t - t_i)/(t_{i+1} - t_i)
U = (u^3,u^2,u,1)
M_{cr} = 0.5 \begin{pmatrix} -1 & 3 & -3 & 1 \\ 2 & -5 & 4 & -1 \\ -1 & 0 & 1 & 0 \\ 0 & 2 & 0 & 0 \end{pmatrix}
G = \begin{pmatrix}P_{i-1} \\ P_i \\ P_{i+1} \\ P_{i+2}\end{pmatrix}
Q(u) = U M_{cr} G
Catmull-Rom interpolation at time t given points P and times
u = (t - t_i)/(t_{i+1} - t_i)
U = (u^3,u^2,u,1)
M_{bezier} = \begin{pmatrix} -1 & 3 & -3 & 1 \\ 3 & -6 & 3 & 0 \\ -3 & 3 & 0 & 0 \\ 1 & 0 & 0 & 0 \end{pmatrix}
G = \begin{pmatrix}P_i \\ C_{i1} \\ C_{i2} \\ P_{i+1}\end{pmatrix}
Q(u) = U M_{bezier} G
Cubic Bezier Interpolation at time t given points P, control points C, and times
u = (t - t_i)/(t_{i+1} - t_i)
U = (u^2,u,1)
M_{bezier} = \begin{pmatrix} 1 & -2 & 1 \\ -2 & 2 & 0 \\ 1 & 0 & 0 \end{pmatrix}
G = \begin{pmatrix}P_i \\ C_i \\ P_{i+1}\end{pmatrix}
Q(u) = U M_{bezier} G
Quadratic Bezier Interpolation at time t given points P, control points C, and times
\theta = cos^{-1}(\vec{p}\cdot\vec{q})
slerp = \vec{r} = \frac{sin((1-u)\theta)\vec{p} + sin(u\theta)\vec{q}}{sin\theta}
Spherical-linear interpolation (slerp) on quaternions p and q at time u in 0..1