Game Development Reference
In-Depth Information
Figure 8-7. A triangle cannon that shoots red rectangles. Impressive!
That's enough physics for your purposes. With this simple model, we can simulate much more
than cannonballs. Super Mario, for example, could be simulated in much the same way. If you
have ever played Super Mario Brothers , you likely noticed that Mario takes a bit of time before
he reaches his maximum velocity when running. This can be implemented with a very fast
acceleration and velocity capping, as in the pseudocode of the last section. Jumping can be
implemented in much the same way as shooting the cannonball. Mario's current velocity would
be adjusted by an initial jump velocity on the y axis (remember that you can add velocities like
any other vectors). You would always apply a negative y acceleration (gravity), which makes
him come back to the ground, or fall into a pit, after jumping. The velocity in the x direction is
not influenced by what's happening on the y axis. You can still press left and right to change
the velocity of the x axis. The beauty of this simple model is that it allows you to implement very
complex behavior with very little code. You can use this type of physics when you write your
next game.
Simply shooting a cannonball is not a lot of fun. You want to be able to hit objects with the
cannonball. For this, you need something called collision detection, which we'll investigate in the
next section.
Collision Detection and Object Representation in 2D
Once you have moving objects in your world, you want them to interact. One such mode of
interaction is simple collision detection . Two objects are said to be colliding when they overlap
in some way. We already did a little collision detection in Mr. Nom when you checked whether
Mr. Nom bit himself or ate an ink stain.
Collision detection is accompanied by collision response : Once we determine that two objects
have collided, we need to respond to that collision by adjusting the position and/or movement
of our objects in a sensible manner. For example, when Super Mario jumps on a Goomba, the
Goomba goes to Goomba heaven and Mario performs another little jump. A more elaborate
example is the collision and response of two or more billiard balls. We won't need to get into
this kind of collision response now, as it is overkill for our purposes. Our collision responses will
usually consist of changing the state of an object (for example, letting an object explode or die,
collecting a coin, setting the score, and so forth). This type of response is game dependent, so it
won't be discussed in this section.
 
Search WWH ::




Custom Search