Game Development Reference
In-Depth Information
velocity.Y += 55;
What happens is that if the character has a negative velocity, this velocity slowly
becomes smaller until it reaches zero, and then starts to increase again. The effect is
that the character jumps to a certain height, and then starts falling down again, just
like in the real world. However, the collision detection mechanism now becomes
even more important. If there is no collision detection, the character would simply
start falling down at the start of the game!
27.6 Collision Detection
Detecting collisions between game objects is a very important part of simulating
interacting game worlds. Collision detection is used for many different things in
games: detecting if you walk over a power up, detecting if you collide with a pro-
jectile, detecting collisions between the character and walls or floors, and so on.
Given this very common occurrence, it is almost strange that we did not have the
need for collision detection in our previous games. Or wait, didn't we? Look at this
code from the Update method in the PaintCan class from the Painter game:
Vector2 distanceVector = ((Painter.GameWorld.Ball.Position
+ Painter.GameWorld.Ball.Center)
(position + Center));
if (Math.Abs(distanceVector.X) < Center.X && Math.Abs(distanceVector.Y) < Center.Y)
{
Color = Painter.GameWorld.Ball.Color;
Painter.GameWorld.Ball.Reset();
}
What we are doing, here, is in fact detecting a collision between the ball and the
paint can (though be it in a very rudimentary fashion). We take the positions of the
center of each object, and see if their distance is smaller than a certain value. If so,
we say they collide and we change the color of the canister. If you look at this case
more closely, you can see that we are representing our game objects by basic shapes
such as circles and we check if they collide with each other by verifying that the
distance between the centers is smaller than the sum of the radii of the circles.
So here you see a first, simple example of doing collision checking in games.
Of course, this is not a very precise way of checking collisions. The ball might be
approximated by the shape of a circle, the paint can does not look like a circle at
all. As a result, in some cases a collision will be detected when there is none, and
sometimes a collision will not be detected when the sprites are actually colliding.
Still, many games use simplified shapes to represent the objects when they do colli-
sion detection. Circles are a good candidate to use as a simplified shape for collision
detection. Rectangles are also widely used. Because these shapes bind the object
within, they are also called bounding circles and bounding boxes .The Tick Tick game
Search WWH ::




Custom Search