# How does this circle collision detection math work?

I'm going through the wildbunny blog to learn about collision detection. I'm confused about how the vectors he's talking about come into play.

Here's the part that confuses me:

p = ||A-B|| – (r1+r2)

The two spheres are penetrating by distance p. We would also like the penetration vector so that we can correct the penetration once we discover it. This is the vector that moves both circles to the point where they just touch, correcting the penetration. Importantly it is not only just a vector that does this, it is the only vector which corrects the penetration by moving the minimum amount. This is important because we only want to correct the error, not introduce more by moving too much when we correct, or too little.

N = (A-B) / ||A-B||

P = N*p

Here we have calculated the normalised vector N between the two centres and the penetration vector P by multiplying our unit direction by the penetration distance.

I understand that p is the distance by which the circles penetrate, but I don't get what exactly N and P are. It seems to me N is just the coordinates of the 3rd point of the right trianlge formed by point A and B (A-B) then being divided by the hypotenuse of that triangle or distance between A and B (||A-B||). What's the significance of this?

Also, what is the penetration vector used for? It seems to me like a movement that one of the circles would perform to get un-penetrated.

I assume that A and B are the centers of the two spheres: Sa, Sb.

V = A - B is the vector that moves the center of Sa to the center of Sb: you see that A + V = B.

||A - B|| is the magnitude of V or modulus or length - as you prefere - that is the distance between the two spheres' centers: p tells you how much the spheres are "too close" to each other.

N = V / ||A - B|| is a unit vector (length = 1) that shares the same direction of V so P = N·p si a vector that has the same direction of V and the length of p.

You don't want to move Sa toward Sb a little more so you will add P to B moving Sb far way from Sa in the right direction (N) and for the correct amount (p).

I will assume that A and B are the centers of the respective circles.

First, we have two circles that move by some given velocities. When you update the position of the circles at the beginning of a new frame, their position will change by Velocity * diff, where diff is the time passed from last frame. If the collision engine finds that these two circles are now penetrating each other, then it must move one or both of them so that they will not be still penetrating. The most realistic way of doing this is by trying to minimize diff so it will find the exact moment when the two circles collide and then stop them here, but that's irrelevant because that's not how the article you're reading does. It assumes it is good enough to move them by the minimum distance possible for them to not be still penetrating.

Let's say you want to move circle 1 in case of penetration. You know the penetration (you calculate it by p = ||A-B|| – (r1+r2)) and that's the distance you will move your circle across for them to be just in touching contact (remember you want to move it by as little as possible and that's p), so you must find the direction along which you need to move the first circle.

Look at the image and you will see that N (the direction vector) is exactly A - B. It must be normalized before multiplying with p (a normalized vector multiplied by a value will have a magnitude equal to that value, e.g. moving circle 1 p distance along N). N = (A-B) / ||A-B|| gives us that direction and P = N*p gives us the penetration vector, which must be added to the position of circle 1 so they will be just in touching contact. P is exactly the line I highlighted in green.

Hope it helps. Let me know if I was unclear.

EDIT: I haven't seen the date of this question when I answered it, so I don't think the OP still needs an answer. Hope it helps other people.

The text is a bit unclear, using the word normal incorrectly. N is the normalised vector pointing from B to A, meaning it has the same direction as B to A but length 1.

What are you trying to do by the way? The blog talks a lot of collision detection, but seemingly nothing about collision resolution, which depend on what kind of physics you'd like.

• Probably he just wants to avoid a compenetration due a single frame/step motion by moving the spheres away so they are simply in touch. – FxIII Jun 27 '11 at 11:10