build-a-physics-engine / lesson-02.md
Lesson 02 · A 2D vector kit

Subtraction and negation

The vector from one point to another is their difference, so subtraction is how the engine will later find the direction between two bodies. Today you add Sub and Neg alongside yesterday's Add.

The goal

Add subtraction of two vectors and negation of a single vector.

Start here - the target
TO DO
Scenario: Subtracting and negating vectors
Giventhe vectors {5, 7} and {1, 2}
Whenthe second is subtracted from the first
Thenthe result is {4, 5}
AndNeg({3, -4}) is {-3, 4} and Sub(v, v) is {0, 0} for any v
Background

Subtraction answers the question the engine asks constantly: given a body at point a and another at point b, what is the vector that points from one to the other? That direction-and-distance is exactly Sub(b, a), and it is the seed of every collision normal you will compute later. Like Add, it works componentwise and returns a fresh vector.

Negation is the mirror operation - flipping a vector to point the opposite way, which you will reach for when a force pushes one body one direction and its partner the other. Pin the identity that a vector subtracted from itself is the zero vector {0, 0}; that degenerate case (two bodies at the exact same spot) shows up in real collision code, and getting it right now saves a surprise later.

Make it work
// componentwise difference: a - b
func Sub(a, b Vec2) Vec2 { /* X - X, Y - Y */ }
// flip both components
func Neg(a Vec2) Vec2 { /* -X, -Y */ }
CheckpointDONE
You can subtract one vector from another and negate a vector. Commit and stop here.