Game Development Reference
In-Depth Information
Chapter 27
Game Physics
27.1 Introduction
In the previous chapters, we have seen how to create an animated character. We
have also shown how you can load levels from files and how to build a tile-based
game world. One of the most important aspects is still missing: defining how the
character interacts with the game world . We can make a character move from left
to right, but if we simply place the character in the level, he/she will only be able to
walk on the bottom of the screen. This is not enough. We want the character to be
able to jump on top of wall tiles, we want the character to fall down if he/she moves
off a wall tile, and we do not want the character to fall off the edge of the screen. For
these things, we need to implement a basic physics system. Since it is the character
interacting with the world, we are going to implement this physics inside the Player
class. There are two aspects of dealing with physics: one is giving the character the
ability to jump or fall, the other is handling collisions between the character and
other game objects, and responding to these collisions.
27.2 Locking the Character in the Game World
The first thing that we are going to do, is lock the character in the game world.
In the examples in Chap. 26 the character could walk out of the screen without any
problem. We can solve this by placing a virtual pile of 'wall' type tiles to the left and
to the right of the screen. We then assume that our collision detection mechanism
(which we have not written yet) will ensure that the character cannot walk through
these walls. We only want to block the character walking out of the left or the right
of the screen. The character should be able to jump out of sight on the top of the
screen. Also the character should be able to fall off the game world through a hole
in the ground (and die, obviously).
In order to build this virtual pile of 'wall' tiles on the left and right side of the
screen, we have to add some behavior to the grid of tiles. We do not want to modify
 
Search WWH ::




Custom Search