Circle to Rectangle Collisions - HELP

BlitzMax Forums/BlitzMax Programming/Circle to Rectangle Collisions - HELP

daaan(Posted 2006) [#1]
Does anyone have any good methods of doing this?

Thanks.


Eric(Posted 2006) [#2]
I would say, if the Radius of the circle is within the bounding Rectangle it is colliding.

R=Radius
X,Y are the Center.

X1,Y1 and X2,Y2 and the two corners of the rect.

If (X+R)> X1 and (Y+R)> Y1 Then Collide
If (X-R)< X2 and (Y-R)< Y2 Then Collide


puki(Posted 2006) [#3]
Oooh.

However, what if you had to do it with a starfish shape and a giraffe shape?

Plus, the starfish shape morphs into a hippopotumus shape every 10 seconds and remains hippopotomus shaped for 5 seconds before morphing back into a starfish shape. Whilst that is all going on the giraffe shape morphs into a piece of amphibious landing craft (for the same time-scale as aforementioned).


Eric(Posted 2006) [#4]
OOOOO kaaayyyy


puki(Posted 2006) [#5]
Yeh, not so smart now are we?

I deal with big-boy stuff - not square and circle stuff.


sswift(Posted 2006) [#6]
I'm not positive, but I suspect that Eric's algorithm won't work. My intuition tells me that he is effectively testing the circles bounding rect vs the square's bounding rect. Also I recall a long time ago when I was making my first version of my shadow system which cast shadows in 2D from above that finding out if a circle was intersecting a square was more complicated than you'd think it would be.

If I recall the way I did this test in my shadow system, (which also works with a sphere and a box), is to determine how far the center of the circle is from each of the lines, with a positive value if it is on the "outside" side of the line, and a negative value if it is on the "inside" side of the line. You subtract the radius of the circle from this value.

If the resulting value is < 0 for all sides, then the circle is intersecting the box. You may be able to test if it is completely inside the box by checking to see if the resulting value is less than < Radius for all sides.

I'm not positive about how accurate this is either though. There was some test I was doing which was somewhat inaccurate and that was okay because it didn't matter if I was a little sloppy around the corners of the box.


sswift(Posted 2006) [#7]
This might be of some help. From my physics notes:



-----------------------------------------------------
Finding the closest point on a line to another point.
-----------------------------------------------------
(x1, y1, z1), (x2, y2, z2) = ends of line segment
(x3, y3, y3) = point


u =

(x3 - x1)(x2 - x1) + (y3 - y1)(y2 - y1) + (z3 - z1)(z2 - z1)
-----------------------------------------------------------
(x2 - x1)(x2 - x1) + (y2 - y1)(y2 - y1) + (z2 - z1)(z2 - z1)



u = the location between the two end points of the line which is closest to the point.

If u is not between 0 and 1 then the closest point is not between P1 and P2


----------------------------
LINE-SPHERE-INTERSECTION
----------------------------

To find out if a line intersects a sphere, find the closest point on the line to the center of the sphere.
Then calculate the location of that point using U.
And finally, find out the if the distance from that point to the center of the sphere is less than the radius of the sphere.




Why might it be of help? Well, if you imagine each side of your box is a segment of an infinite line, then you have four lines, and the above will give you the point on each line that is closest to the center of the circle. This might help you figure out if the circle is in the box.

For example, if all four points are on the portions of the lines which contain the edges of the box, then you know the center point of the circle is inside the box.

But I dunno if that would extend to finding out if the circle is in the box. It's an interesting problem though.


Eric(Posted 2006) [#8]
Yep,

You are right Shawn, Thanks.

Eric


Haramanai(Posted 2006) [#9]
Check out this: Elipsoid Coillision.