Circle to Circle collision, checking each circle against all others

Posted by user14861 on Game Development See other posts from Game Development or by user14861
Published on 2012-03-29T22:00:40Z Indexed on 2012/03/29 23:42 UTC
Read the original article Hit count: 265

Filed under:
|
|

I'm currently coding a little circle to circle collision demo but I've got a little stuck. I think I currently have the code to detect collision but I'm not sure how to loop through my list of circles and check them off against one another.

Collision check code:

public static Vector2 GetIntersectionDepth(Circle a, Circle b)
    {
        float xValue = a.Center.X - b.Center.X;
        float yValue = a.Center.Y - b.Center.Y;

        Vector2 depth = Vector2.Zero;

        float distance = Vector2.Distance(a.Center, b.Center);

        if (a.Radius + b.Radius > distance)
        {
            float result = (a.Radius + b.Radius) - distance;
            depth.X = (float)Math.Cos(result);
            depth.Y = (float)Math.Sin(result);
        }

        return depth;
    }

Loop through code:

        Vector2 depth = Vector2.Zero;

        for (int i = 0; i < bounds.Count; i++)
            for (int j = i+1; j < bounds.Count; j++)
            {
                depth = CircleToCircleIntersection.GetIntersectionDepth(bounds[i], bounds[j]); 
            }

Clearly I'm a complete newbie, wondering if anyone can give any suggestions or point out my errors, thanks in advance. :)

© Game Development or respective owner

Related posts about XNA

Related posts about c#