In order to let the player interact with our gaming world, it is neccessary to know with whom and how the player is able to do something. In Box2D you have the possibility to do this with sensors. Sensors are invisible boxes which are able to track collisions without colliding themselves. Although sensors are useful, we wanted to keep their numbers at a minimum.
The first time we used a sensor was at the beginning of our implementation-phase: Our characters should be able to jump, but shouldn’t keep flying if you hold down the jump-button. So we decided to use a sensor on the bottom of the character to check if he is standing on the ground. At first that worked fine. But then our character was able to jump off vertical walls because of unintended collisions with the side of the sensor. So we made the sensor a few pixel smaller on both sides and the problem was solved.
Then we need sensors to let our player lift, catch and push objects. Because our characters are not able to lift things from far away with telekinesis, we needed sensors to check if there are dynamic objects within his range. To do that we need three new sensors. One above for catching and one on each side of the player for lifting and pushing objects. When the sensor touches an object in viewing direction it allows the character to interact with it.
Next, the players should also be lifted. But it seemed the sensors for interacting with dynamic objects won’t work properly to lift the characters too. The lift-sensors were only outside the player but the characters are able to stand inside each other. So the small player could not be lifted if he was standing inside the big one. Our first impulse was to resize the sensors into the inside of the player, but now he could only lift the small player when looking to the right. This happened because the player to be lifted touched the sensors of both sides and the lifting was overridden by the left sensor. Instead we needed a new sensor in the middle of the character. If the small character now touches the sensor in the center, he can be lifted, no matter what the other sensors say.
Lastly we needed sensors to let the characters die in various ways. Here we distinguish between three deaths:
- Headcrash, getting hit by an object from above
- Squishing, being squished from the sides by a dynamic object
- Shifting, not having enough space to shift
The first way to die was very easy and need only a sensor on the head.
The squishing death was also relatively simple. For that we needed two more sensors which go through the complete right and left side of the players bounding box. Once both sensors detect a collision and one of the collisions object is an dynamic object, the player dies.
The shifting death was somewhat more difficult and required four more sensors. These are smaller and are located to the right, left, up and down within the characters. The player dies if the sensors detects a collision on the right and left, but not touching the top and bottom and vice versa. In addition, of course, if all 4 sensors collide with walls or objects the player dies too.
And thus we have 12 sensors in total (as seen on the big player):
- Red: 1 Jump sensor
- Green: 4 Lift sensors
- Blue: 1 Headcrash, 2 squishing sensors
- Yellow: 4 Shifting sensors
- Pink: Player bounding box (not a sensor)