time for a snapshot

This commit is contained in:
Schluffe
2026-07-11 17:48:28 +02:00
parent 135b7c3042
commit 7262752e16
47 changed files with 1712 additions and 61 deletions
@@ -0,0 +1,39 @@
# Step 02 — Length, Normalize, Dot
## Concept
### Length (magnitude)
An arrow `(x, y)` has a length: how far it reaches. Pythagoras:
`length = sqrt(x*x + y*y)`. A velocity's length is its **speed**.
### Normalize
Often you want *just the direction* of an arrow, with length exactly 1 (a "unit
vector"). You get it by dividing the arrow by its own length:
`(x / len, y / len)`.
This is how your engine turns "the player pressed up+left" into a clean diagonal
direction before multiplying by a speed — so diagonal movement isn't faster than
straight movement.
> ⚠️ **The zero-vector trap.** What is the length of `(0, 0)`? Zero. What is
> `0 / 0`? `NaN`. If you normalize a zero vector naively, you poison it with
> `NaN`, and `NaN` spreads through every later calculation silently. A correct
> `normalize` must check for zero length and return `(0, 0)` instead of dividing.
> Remember this trap — it is exactly the kind of bug that hides in a real engine.
### Dot product
`dot(a, b) = a.x*b.x + a.y*b.y`. One number out of two vectors. For now just
implement it; in step 09 you'll learn that it answers "how much of arrow A points
along arrow B?" — the key to sliding along a wall.
## Task
Implement `length`, `normalize` (zero-safe!), and `dot` in `mathv.ts`.
`vec`/`add`/`sub`/`scale` are already provided in `given.ts`.
```sh
bun test workshop/steps/02-vectors-length
```