# ​Introduction to Collision Detection For Games

## Introduction to Collision Detection For Games

In this tutorial, I will teach you about collision detection!

Physics in games is a great topic, so I will introduce to you collision detection (to compliment last tutorial about movement) and help to make your own collision tests.
First, we will understand the theory and the math, and visualizing the equation needed to do a simple, 1D collision.

After programming this, we do 2D and learn about a very common collision in games: AABB vs AABB (Axis Aligned Bounding Box).

This is clip from the "How to Program a Game in C++" tutorial series. If you want to learn more about game development, be sure to check them out here: https://www.youtube.com/playlist?list...

## Tutorial Transcriptoin

Now there's a problem: Our player goes straight through the wall! We have to make it collide with it.
So let's learn about collision detection.

Game physics is a great topic on its own, so we will learn the introdution to collision detection, so we start making our game actually work.

The basic idea is: we want to move the player to here. We need to know if that move is valid or not!

We have the player position, as well as the half size. We also have the size of the arena.
So all we have to do, is to see if the player position plus its half_size (which is this point) is greater than the arena.
It's that simple!
This collision is a 1D collision, because it only considers one axis.

Let's program that:
If the player p plus the half_size is greater than the arena_y, we are colliding! So what will we do? First, we need to move the player back, so it's not colliding with the wall. All I have to do is to set the p to the arena minus the player_half_size, that will move it back to the limit.
And we should also change the velocity, because we abruptly stopped the player.

Let's see the result.
Awesome, right?

We can set the velocity to different values, like inverting it, so that the player will bounce in the opposite direction.
Pretty cool. We can also make that value larger, so that the player is repelled from the wall.
Hahaha
I think the best thing to do in this case is just to set it to zero.

Now let's do that to the other wall.
It will be the same thing but inverted.
And to the other player as well.

Great! Our players are done!

Now let's move the ball!
For the ball, I'm not going to add an acceleration, because it makes it harder to predict. So the ball will just linearly float around the arena, not losing or gaining much energy.
But I'll set an initial velocity.
And add the position equation with the first derivative.
Now that's moving alright, but not colliding.

So for the ball, we will have to do a more complicated collition, one that will consider both shapes' sizes and the x and y axis.
This shape can be called an AABB, which stands for Axis Aligned Bounding Box. It's simply a box that's not rotation, it's aligned to the coordinates axis.
So we want to test an AABB vs AABB collision.

The idea is the same we did last time: if the right-most point of this guy is greater than the left-most point of this guy, we have a collition. But we also have to consider the other axis, see? So this has to be done for all 4 directions.

Let's first add a collition with the ball's right side (so ball_p plus the ball_half_size. And I'll make a variable out of that), with the player's left side.
When we are colliding, I'll set the ball_p to the limit position, like we did before and invert the x velocity, so it now moves the other way.

Let's compile... Great!!
But if I move the paddle out of the way... See that it's still colliding? So we have to test the other directions as well.
So it will only be colliding if we are penetrating in the positive x, the negative x, the positive y AND the negative y.
Let's see... Perfect!

That is an AABB vs AABB collision.