Collision detection

Collision detection is one of the key aspects of game programming. For all non-rectangular objects it gets harder and harder the more accurate you want it to be. But today we’re gonna continue just using rectangles. This part describes a simple algorithm to check for intersections.

After we’ve created a functions that does rectangle-rectangle collision detections, we’ll use all of what we’ve learned so far and make a simple game!

Rectangle – rectangle collisions

Instead of checking if two rectangles are inside or touching each other, we’re gonna check for the opposite; if they are outside of each other. The algorithm to do so consists of two steps.

1. Find the x coordinate of the left and right and the y coordinate for top and bottom.

Say we have two rectangles, rect1, and rect2.

Finding the x/y of left/right/top/bottom is very easy.

The process is exactly the same for the second SDL_Rect.

2. Use the 8 edges to check if they are not colliding

Take a look at the drawing below

As you can see, the red rectangle is farther to the right than the blue one. But how do can we easily check that? Simply by checking if redLeft is further to the right than blueRight. If it is ( like it is in our case ) there is no collision. Then it’s just a simple matter of repeating it for the other edges. So we end up with something like this :

This is the final function for you to copy and test out.

If you want to shorten the code, you can remove the comments and replace the variables with the rect. x / y / w / h values like so :

I chose to not do this as the longer version is a bit easier to read and understand.

Our first game!

Our first game is of the “lead the chicken safely across the road” kind of things. Or, as in our case, “lead the square from the rectangle, past all the other squares and to the other rectangle.” You control the square with the arrow keys. If you hit a red square, you’ll end up where you started. If you make it to the other side, you’ll also end up where you started, but at least you have that tiny sense of victory for helping our poor little square!

The code just uses the different things we’ve learned so far, so I won’t explain it other than in code comments and ( hopefully ) descriptive names.

So without further ado, here’s the code :

Feel free to comment if you have anything to say or ask questions if anything is unclear. I always appreciate getting comments.

You can also email me : olevegard@headerphile.com

10 thoughts on “[ SDL2 – Part 5 ] Collision detection”

1. Razvan says:

Awesome tutorials until now,best SDL2 tutorials I could find. We need more people like you.
Now questions time!
Can you please tell me more about “auto”? Why did you once used “auto” (line 243) and once “const auto” (line 292). And then what is with the scope? auto &p : enemies?

1. olevegard says:

Hello there

auto is a keyword you can use instead of specifying type. So instead of int i = 0 you can write auto i = 0. This also applies to classes and structs. The only requirement for using auto is that you have to assign it to something.

const auto is the same only that the variable will be const so you can’t change it. So in the first example you posted, I change p in the loop, thus I can’t use const in the other example, I don’t change p so I can use const

The scope in that case is just the for loop. It's just an easier way of looping through all elements in a collection.

Hope that helps

2. older and older says:

Hello
I think there is a mistake in first explanation about the collisions. In the example:

“Finding the x/y of left/right/top/bottom is very easy.
int left1 = rect1.x;
int right1 = rect1.x + rect2.w;
int top1 = rect1.y;
int bottom1 = rect1.y + rect2.h;

you are using the rect2 values to calculate the edges of rect1.

1. olevegard says:

Hi
I think you are right, thanks for pointing it out 🙂

I updated it now. Sorry for the late reply

3. I have a question about the Enemy struct. I see that it’s declared again. Can you help me understand why is this needed, how it works? Please forgive me on the basic question, I’m in the process of learning C++ in addition to SDL. My background is generally C#. Thanks!

struct Enemy
{
Enemy( SDL_Rect pos_, int speed_, Direction dir_ )
{
pos = pos_;
speed = speed_;
dir = dir_;
}

SDL_Rect pos;
int speed;
Direction dir;
};

1. olevegard says:

Hi 🙂

This is not a redeclaration, but the constructor that’s used to create the object

4. roserobin says:

There are several gaming software which is not compatible with Mac OS-based platforms. The game may be crashed down on the system. The users get an actual solution instructed by apple customer support number to know in advance.

5. The person who are not interested or not playing continous games then it is hard for that non programmers.

6. Use the “Action” or the “Tools” Button to find the print function. Then epson wireless printer setup on the Wi-Fi network and print. Air Print takes care of the printing in the background. It’s built into iOS so there’s no printer software or cables to connect.