Lesson 10 of 22, in world 3: Physics & Collisions
Gravity, Jumping & Collision Response
Detecting overlap tells you that you hit a wall. Collision response decides what happens next — usually, push the object back out so it’s touching but not inside. Combine that with gravity and a jump and you have the heart of every platformer.
Gravity is just acceleration
From lesson 6: acceleration changes velocity. Gravity is a constant downward acceleration, so every frame vy += GRAVITY * dt. A jump is a single instant upward kick: vy = -JUMP_SPEED. Gravity then slows the rise, stops it, and pulls you back down — tracing a parabola.
Move with ←→, jump with Space or ↑. Tap jump for a short hop, hold it for a high jump. Walk off a ledge and jump a moment late — coyote time still lets you. The dotted trail is your jump arc: a parabola, because gravity is a constant acceleration.
Resolving collisions one axis at a time
If you move diagonally into a corner, which way should you be pushed out? The simplest robust approach: move along x, fix any overlap along x; then move along y, fix any overlap along y. Because only one axis changed each time, you always know which side you came from.
// Horizontal
player.x += player.vx * dt
for (const wall of solids) if (overlaps(player, wall)) {
player.x = player.vx > 0 ? wall.x - player.w // moving right → snap to wall's left side
: wall.x + wall.w // moving left → snap to its right side
player.vx = 0
}
// Vertical
player.y += player.vy * dt
player.onGround = false
for (const wall of solids) if (overlaps(player, wall)) {
if (player.vy > 0) { player.y = wall.y - player.h; player.onGround = true } // landed
else player.y = wall.y + wall.h // bonked head
player.vy = 0
}Game-feel tricks every platformer uses
- Coyote time — allow a jump for ~0.1 s after walking off a ledge (like Wile E. Coyote hanging in mid-air). Players feel they pressed in time, because they almost did.
- Jump buffering — from the input lesson: remember an early jump press until you land.
- Variable jump height — if the jump button is released while still rising, cut the upward velocity. Tap = hop, hold = high jump.
- Higher gravity when falling — makes jumps feel snappy instead of floaty.
Key takeaways
- Gravity: vy += g·dt every frame. Jump: set vy to a negative value once.
- Resolve collisions one axis at a time: move x → fix x, move y → fix y.
- Coyote time, jump buffering and variable jump height make controls feel fair.
Check yourself
Q1. After landing on a platform, what should vy become?
Q2. Why resolve x and y separately?
Q3. A player runs off a cliff and presses jump 3 frames later. It works. Why?
Exercises
- Turn coyote time off and try to jump at the very edge of the first gap. How often do you fail?
- Add "fall gravity": use 1.6× gravity when vy > 0. Does the jump feel better?
- Add a double jump: a counter that resets on landing and allows one jump in mid-air.