How does a kinematic character controller's collide-and-slide loop work, and how should it treat walkable slopes differently from steep walls?
answer
- sweep, stop short, redirect, repeat
- remove the part pushing into the surface
- a skin and an iteration cap
- normal's up component against the slope limit
basics
~20 sEach step the controller sweeps its capsule along the desired move, stops just short of the first hit, removes the part of the remaining motion that pushes into the surface and repeats; the slope angle decides whether a surface is ground or wall.
solid answer
~50 sA kinematic controller is not pushed by the solver; it moves itself with shape casts. Each step it sweeps the capsule along the remaining displacement, advances to just short of the first hit (leaving a small **skin**), then projects the leftover onto the surface plane with `r - dot(r, n) * n` and sweeps again, for a handful of iterations. The **slope limit** classifies each hit: if `dot(n, up) >= cos(maxSlope)` the surface is **walkable ground**, otherwise a **wall**. On walkable ground the character is grounded, input is projected onto the ground plane and rescaled so speed does not drop uphill, and gravity is not slid along the slope, or the character creeps downhill while standing still. On a steep surface the normal is flattened to horizontal before projecting, so walking into it cannot climb it, and gravity is allowed to slide the character down.
code
pseudocode · 19 linesfunction moveAndSlide(position, displacement):
remaining = displacement
for i in 0 .. MAX_ITERATIONS - 1:
dist = length(remaining)
if dist < EPSILON:
break
dir = remaining / dist
hit = capsuleCast(position, dir, dist + SKIN, filter)
if hit is none:
position = position + remaining
break
travel = max(hit.distance - SKIN, 0)
position = position + dir * travel
leftover = dir * (dist - travel)
n = hit.normal
if dot(n, up) > 0 and dot(n, up) < cos(MAX_SLOPE):
n = normalize(n - dot(n, up) * up) // steep upward-facing slope: treat as a vertical wall
remaining = leftover - dot(leftover, n) * n
return positiongo deeper
Recall the loop: sweep the capsule, stop at the hit, slide the rest along the surface, repeat a few times. Know that a slope limit separates ground from wall.
Explain the projection formula, the skin, the iteration cap and the slope test with the normal's up component against the cosine of the limit.
Diagnose creeping on gentle slopes, slower uphill speed, climbing steep slopes and corner jitter, and fix each in the loop rather than with tuned friction.
Decide which movement rules belong in one shared controller and which are per-game tuning, so slope limits and skin match the level metrics designers build to.
## Why a kinematic controller moves itself A **kinematic character controller** is a character body that the physics solver does not move. Gameplay code decides the desired displacement each step (from input, gravity and jumps), and the controller's job is to apply as much of that displacement as the world allows, without ever ending inside geometry. The algorithm almost every controller uses for this is **collide-and-slide**: move until you touch something, then slide along it. ## The loop step by step For a desired displacement `d` in one step: 1. Set `remaining = d`. 2. **Sweep** the capsule from its current position along `remaining` (a shape cast), with length `length(remaining) + skin`. 3. If nothing is hit, move the full `remaining` and stop. 4. Otherwise **advance** to `max(hit.distance - skin, 0)` along the direction, so the capsule stops a small skin distance short of the surface. 5. Take the part of `remaining` not yet travelled and **remove its component into the surface**: `leftover - dot(leftover, n) * n`, where `n` is the hit's unit normal. 6. Repeat from step 2 with the projected leftover, for at most a handful of iterations. Projecting rather than reflecting is the point: `r - 2 * dot(r, n) * n` would bounce the character off the wall, while `r - dot(r, n) * n` keeps only the motion along it, which is what sliding feels like. The same projection is usually applied to the stored velocity, so momentum into a wall does not build up. ## Skin, iterations and creases - The **skin** keeps the capsule slightly separated from surfaces. Without it, floating-point error leaves the capsule touching or barely inside geometry, and the next sweep starts in contact, which engines report inconsistently. - The **iteration cap** bounds cost. Three to five iterations is a common choice; the remainder is dropped if the cap is reached. - In a **crease** between two surfaces (a corner, or a floor meeting a wall), sliding along one plane can push the character back into the other, and the loop jitters between them. A robust fix is to move along the crease line `cross(n1, n2)` when the second hit's projection would re-enter the first plane. ## Walkable slopes and steep walls Every hit is classified by comparing the normal's up component with the cosine of the **slope limit**: | Slope limit | Threshold `cos(limit)` | Normal `n.y` of a 30 degree slope | Classification | |---|---|---|---| | 45 degrees | 0.707 | 0.866 | walkable | | 25 degrees | 0.906 | 0.866 | wall | Here `y` is up and the slope angle is measured from horizontal, so a slope of angle `t` has a normal whose up component is `cos(t)`. - **Walkable**: the hit counts as ground. The character is grounded, may jump, and its horizontal input is turned into motion along the ground plane. - **Steep**: the hit is treated as a wall. Before projecting, the normal's vertical part is removed and the normal renormalised, so walking into the slope slides sideways but never upwards. Gravity's downward motion still projects onto the real slope, so the character slides down. ## The classic slope bugs - **Creeping down a gentle slope while standing still.** If gravity keeps accumulating while grounded, its downward displacement hits the slope and the projection keeps a component along the slope of size `g_disp * sin(t)` each step, pointing downhill. Fix: while grounded on walkable ground, reset vertical velocity to a small stick-down value and do not slide that part along the surface. - **Slower uphill, airborne downhill.** Horizontal input of speed `s` sliding up a slope of angle `t` keeps only `s * cos(t)`. Fix: project the input direction onto the ground plane first and rescale it to full speed. Downhill, horizontal motion leaves the surface every step; ground snapping handles that. - **Climbing a steep slope by pushing into it.** Projecting horizontal input onto the true steep normal yields an upward component. Fix: flatten the normal as above. - **Launching off the top of a ramp.** If slid displacement is written back into vertical velocity, the character keeps rising past the crest. Keep the vertical velocity that gameplay set, not the one the slide produced.
- Why is the leftover motion projected onto the surface instead of reflected?Reflection, `r - 2 * dot(r, n) * n`, sends the character away from the wall, which feels like bouncing. Projection, `r - dot(r, n) * n`, removes only the component into the surface and keeps the motion along it, so the character slides along walls and up ramps the way players expect.
- Why does the controller stop a skin distance short of the hit?Ending exactly on the surface leaves the capsule touching it, and rounding error can place it slightly inside. The next sweep then starts in contact, which engines report as a zero-distance hit or ignore. A small gap keeps every sweep starting clear, at the cost of a barely visible separation.
- What goes wrong in a corner between two walls, and how is it handled?Sliding along one wall can push the character into the other, and the next iteration slides it back, so it jitters in the corner. When the second projection would re-enter the first plane, move along the crease direction `cross(n1, n2)` instead, or stop if that also points into a surface.
- The leftover motion should be reflected off the wall
- A kinematic controller is moved by the solver's contact forces
- Slope angle is the arcsine of the normal's up component
- Applying gravity every step while grounded is harmless
- Collide-and-slide needs no iteration cap