Workshop › Asteroids › Step 3 of 41

Which way it points

What we are doing

The one bit of trigonometry games use every day: a direction at angle a is (Math.cos(a), Math.sin(a)). The ship's nose is SHIP_R pixels from its center in that direction. For now we draw a line from the nose to the center.

```js
const SHIP_R = 14 // the ship's size: distance from its center to its nose

function drawShip() {
  const tip = { x: ship.x + Math.cos(ship.angle) * SHIP_R, y: ship.y + Math.sin(ship.angle) * SHIP_R }
  ctx.beginPath()
  ctx.moveTo(tip.x, tip.y)
  ctx.lineTo(ship.x, ship.y)
  ctx.stroke()
}

  ctx.strokeStyle = 'white'
  ctx.lineWidth = 2
  drawShip()
```

What it means

Your turn

  1. Under the two canvas lines, after an empty line, write SHIP_R and an empty line.
  2. Above function draw() { write drawShip, followed by an empty line.
  3. In draw, under the black background, leave an empty line and write the three new lines.

Skills: Velocity and gravity, Drawing on a canvas

All steps

  1. Black space
  2. Where the ship is
  3. Which way it points
  4. A triangle ship
  5. Which keys are down?
  6. Turning
  7. The game loop
  8. Thrust and drift
  9. A little friction
  10. A speed limit
  11. Around the screen
  12. Making a bullet
  13. Fire!
  14. Bullets fly
  15. A lifetime for bullets
  16. Making a rock
  17. Four big rocks
  18. Drawing the rocks
  19. Jagged rocks
  20. Drifting rocks
  21. When do two circles touch?
  22. Breaking a rock
  23. Bullets hit rocks
  24. Points
  25. Show the score
  26. Losing a life
  27. A rock hits the ship
  28. Show the lives
  29. What time is it?
  30. A moment of safety
  31. Blink while safe
  32. Waves of rocks
  33. The next wave
  34. Faster every wave
  35. One function to start
  36. Playing or over
  37. Game over
  38. Best score
  39. GAME OVER
  40. Play again
  41. Build it yourself: hyperspace