Description
This is an implementation of the game Asteroids using the p5js library. The project was an assignment for our Applied Math for Games class in college, and had many learning goals. The assignment was a sort of "choose your own adventure", where we were given a list of features with an associated point value, and a total point threshold that would be considered 100%.
The most interesting parts of the project for me, where the architecture, the polygon collision detection, and the collision avoidence system for small saucers.
Collision
The colliders use an implementation of the
Separating Axis Theorem
where concave shapes are achieved using multiple convex colliders. I
also implemented a specialized circle collider for simple shapes, as
where applicable, it saves on some dot-product operations and finding
the minimum and maximum distances along a given axis. It also provides
more accurate collision for round objects. Ensuring objects could rotate
and still collide correctly proved to be annoying, especially given each
object has a list of colliders that all need to be rotated. To solve
this, I had to pass in not only the other object being checked against,
but also the current objects position and rotation every time I called
the CheckCollision()
method.
Navigation
The saucer navigation was a bonus challenge on the assignment worth 10 points where all others were worth 3 or less. The task was to make small saucers intelligently avoid asteroid, bullets, and the player while making their way accross the screen. The way I solved this problem was by adding a secondary circle collider to act as a detection range, and using the directions from the saucer to all other objects in that range to influence its current travel direction. The influence each object has is inversly propertional to the distance from the object to the saucer, meaning the closer the object is, the more urgently the saucer attempts to flee from it.
Source Code
The following is some of the more insteresting code. Full source code can be found here.
Core Logic
sketch.js
game.js
playerShip.js
Colliders
collider.js
circleCollider.js
Saucers
saucer.js
smallSaucer.js