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 that will be called when our objects collide. They are as follows:
- 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 regarding 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 straightforward (especially with SFML). We will use the same technique that we used in the Pong game. The following image shows how a rectangle...