Detecting collisions
We just need to know when certain objects from our game touch certain other objects. We can then respond to that event in an appropriate manner. In our classes, we have already added functions to call when our objects collide. They are the following:
The
Player
class has ahit
function. We will call it when a zombie collides with the player.The
Zombie
class has ahit
function. We will call it when a bullet collides with a zombie.The
Pickup
class has agotIt
function. We will call it when the player collides with a pickup.
If necessary, look back to refresh your memory on how each of those functions works. All we need to do now is detect the collisions and call the appropriate functions. We will use rectangle intersection to detect collisions. This type of collision detection is really straightforward (especially with SFML). We can think of drawing an imaginary rectangle—we can call it a hitbox or bounding rectangle—around the objects we want to test for collision, and...