Asteroids is a game I programmed as a challenge project to learn more about polygon collision detection. Using various guides and the separation axis theorem, I coded my own 2D physics engine via a language called Processing. It is quite useful for games and 2D visuals because of its ready-made update loop. The game has powerups, accurate 2D polygon collision, momentum calculations, screen wrapping, particle physics, and dynamic UI animation. I put a lot of hard work into Asteroids and I’m immensely pleased with the end result. You can watch a video of the game below, or download the executable here.
The biggest design challenge I faced was accurate 2D polygon collision. Normally I could use simple circle collisions for this game, but doing so would lead to frustrating visual effects for the player. The ship could explode even if no asteroids appeared to be colliding with it. That sort of thing is something that makes people dislike the game. It makes the controls feel clunky and the game unpolished. So for asteroids, I absolutely needed accurate collisions.
Polygon collision follows something called the Separation Axis Theorem (using vector math and linear algebra) to determine if a collision between two shapes is happening. A far more in-depth and mathematically heavy guide can be found here, but I’ll detail it briefly. If you can draw a line between two objects, they are obviously not colliding. But we need an algorithm to find that line. There are an infinite number of lines, so the SAT describes a way of programmatically choosing the best lines to check. These lines are found using the normal vectors to every edge of the polygons (those at right angles to the edge). To check, the polygons are both projected orthogonally onto the line. If the projections don’t overlap, the shapes are not colliding with one another. The algorithm’s efficiency comes from the fact that if it finds a separating line, the shapes are proved to be non-colliding and the algorithm can return right away. In this way, we can run efficient polygon collision detection.
2D Polygon Collision
Accurate 2D polygon collision using the Separation Axis Theorem ensures that there are no odd visual effects when asteroids collide.
Momentum-based Elastic Collisions
Asteroids have mass based on size, bouncing off each other with accurate transfers of momentum. The ship also bounces off asteroids when shields are active.
Dynamic UI Animation
The power-up UI animates dynamically. Circle meters fill up based on points, smooth animation between powerup selections, and none of it breaks with rapid inputs.
Three different power-up abilities enable engaging gameplay. The Nuke destroys everything. The shield softens blows. The Laser shoots a beam, destroying things touched.
Asteroids floating off one side will simultaneously be floating in from the opposite. Collisions for the asteroid are detected at both positions.
Energy particles employ gravitation and forces efficiently enough for hundereds to be on screen at once. All will slowly gravitate toward the player before being consumed.
Copyright © 2016 Daniel Timko