This is the 5th Tutorial in the pre-ludum dare series that I’ve been putting together. Find tutorial 4 here.
Concept of Collision Detection
So why do we need things to collide? Well if we don’t check to see if two things have collided then we don’t really have much of a game. We’d fall through floors in platformers, no-one would be able to shoot us and we’d be able to walk through walls in first person shooters. So with our ateroids clone (as it’s shaping up to be) we need to calculate whether the centre of the objects is too close together.
We can do this by using FiltoidCollisionCheck() in FiltoidUtils.js. We can assign this to the player object and then pass in any enemy objects then we can use their locations and sizes to calculate this. The function calculates the distance between the two points and then if this distance is less than the two sizes then we shall not let the movement forward occur and (in this case) we’ll reset the spacecraft to the start point (this will look a little like death).
In the next tutorial we’ll make a collision manager…