OUR EXPERT
David Bolton played Asteroids so much in his youth that he still dreams that he’s surrounded by drifting white shapes.
OUR EXPERT
David Bolton played Asteroids so much in his youth that he still dreams that he’s surrounded by drifting white shapes.
The Asteroids videogame uses a high-speed collision detection method to check when bullets are near or might hit asteroids or enemy ships, or when an asteroid might hit your own ship. This article explains how the collision detection can be achieved and at speed.
The original Atari Asteroids game used wireframe graphics drawn by a dedicated graphics chip. But if we’re going to make it today, we’ll be using solid – that is, raster – graphics. This version of Asteroids has graphic images for asteroids, a player’s ship, bullets, explosions and alien ships.
Accurate collision detection is important – it’s not much fun if your ship blows up when a bullet flies past but doesn’t actually touch it.
Graphic impact
First, we need to discuss the graphics. There are 24 each of four different asteroid sizes: 35, 70, 140 and 280 pixels square, with one image for each size containing 24 rotations. Asteroids and the player’s ship can rotate in 15-degree increments, so there are 24 images in a PNG in two rows of 12. Graphic 0 is facing north, then graphic 1 is at a rotation of 15 degrees, and so on. The explosion graphics don’t move or rotate, so we won’t discuss them.
Red and white grid cells with black objects overlapping and showing how many cells each one overlaps.
ASTEROIDS GAME IN C
There are about 2,200 lines of C in four source files and three headers. The code compiles and runs on an x86 Linux or Raspberry Pi. Note there is some Pi-specific code but that is only enabled when running on a Pi system and not called on other Linux boxes.
First get the source code and files from GitHub (https://bit.ly/lxf314astro). Create a folder called asteroids and fetch everything from the GitHub asteroids folder. You’ll also need the VS Code ZIP (https://github.com/David-H-Bolton/Projects/blob/main/vscode.zip) file. Download and unzip the .vscode folder into the asteroids folder. First make sure you have the dev SDL libraries installed. Run these four commands in a terminal:
To compile asteroids.c,GCC or Clang will do. There is a supplied tasks.json to simplify things but it is preconfigured for Clang 14. If your Clang version is different, edit tasks.json and change the args command to your version:
The folders images, masks and sounds must be located in the asteroids folder. In Visual Studio Code, the asteroids folder should be the Work folder you select when you click on the Explorer tab. The .vscode folder should be under the asteroids folder as well.
After compiling, the game is started by double-clicking the compiled asteroids executable or with the command: ./asteroids in a terminal.
The keys are q and w to rotate the ship, Space to fire the gun, with a maximum of 16 bullets on screen at once. The s key puts up a shield but this counts down as long as s is pressed, and j does a hyperspace jump. You can’t jump again for a couple of seconds after.
ASTEROIDS GAME IN C