Smooth Jumping for Platformers
Few genres are more popular or more recognizable than platformers. They have a magic simplicity - your character will jump and possibly attack - that has been iterated on with countless twists and enhanced with many memorable characters. At the core of the best of these games, however, is a shared element. Something so essential it can make or break a game.
Tight controls.
Since moving around platforms is such an integral part of the platformer experience, it is crucial that this feels good to the player. Getting controls to feel just right though is a balancing act between in-game physics, collision, and game design. It is a feat for the truly meaty at heart.
If this isn't you, fear not! Read on to find out more about these elements, and how we worked through them to make the jumping of Kon, a ghost minotaur, feel great in our upcoming NES platformer, The Meating!
Dead Minotaurs Can't Jump
Collisions are one of the most basic of game elements. They are necessary for the character to "feel the ground under his feet."
For example, if we stop handling collisions for Mario, he will not be able to break brick blocks, because he will move through them. He will not even be able to walk on the ground, because he will fall through it. If we do not handle the collisions of Bomber Man, he will walk through the walls, and if we take away the collisions from his enemies, Bomber Man will not be able to hide from them.
In most cases, it is enough to fit an ABCD Rectangle to the player character's sprite, as we did with Kon below:
This becomes the collision box for the character. Our challenge is to ensure that none of these four points would ever intersect with "solid" elements of the background: walls, ceiling, ground, etc.
AC and BD collisions prevent the player from passing through the left and right walls respectively. The collision AB does not allow the player to jump through solid blocks above, and the collision CD does not allow the player to fall through the ground.
However, there are problems.
Minding the Gap
Let's suppose a player needs to jump on the platform above, through this gap in the platform:
Since the width of the rectangle that describes the player's collision box has a size of 16 pixels, and the gap between the blocks is also 16 pixels, the player must set the character's sprite exactly - to the individual pixel - between these blocks before the jump.
Needless to say, this can become frustrating rather quickly. If the player is forced to repeat this aiming process, again and again, the game will quickly bore him or her. Fixing this problem is necessary for achieving tight controls and a good game feel.
How do we do it? One solution would be to reduce the width of our ABCD Rectangle by 5 pixels left and right:
Now we can safely jump. But there will be another problem:
As we can see, the player's sprite overlaps the background block by 5 pixels. To remedy this, we have to align the player's sprite into a 16x16 pixel grid during the jump.
This trick is used in many great platformers, like Super Mario Bros.:
In fact, it is used in many types of game, including action games like Bomber Man:
To do this, we again need to check for a collision of the right and left edges of the player's sprite with background elements:
If the AB side intersects with the solid background tile, we will move the character's sprite to the right by one pixel. If the CD side intersects with the solid background, then we will move the player to the left. We will repeat these steps until the sprite is completely aligned:
It seems to work! Now Kon, our ghost minotaur hero, can jump like the platforming greats. Test out the tight controls and smooth jumping yourself, as you play The Meating and help Kon recover his tragically-sourced meat!
Alex Tokmakov is a retro dev and meat enthusiast. He is the beefy programmer bringing this ghost minotaur game to life.