Question

Possible Duplicate:
Circle-Rectangle collision detection (intersection)
How to determine the collision of a circle with a rectangle?

I want to detect the intersection or collision of a circle with an generaly positioned block. The block is rectangular and may be any size or rotation.

What is the math behind this?

Was it helpful?

Solution

Write one function that checks if an arbitrary segment ab intersects a circle C. Write a second function that checks if a point p is inside a circle C. Then call the 1st function four times, for the four rectangle sides, and if all those fail, call the 2nd function on one corner (to check if the rectangle is entirely inside the circle). Depending on your definition of "collision," you might have to also check if the circle is entirely inside the rectangle!

There are various ways to speed up the computations, in case you are going to invoke this millions of times in a real-time simulation. The most obvious is to first check against the square S circumscribing the circle C, and only then proceed to the (slightly) more costly circle test.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top