[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] circle rect collision code... from irc.



Hey Joe,

I agree. The code seems to cover all cases. I was a bit quick with my assessment. sorry.
I also found this[1] code on stackoverflow which has pretty much the same idea but
seems a little more optimized and comes with a nice explanation (including an image).

Cheers,
//Lorenz

[1] http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection/402010#402010


On 03/25/2011 03:34 PM, Joe Ranalli wrote:
Lorenz,
I believe that would circle be caught by the code, because the "side" collide condition is on an OR basis.  So if the
circle center is vertically within the rect (your circle), OR horizontally within the rect, it will return True.  That
seems to cover most cases of intersection in a pretty clever way that looked like a bug to me at first too.  In any
case, it looks to me like any time the circle center is outside the rect in both directions will demand corner
intersection for a possible collision, and thus all the collisions are caught.

I think that in retrospect, the only bug (that I fixed already in the code) was a mistake in initial conditional testing
for rectangle collisions.

Joe

On Fri, Mar 25, 2011 at 9:56 AM, <don@xxxxxxxxxxxxxxxxx <mailto:don@xxxxxxxxxxxxxxxxx>> wrote:

    Hey,

    as Joe said: there are several bugs.
    for example it will miss the collision of this setup:

    circle:
      radius = 10
      x, y   = 10, 10

    rect:
      x, y = 15, 5
      w, h = 10, 10

    I hope that is correct (I just did the example in my head).
    The circle should be next to the rect cutting the same side of the rect twice.
    The code only seems to check if the center of the circle is inside the rect or if any of the corners are inside the
    circle.

    sincerely yours,
    Lorenz



    On Fri, 25 Mar 2011 11:40:59 +0000, René Dudfield <renesd@xxxxxxxxx <mailto:renesd@xxxxxxxxx>> wrote:

        Hi,

        I think it's a circle rect collision function.


        On Fri, Mar 25, 2011 at 11:30 AM, Joe Ranalli <jranalli@xxxxxxxxx <mailto:jranalli@xxxxxxxxx>> wrote:

            Was there any background on the purpose of that code?  It seems at a glance
            like there's several bugs.

            On Fri, Mar 25, 2011 at 4:39 AM, René Dudfield <renesd@xxxxxxxxx <mailto:renesd@xxxxxxxxx>> wrote:


                Hey,

                I noticed someone on irc left some code... but they aren't on irc any
                more, and I wasn't around when they mentioned the code.

                The code is here:
                http://collabedit.com/exb5k

                Want to discuss it here?



                cya.