Lesson goal: Asteroids: Velocity & Angles

Previous: Introduction to the 3D Rubik's Cube | Home | Next: Tetris: Stacking Blocks & Line Clears

In 1979, Atari released Asteroids, engineered by Lyle Rains and Ed Logg, which became an arcade sensation and one of the defining titles on the Atari 2600.

Asteroids was celebrated for bringing real Newtonian physics into gaming: spaceships drift with momentum, photon cannons fire vector trajectories, and objects wrap continuously around toroidal space.

The Asteroid Belt & Coordinates

The sector map is a 2D Cartesian plane:
  • Width ($x$): $0$ to $600$ pixels (left to right). The center of space is $x = 300$.
  • Height ($y$): $0$ to $400$ pixels (top to bottom). The center of space is $y = 200$.
  • Toroidal Screen Wrapping: Space wraps seamlessly in both directions! If a ship or asteroid drifts off the right edge ($x \ge 600$), it re-enters smoothly from the left ($x = 0$); drift off the top ($y < 0$) and it emerges from the bottom ($y = 400$).


Angles, Heading & Vectors

Your spaceship starts at the center $(300, 200)$, pointing straight UP:
  • $0^\circ$: Facing Right ($+x$ direction).
  • $90^\circ$: Facing Down ($+y$ direction).
  • $180^\circ$: Facing Left ($-x$ direction).
  • $270^\circ$ (or $-90^\circ$): Facing Up ($-y$ direction).


Controlling Your Ship with Code

Instead of twitch arcade reflex controls, you program flight commands:
  • asteroids_game(n): Initializes the simulation for Sector #$n$. Every run with game #$n$ is 100% deterministic: asteroid positions, shapes, and velocities are completely reproducible!
  • turn(deg): Rotates the ship by deg degrees. Positive degrees turn clockwise; negative degrees turn counterclockwise. Shortcuts: turn_left(deg) and turn_right(deg).
  • aim(x, y): Automatically calculates the tangent angle $\theta = \operatorname{atan2}(y - y_{\text{ship}}, x - x_{\text{ship}})$ and turns your ship directly toward coordinate $(x, y)$!
  • thrust(seconds): Fires your rocket thrusters for seconds (default $0.4\text{s}$). The ship accelerates forward with Newton's second law ($F = ma$) and continues drifting with momentum.
  • fire() (or shoot()): Fires a high-speed photon cannon pulse out of your ship's nose cone ($440\text{ px/s}$).
  • wait(seconds): Lets time advance by seconds while objects drift and bullets travel.
  • hyperspace(): Atari 2600 emergency hyperspace warp! Teleports your ship to a new safe sector.


The Math of Splitting Asteroids

When a photon cannon blast strikes an asteroid:
  • Large Rock (Radius $32$, $20$ pts): Fractures into $2$ Medium Rocks drifting outward!
  • Medium Rock (Radius $18$, $50$ pts): Fractures into $2$ Small Rocks!
  • Small Rock (Radius $9$, $100$ pts): Completely vaporizes into a brilliant burst of stardust sparks!
Notice the total points possible from one large asteroid: $$20 + (2 \times 50) + (4 \times 100) = 520 \text{ points!}$$
asteroids_game(1)
fire()

wait(0.4)

turn(25)

fire()
Move the mouse over a dotted box for more information.

  • Aim Vector Radar: The cyan dashed guide line extending from your ship's nose displays your exact aiming angle in degrees.
  • Interactive Playback: Click Run Code to test your program. Use Play/Pause, Step (⏭), and Reset (↺) to analyze flight paths step-by-step.
  • Manual Flying: You can also pilot manually with the keyboard: ← / → to turn, ↑ to thrust, Spacebar to fire, and Shift for hyperspace!

Now you try. Can you clear all fragments in Sector #1, or try asteroids_game(2) to practice timing shots across the horizontal Asteroid Belt?

Type your code here:


See your results here: