3D Math Primer Notes
This page will display the notes that I took while reading the book 3D Math Primer for Graphics and Game Development by Fletcher Dunn and Ian Parbery. I am reading this book to learn the mathematical concepts behind 2D and 3D objects in games, namely their positions, orientations, trajectories, and more. With this, I hope to demonstrate 3D vector math knowledge to future employers.
Table of Contents
Introduction
This section gave me a very good overview of the book as a whole. I am very excited to be reading about many different areas of mathematics such as trigonometry, linear algebra, and calculus. I have experience with all of these topics from my undergraduate education, but I am excited to learn how it is put into practice!
Chapter 1: Cartesian Coordinate Systems
1.1 - 1D Mathematics
- The Cartesian coordinate system is used to measure locations, distances, and angles mathematically in 2D and 3D spaces
- In programming, all variable types are discrete in the end, including everything from short and int to float and double.
- Choosing a variable type is less so a matter of rational versus natural numbers, but rather a matter of precision needed
- The first law of computer graphics: if it looks right, it is right
1.2 - 2D Cartesian Space
- Cartesian implies rectangular. Your basic xy-axis is Cartesian, for instance.
- A 2D Cartesian coordinate space is defined by two things: the origin of the (boundless) plane and the two axis’ that run perpendicular through it
- Normal orientation of a 2D plane implies that North faces up and East faces right (as it should be), but 2D planes can be in any orientation.
- All 2D planes can be rotated or reoriented to become the normal orientation, thus suggesting all planes in the 2D space are equal. This is not the case in 3D
- 2D Cartesian coordinates are (x, y) (naturally enough)
1.3 - 3D Cartesian Space
- The conventions for defining the position and negative directions for each x, y, and z axis in 3D is not standardized like it is in 2D.
- 3D Cartesian coordinates are (x, y, z)
- You can orient a 3D space to have 2 of the axis line up with the direction you want, but the third axis will always point in the wrong direction. In other words, unlike 2D space, not all 3D spaces can be rotated in a way to line up with another.
- There are two types of 3D coordinate spaces: left-handed and right-handed.
- If two coordinate spaces have the same “handiness”, then they can be rotated to match each other
- Positive and negative rotation about an axis differs between left- and right-handed coordinate spaces. Refer to this diagram for a good visual explanation
1.4 - Odds and Ends
- Summation notation is for adding a bunch of terms together
- There is also an equivalent procedure for multiplying terms together which, though unnamed, I will call product notation
- Be familiar with interval notation: [x,y], (x,y], [x,y), (x,y)
- (x,y) could either refer to a point in 2D space or an open (exclusive) interval
- [x,y] could either refer to a 2D vector or a closed (inclusive) interval
- Angle measures can be in degrees, which range from 0-360, or radians, which range from 0π-2π
1 rad = (180/π) deg
1 deg = (π/180) rad
- Trig functions for defining the x- and y- coordinates of a rotated point on the unit circle:
cos(θ) = x
sin(θ) = y
- More trig functions:
sec(θ) = 1/cos(θ)
csc(θ) = 1/sin(θ)
tan(θ) = sin(θ)/cos(θ)
cot(θ) = 1/tan(θ) = cos(θ)/sin(θ)
- More generalized trig functions on the unit circle considering the following figure:
cos(θ) = x/r
,sin(θ) = y/r
,tan(θ) = y/x
sec(θ) = r/x
,csc(θ) = r/y
,cot(θ) = x/y
- Trig identities:
sin(-θ) = -sin(θ)
,cos(-θ) = -cos(θ)
,tan(-θ) = -tan(θ)
sin((π/2)-θ) = cos(θ)
,cos((π/2)-θ) = sin(θ)
,tan((π/2)-θ) = cot(θ)
- Identities from applying the Pythagorean theorem to the unit circle:
sin^2(θ) + cos^2(θ) = 1
1 + tan^2(θ) = sec^2(θ)
1 + cot^2(θ) = csc^2(θ)
- Also refer to the sum and difference identities, double angle identities, and the law of sines and law of cosines
Chapter 2: Vectors
2.1 - Mathematical Definition of Vector
- Mathematically, vectors are a list of numbers
- Scalars are ordinary numbers
- Ex: “velocity” and “displacement” are vector quantities while “speed” and “distance” are scalar quantities
- A vector’s dimension is how many numbers it contains (the length)
- A 1D vector has a single value (a scalar can be considered 1D), a 2D has two values, etc.
- Vector syntax: [1,2,3] (this is a row vector) (column vectors are written vertically)
- When refering to individual values within a vector, use subscript notation
- Ex:
**a** = [3,4,5,6]
; a_1 = a_x = 3
;a_2 = a_y = 4
;a_3 = a_z = 5
;a_4 = a_w = 6
;
- Ex:
- Common syntax for scalar, vector, and matrix variables:
- Scalar quantities use lowercase in italics: a, b, x, y, θ
- Vector quantities use lowercase in boldface: a, b, u, v
- Matrix quantities use uppercase in boldface: A, B, M, R
2.2 - Geometric Definition of Vector
- Vectors are directed line segments with a magnitude and direction
- Magnitude: length of the vector (nonnegative)
- Direction: which way the vector is pointing in space
- The head of a vector refers to the end with the arrowhead on it and the tail refers to the opposite end
- Vectors do not have positions, only magnitudes and directions. Consider the following examples:
- Displacement: “Take three steps forward” does not require a position
- Velocity: “I am traveling northeast at 50 mph”. It has a magnitude (50 mph) and direction (northeast), but no position
- Comparison of scalar values with their similar, but different, vector counterparts:
- Speed (scalar) is the magnitude of the vector velocity
- Distance (scalar) is the magnitude of the vector displacement
- Vectors are relative, not absolute, due to the lack of position
2.3 - Specifying Vectors with Cartesian Coordinates
- You can assign values to vectors using Cartesian Coordinates, but they do not represent a position in space, but
rather a displacement from the tail of the vector to the head of the vector.
- Two vectors with the same value (ex:
(1.5, 1)
), can be represented at different locations in a plane because vectors have no position, but can share the same displacement. - Examples below
- Two vectors with the same value (ex:
- Polar coordinates can also be used to specify vectors (refer to Chapter 7 for details)
- The 3D vector [1,-3,4] represents a single displacement
- 1 unit to the right, 3 units down, and 4 units forward
- The order in which the three sub-displacements take place does not matter; the total displacement will be the same
- The zero vector has zeroes in all positions. Ex: 3D zero vector is [0,0,0]
- It is the only vector in its dimension with zero magnitude and no direction
- Describes “no displacement”
2.4 - Vectors versus Points
- Points specify a position while vectors specify a displacement
- Because vectors describe displacements, they can inherently describe relative positions
- Any point can be represented as a vector from the origin: (x,y) => [x,y]
2.5 - Negating a Vector
- Every vector v has an additive inverse -v such that
**v** + (**-v**) = 0
- Below are examples of vectors and their inverses
- Below are examples of vectors and their inverses
- To negate a vector of any dimension, simply distribute a negative sign to each element within the vector
- Ex:
-[a_1, a_2, a_3] = [-a_1, -a_2, -a_3]
- Interpretation: negating a vector results in a vector of the same magnitude but in the opposite direction
- Ex:
2.6 - Vector Multiplication by a Scalar
- You can multiply (but not add) a scalar and a vector. Simply distribute the scalar into each value of the vector.
- Ex:
k[a_1, a_2, a_3] = [ka_1, ka_2, ka_3]
- The result is a new vector parallel to the original, but possibly of different magnitude and opposite direction
- Interpretation: multiplying a scalar into a vector scales the length of that vector (refer to figure below)
- Ex:
- You can also divide a vector by a scalar since it is the same as multiplying by the reciprocal
- Below is an example of scalar multiplication
- A scalar cannot be divided by a vector, nor can a vector be divided by another vector
2.7 - Vector Addition and Subtraction
- Vectors must be of the same dimension to be added or subtracted. The process is simple:
- Ex:
[a_1, a_2, a_3] + [b_1, b_2, b_3] = [a_1 + b_1, a_2 + b_2, a_3 + b_3]
- Ex:
[a_1, a_2, a_3] - [b_1, b_2, b_3] = [a_1 - b_1, a_2 - b_2, a_3 - b_3]
- Ex:
- You can use the triangle rule to add multiple vectors by positioning the tail of one vector at the head of the previous and connecting the tail of the first vector with the head of the last.
- Consider the visual below
- Consider the visual below
- When adding multiple vectors together, think of it like different forces being applied to an object (ex: wind is blowing you one way,
your car is bringing you another way, while your seat is moving backward).
- Below is a good visual for considering the result of applying many different forces to an object
- Below is a good visual for considering the result of applying many different forces to an object
- Finding the displacement between two points a and b is as simple as considering the vector representation of the points
and subtracting them: b - a
2.8 - Vector Magnitude (Length)
- Magnitude = length or norm of vector
- Since vectors are represented by a cartesian coordinate (ex: (3,4)), which does not represent either magnitude or displacement, the maginutude must be computed
-
A magnitude is represented mathematically with double pipes: v (magnitude of v) (sometimes represented with one bar) - Computing the magnitude
- Take the square root of the sum of the square of each component in the vector
- (Pretty much the Pythagorean theorem)
- Below is the formula for the magnitude of an arbitrarily-sized vector
- Below is the formula for 2D and 3D vectors
2.9 - Unit Vectors
- A unit vector, or normalized vector, has a magnitude of 1
- Useful when we dont care about magnitude, only direction (I want to know which way I am facing, but thats it)
- NOTE: a normal vector is one that is perpendicular to a surface (usually still with unit length), so don’t get confused
- Normalizing a vector means to compute a new vector whos magnitude is 1 (unit length) but points in the same direction as the original
- Common case is to use the “hat” symbol to mark a variable as a normalized vector (ex: “v hat”)
- To normalize a vector, divide the vector by its magnitude (shown below)
- The zero vector cannot be normalized
2.10 - Distance Formula
- Refer to the image below for the distance formula for 2D and 3D vectors
2.11 - Vector Dot Product
- The dot product is also known as the inner product
- The dot product must be represented with the dot symbol between the vectors
- The dot product of two vectors is the sum of the products of corresponding components
- It results in a scalar (refer to formula below)
- It results in a scalar (refer to formula below)
- The dot product can be used to determine the angle between two vectors using the following formula:
- The dot product can also measure the length of the projection of b onto a, multiplied by the length of a
2.12 - Vector Cross Product
- A cross product can only be applied in 3D and yields a 3D vector
- The cross product will always use the cross symbol (x)
- The cross product yields a vector that is perpendicular to the original two vectors (shown below)
- The length of a x b is equal to the product of the magnitudes or a and b and the sine of the angle between them:
||a x b|| = ||a|| ||b|| sin(θ)
2.13 - Linear Algebra Identities
- No need to memorize these
Chapter 3: Multiple Coordinate Spaces
3.1 - Why Bother with Multiple Coordinate Spaces?
- Having one universal coordinate space for everything in, well, the universe is a terrible idea
- Many coordinate spaces are relative to something nearby, not something on the other side of the planet, thus creating the preference for a custom coordinate space
- Transformational relativism: the contention that no place or orientation of coordinate system can be considered superior to others
3.2 - Some Useful Coordinate Spaces
- World coordinate system: the coordinate space for the planet, with the origin being at the intersection of the equator and the longitude
for Greenwich, England
- Operates on longitudes and latitudes
- Uses spherical coordinates instead of Cartesian coordinates
- Is an absolute coordinate space (because its the biggest one we care about)
- Object space: the coordinate space associated with a particular object (such as yourself, or a box)
- Camera space: the coordinate space associated with the viewpoint used for rendering
- The camera is considered to be at (0,0)
- Camera space is 3D while screen space is 2D
- Mapping camera space => screen space involves projection
- Upright space: associated with an object and halfway between world space and object space in that the axis are parallel to the world space but the origin is the same as the object’s origin
3.3 - Basis Vectors and Coordinate Space Transformations
- Basis vectors: pretty much the x,y or x,y,z axes of a coordinate plane (they dont necessarily need to be perpendicular)
- Different questions require different coordinate spaces
- Example: To determine if a sandwich is north, use world space; to decide how to grab it (turning left/right), use the robot’s object space.
- Sensors and motor controls rely on object space; rendering and lighting may require world space.
- Coordinate space transformations solve the problem of mismatched frames of reference
- You often need to convert data (positions, directions) between spaces like world, object, and camera space.
- These transformations are mathematical, not physical; objects don’t move, their representation changes.
- Two perspectives on transformations: Active vs. Passive
- Active: The object moves within a fixed coordinate space.
- Passive: The coordinate system moves around a fixed object.
- Both yield the same result but differ in mental framing
- Model transformations follow a sequence: Rotate, then Translate
- Rotating around the origin is simpler (a linear transform) than rotating around another point (an affine transform).
- Translating first requires extra steps to simulate rotation about the origin anyway, so rotate-first is preferred.
- Camera transformations invert the logic: Translate then Rotate the world
- To move from world space to camera space, imagine moving the entire world so the camera is at the origin.
- This shift allows relative positions to be calculated without physically moving the camera or objects.
3.4 - Nested Coordinate Spaces
- It becomes easier to animate little things with nested coordinate spaces
- Take a sheep for instance:
- The origin of a sheep object as a whole is know in the world space
- The sheep’s head can have it’s own coordinate space
- The movement/animation of the sheep’s ear can be in relation to the sheep’s head space
- The sheep’s head space is know in relation to the sheep’s body space, which in turn is known in the world space
- This creates a hierarchy of coordinate spaces, with the world space as the root
Chapter 4: Introduction to Matrices
4.1 - Mathematical Definition of Matrix
- An m x n matrix has m rows and n columns
- A square matrix has the same number of rows and columns
- The diagonal elements go from top-left corner to bottom-right
- Diagonal matrix: all nondiagonal elements are 0
- Identity matrix: a diagonal matrix with all 1’s on the diagonal
- Matrix transposition:
- Given an r x c matrix M, the transpose (denoted M^T) is the c x r matrix such that the values are flipped along the
main diagonal as such:
- Given an r x c matrix M, the transpose (denoted M^T) is the c x r matrix such that the values are flipped along the
main diagonal as such:
- Multiplying a matrix by a scalar:
- Simply distribute the scalar into each element of the matrix via multiplication
- Multiplying two matrices:
- Considering two matrices r x n and n x c, the result would be an r x c matrix
- NOTE: the number of columns in the first matrix must be equal to the number of rows in the second.
4.2 - Geometric Interpretation of Matrix
- A square matrix can describe any linear transformation such as
- rotation
- scale
- orthographic projection
- reflection
- shearing
- Example of visualizing the transformation that a matrix applies to any given vector
- Consider the following matrix:
- We first extract the basis vectors p and q from the rows:
- p = [2 1]
- q = [-1 2]
- Then you place them at the origin. Below on the left represents an untouched vector (or asset) and on the right
is the result of the transform of this particular matrix (rotates and scales it)
- Consider the following matrix:
- The rows of a square matrix can be considered the basis vectors of a coordinate space
- Multiply the vector by the matrix to transform the vector to the new coordinate space