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

The atan2 trap

What we are doing

Point below your barrel and the barrel points into the ground: atan2 happily gives angles between 0 and π there (down is positive on a canvas). The barrel must stay in the upper half, so below it we aim flat to that side instead.

```js
// Below the barrel, atan2 gives an angle between 0 and π: aim flat to that side instead.
t.angle = angle > 0 ? (angle > Math.PI / 2 ? -Math.PI : 0) : angle
```

What it means

Your turn

Above the atan2 line, write the comment; replace t.angle = angle with the checked version.

Skills: Velocity and gravity

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