Workshop › Scorched Earth-style Artillery › Step 9 of 23

Flight

What we are doing

Every frame gravity adds a little to the downward speed, and the shell moves by its speed. That is all it takes for a real curve. fly moves a shell one frame and says what it hit: 'away' off the side, 'ground', or null. When the shell hits something it disappears, and you can aim again.

```js
const GRAVITY = 0.15

// One frame of flight: gravity pulls down. Returns what it hit, or null.
function fly(s) {
  s.vy += GRAVITY
  s.x += s.vx
  s.y += s.vy
  if (s.x < 0 || s.x >= W) return 'away'
  if (s.y >= groundAt(s.x)) return 'ground'
  return null
}

function update() {
  if (state !== 'flying') return
  const hit = fly(shell)
  if (!hit) return
  shell = null
  state = 'aiming'
}

  update()
```

What it means

Your turn

  1. Under H, write GRAVITY.
  2. Above aimBy, write fly with its comment and update.
  3. In loop, call update() before draw().

Skills: Velocity and gravity, The game loop

All steps

  1. The ground
  2. Hills
  3. Two tanks
  4. The barrel
  5. The game loop
  6. Aim
  7. Angle and power
  8. Fire!
  9. Flight
  10. Boom
  11. Craters
  12. Falling tanks
  13. Damage
  14. Taking turns
  15. The computer's shot
  16. Hitting a tank
  17. Win or lose
  18. Wind
  19. The computer aims
  20. Aim with the mouse
  21. Drag and let go
  22. The atan2 trap
  23. Build it yourself: a trail