MonoGame: Collision Detection

Reacting to Solid Objects

Collision detection is an important technique that you should learn. It is a requirement for every game ever made. I can’t think of any game that does not need collision detection, because it is such an essential aspect of gameplay. Without collisions, there is no action, goal, or purpose in a game. There is no way to interact with the game without collisions taking place. In other words, collision detection makes the sprites in a game come to life and makes the game believable. Not every situation in which collision detection occurs necessarily means that something is hit or destroyed. We can also use collision testing to prevent the player from going into certain areas (such as a lake or mountain area that is impassible).

Rectangle Intersection

Collision detection is pretty easy to do using the System.Drawing.Rectangle class. First, you will create a rectangle based on the position and size of one object, such as a sprite. Then you will need to create a similar rectangle for a second object. Once you have two rectangles, which represent the position and size of two objects, then you can test to see whether the rectangles are intersecting. We can do this with a function in the Rectangle class called IntersectsWith()

Collision Test

public bool IsColliding(ref Sprite other)
{
//test for bounding rectangle collision
bool collision = Bounds.IntersectsWith(other.Bounds);
return collision;
}

Hint
You will get better results in your game if you make sure there is very little empty space around the edges of your sprite images, since the image is used as the bounding rectangle!

Let’s dissect this method to determine what it does. First, notice that IsColliding returns a bool value (true or false). Notice also that there’s only one Sprite passed by reference (ref). Thus, the entire sprite object in memory (with all of its properties and methods) is not copied as a parameter, only a reference to the sprite is passed. This method is small thanks in part to the Sprite.Bounds property, which returns a Rectangle representing a sprite’s position and size as it appears on the screen. Thus, two rectangles are essentially created based on the position and size of each sprite, and then IntersectsWith() is used to see whether they are overlapping each other.

Definition

“Collision” is a misnomer since nothing actually collides in a game unless we write code to make it happen. Sprites do not automatically bump into each other. That’s yet another thing we have to deal with as game programmers!

Often, the code to perform a collision test is trivial compared to the code we need to write to respond to the collision event!

This is how you can use the code (mange the isColliding method):

public void Game_Update(int time)
{
if (MyPlayer.Alive)
{
//see if arrow hit spider
if (MyPlayer.IsColliding(ref spider))
{
arrow.Alive = false;
score++;
spider.X = 800;
}

//see if arrow hit dragon
if (MyPlayer.IsColliding(ref dragon))
{
arrow.Alive = false;
score++;
dragon.X = 800;
}
//see if arrow hit zombie
if (arrow.IsColliding(ref zombie))
{
arrow.Alive = false;
score++;
zombie.X = 800;
}

//see if arrow hit skeleton
if (arrow.IsColliding(ref skeleton))
{
arrow.Alive = false;
score++;
skeleton.X = 800;
}
}
}

References:
Visual C# Game Programming for Teens
By: Jonathan S. Harbour

Learn 2D Game Development with C#
By: Jebediah Pavleas; Jack Keng-Wei Chang; Kelvin Sung; Robert Zhu

One thought on “MonoGame: Collision Detection

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

*