Workshop › Endless Runner › Step 4 of 19

Gravity

What we are doing

The runner gets a vertical speed vy. Every frame gravity adds to it and the speed moves the runner; when the feet reach the ground, it stops there.

```js
const GRAVITY = 0.6

let runner = { x: 50, y: GROUND - STAND_H, w: 40, h: STAND_H, vy: 0 }

function onGround() {
  return runner.y + runner.h >= GROUND
}

function update() {
  runner.vy += GRAVITY
  runner.y += runner.vy
  if (onGround()) {
    runner.y = GROUND - runner.h
    runner.vy = 0
  }
}

  update()
```

What it means

Your turn

  1. Under GROUND, write GRAVITY; add , vy: 0 to runner.
  2. Under runner, write onGround and update.
  3. In loop, call update() before draw().

Skills: Velocity and gravity

All steps

  1. The ground
  2. The runner
  3. Draw again and again
  4. Gravity
  5. Jump
  6. Short hops
  7. Tap to jump
  8. Things that come at you
  9. Cacti keep coming
  10. Wait for the start
  11. Crash
  12. Try again
  13. Faster and faster
  14. Remember the best run
  15. The score counter
  16. Birds
  17. Which keys are down?
  18. Duck
  19. Build it yourself: jump buffering