Collision Detection 2 – Point Circle

Point – Circle

Perhaps one of the most fundamental aspects to collision testing is to see if a point in space is within the bounds of a circle or sphere.
This is done by collapsing the line between the centre of the circle and point into a scalar length, and then comparing it with the radius of the circle.

Here we have 2 positions (Marked by the black dots), the Origin (Centre) of the circle and the Point. If we split the X and Y coordinates we can make a triangle. Pythagoras can then be used to get the Hypotenuse, giving us the length between the 2 points:

A² = B² + C²

So:
CircleOriginToPoint² = (CircleOriginXToPointX)² + (CircleOriginYToPointY)²

Fill in the X and Y Distances:
CP² = (Cx – Px)² + (Cy – Py)²

To get CP from CP² we have to square root:

√(CP²) = Length of the Circle Origin to Point

We can now compare this length to the radius of the circle, If CP <= Radius a collision occurs as the point is inside the circle.

CP

Radius

In our example no collision occurs

This same method can be done with a sphere in 3D, we just add Z values in the Pythagoras step

CP² = (Cx – Px)² + (Cy – Py)² + (Cz – Pz)²

TIP:
You can increase performance of the distance calculation by negating the square root (Which is expensive), and instead squaring the radius for comparison. BE WARNED: know your limits when doing this, the max value of a square on int32 is about 2,000,000,000 that sounds a lot, but its only 46,340 squared, depending on your scale this could be as little as 460meters before your calculations will no longer work!
Remember the sphere’s radius is largest at its origin no matter what direction the ray comes from, which allows the problem to be converted into a 2D point in Circle test.

Leave a Reply