This commit is contained in:
Schluffe
2026-09-04 13:57:52 +02:00
parent 71cfa64793
commit e961887953
24 changed files with 340 additions and 360 deletions
+16 -13
View File
@@ -1,28 +1,31 @@
# Step 09 — Slide (this is what `dot` was for)
Back in step 02 you implemented `dot` and I said "you'll see later." This is
later. Sliding is the difference between a game that feels good and one where you
stick to every wall like glue.
later. Sliding is the difference between a game that feels good and one where
you stick to every wall like glue.
## The problem
You're moving with velocity `v` and you hit a wall whose outward normal is `n`.
If you just *stop* (velocity → 0), the player jams against the wall — press
into a wall diagonally and all motion dies, even the part that was parallel to
the wall and perfectly fine. What we actually want: **cancel only the part of `v`
that pushes *into* the wall, and keep the part that runs *along* it.** That's a
If you just _stop_ (velocity → 0), the player jams against the wall — press into
a wall diagonally and all motion dies, even the part that was parallel to the
wall and perfectly fine. What we actually want: **cancel only the part of `v`
that pushes _into_ the wall, and keep the part that runs _along_ it.** That's a
slide.
## The math (projection)
Any velocity `v` can be split into two pieces relative to the wall:
- the part **along the normal** (into/out of the wall) — this is what the wall forbids,
- the part **along the wall surface** (perpendicular to the normal) — this is fine.
- the part **along the normal** (into/out of the wall) — this is what the wall
forbids,
- the part **along the wall surface** (perpendicular to the normal) — this is
fine.
Because `n` is a **unit vector**, the amount of `v` pointing along `n` is exactly
`dot(v, n)`. That single number is "how much of `v` goes straight into the wall."
The vector piece pointing into the wall is `n * dot(v, n)`. Subtract it off:
Because `n` is a **unit vector**, the amount of `v` pointing along `n` is
exactly `dot(v, n)`. That single number is "how much of `v` goes straight into
the wall." The vector piece pointing into the wall is `n * dot(v, n)`. Subtract
it off:
```
vSlide = v - n * dot(v, n)
@@ -30,8 +33,8 @@ vSlide = v - n * dot(v, n)
What's left has **zero** component along the normal — it lies flat against the
wall. (That's the geometric meaning of `dot`: it measures how much two vectors
share a direction. Subtract the shared-with-the-normal part, and nothing pointing
into the wall survives.)
share a direction. Subtract the shared-with-the-normal part, and nothing
pointing into the wall survives.)
### Feel it with numbers