Wikipedia defines collision detection as “the computational problem of detecting the intersection of two or more objects”. In real life you cannot go through objects but in a computer simulation objects can overlap and go through each other. In order to resemble reality we need a way to detect when objects are overlapping.
Imagine two billiard balls, one of them is moving towards the other. In the real world they collide and each ball goes in a different direction. When programming a simulation we need to check every frame for collision, and when a collision is found resolve it.
Collision detection is a broad and complex problem, and if that wasn’t enough it is also coupled with simulations and physics.
In this article I will only talk about collision detection of two or more circles in 2 dimensions(although the same algorithm works for 3D spheres), and to make it a little more fun I will explain one of the many ways of making things interact besides checking if they are colliding or not.
Here’s a preview of what we will be implementing.
I have to draw the knowledge required line somewhere, and this is it. Nothing of what i’m about to explain/use is hard or complex. You can learn it in a few minutes. The internet is full of guides and this one will do the job just fine.
These are the things you need to know:
- What is a circle’s radius
- What is a vector
- Subtracting two vectors
- Magnitude of a vector
- Normalizing a vector
- Scaling a vector
I will be programming in C since it’s my favourite language, feel free to translate the code to your language and style of your choice. Let’s start by defining vector and circle structs.
Now let’s define some functions we will need.
Checking if two circles collide is really easy. If the distance between the two circle’s center is less than the sum of their radii, they are colliding. Play around with the following example and you should get the idea. The black line represents the distance between the two circles, and the radii sum is just the sum of both circle’s radii.
The following function returns just a boolean, wether the input circles are colliding or not.
Here we are going to do the red and blue circle interaction seen on the first section.
In order to move the circles apart we need what is called the “minimum translation vector”, in other words it’s how much the circles are overlapping. With this length, we can simply move both circles apart or just one as i did with the red one.
To get the minimum translation vector we subtract the distance from the radiisum and take the absolute value of the result. After that we need to get the normalized direction vector of the distance. After that we just need to add the movement to the red circle.
Math in words makes everyone dizzy, and i’m not good at explaining. Code is a more elegant medium.
This last section is a bit more complicated, if you don’t understand something, i recommend doing sketches on pen and paper, that usually helps me.
I’m happy you read this whole thing, thank you, you are awesome.
Until next time.