Multiphase collision detection
We have seen that our player character simply falls though the world and into oblivion. Of course we need the player to be able to stand on the platforms. Here is what we will do.
We will provide every object that matters with a hitbox as we can then provide methods in the Player
class to test if a hitbox has made contact with the player. Once per frame, we will send all hitboxes that have not been clipped by the viewport to this new method where a collision can be tested for.
We do it like this for two main reasons. Firstly, by sending only unclipped hitboxes for collision testing, we drastically reduce the number of checks, as described in Chapter 3, Tappy Defender – Taking Flight, in the section Things that go bump – collision detection. Secondly, by handling the checks within the Player
class, we can give the player multiple different hitboxes and respond slightly differently according to which one is hit.
Let's create our own class for a hitbox, so we can...