Step 01 — Vectors
You know this cold. The point of this step is to make the red→green loop automatic, not to teach you arithmetic. Blast through it.
Concept
A vector here is nothing mystical: a pair of numbers (x, y). We use the
same value to mean two different things depending on context:
- a position — a point in the world.
- a displacement / velocity — an arrow: "move this much in x, this much in y."
That's it. All of 2D physics is built on adding, subtracting, and scaling these pairs.
add(a, b)→(a.x + b.x, a.y + b.y)— apply an arrow to a point.sub(a, b)→(a.x - b.x, a.y - b.y)— the arrow that points from b to a.scale(a, s)→(a.x * s, a.y * s)— make an arrow longer/shorter.
Note: we return new objects (pure functions) here for clarity. Your real engine mutates vectors in place to avoid garbage collection — that's a speed optimization we'll deliberately ignore until the very end.
Task
Implement vec, add, sub, and scale in vector.ts.
bun test workshop/steps/01-vectors