If it is in 2d, you are intersecting a circle with a rectangle? If the rectangle is aligned to the x- and y-axes, and the centre of the circle is (a, b), then the point on the rectangle between the two (or none) points of intersection has y=b. If the leading edge of the "box" has x = m, then the distance from the centre of the circle to the edge of the box is (m - a). If the radius of the circle is r, the two points of intersection are
Code: Select all
(m, b + sqrt(r^2 - (m - a)^2)) and (m, b - sqrt(r^2 - (m - a)^2))
and the normal is (-1, 0) or (1, 0) depending whether m > a or m < a. If the box is not axis-aligned, you can either rotate the world until it is, get the information you need and rotate it back, or drop a normal from the circle to the box. The normal to the vector (a, b) is the vector
Code: Select all
(b, -a)/sqrt(a^2 + b^2) or (-b, a)/sqrt(a^2 + b^2)
The same considerations apply to the top/bottom of the box.
If you just want to determine if the circle is touching the box, then the difference between the x coordinate of the centre of the circle and the x-coordinates of the vertices of the box, or the y-coordinate of the centre of the cirle and the y-coordinates of the vertices of the box, is r, the radius of the circle, for some vertex. Of course, if one of these quantities is less then r, the circle intersects the box.
Oops, I wrote the wrong normal.