0
\$\begingroup\$

I have two circles, one of the circles is static the other is moving around. I have a method that checks whether the two circles collide. How to make the two circles don't overlap when colliding?

public void UpdateGame()
{
    //keyboard input and moving the circle here

    if(CollisionDetection(dynamicCircle,staticCircle)
    {
        ResolveOverlap(dynamicCircle,staticCircle);
    }
}

public class CircleBody
{
    public Vector2 position {get;set;} // top left corner
    public float CenterX;
    public float CenterY;
    public int Width { get; set; }
    public int Height { get; set; }
}

public bool CollisionDetection(CircleBody body1, CircleBody body2)
{
    float radius = body1.Radius + body2.Radius;
    float distanceBetweenTwoCircles = ((body1.CenterX - body2.CenterX) *
        (body1.CenterX - body2.CenterX)) + 
        ((body1.CenterY - body2.CenterY) * 
        (body1.CenterY - body2.CenterY));

    if(radius > Math.Sqrt(distanceBetweenTwoCircles))
    {
        return true;
    }
    return false;
}

public void ResolveOverlap(CircleBody body1, CircleBody body2)
{
    //answer to my question
}

If it wasnt clear by now here is my code for solving that with two rectangles I want the same thing to happen to the circles.

public void ResolveOverlap(RectangleBody body1, RectangleBody body2)
{
    double widthY = (0.5 * (body1.Width + body2.Width)) * (body1.CenterY - body2.CenterY);
    double heightX = (0.5 * (body1.Height + body2.Height)) * (body1.CenterX - body2.CenterX);

    if (widthY > heightX)
    {
        if (widthY > -heightX)
        {
            //collision - top
            body1.Position.Y = body2.Position.Y + body2.Height;
        }
        else
        {
            //collision - right
            body1.Position.X = body2.Position.X - body1.Width;
        }
    }
    else
    {
        if (widthY > -heightX)
        {
            //collision - left
            body1.Position.X = body2.Position.X + body2.Width;
        }
        else
        {
            // collision - bottom
            body1.Position.Y = body2.Position.Y - body2.Height;
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Here is a Method that I use for circle vs circle collision :

public static bool CirclesIntersect(Circle c1, Circle c2, out float depth)
    {
        float distance = (c1.Position - c2.Position).Length();
        float sumOfRadii = c1.Radius + c2.Radius;

        depth = sumOfRadii - distance;
        if (depth < 0)
        {
            return false;
        }
        return true;
    }

The depth float that is passed out will give you the smallest distance you need to separate the circles. You can use the depth value like this :

float depth;
if (CirclesIntersect(dynamicCircle, staticCircle, out depth))
{
     Vector2 direction = dynamicCircle - staticCircle;
     direction.Normalize();

     dynamicCircle.CenterPosition += direction * depth;
}

If you want to collide 2 moving circles you can find the direction opposite of both centers and create the "push" vector by dividing depth by 2... something like this :

float depth;
if (CirclesIntersect(dynamicCircleOne, dynamicCircleTwo, out depth))
{
     Vector2 direction_One = dynamicCircleOne - dynamicCircleTwo;
     direction_One.Normalize();

     Vector2 direction_Two = dynamicCircleTwo - dynamicCircleOne;
     direction_Two.Normalize();

     dynamicCircleOne.CenterPosition += direction_One * (depth / 2);
     dynamicCircleTwo.CenterPosition += direction_Two * (depth / 2);
}

Note I haven't compiled this code or anything so there could possibly be an issue, but I hope that this will at least help push you in the right direction.

There are probably a lot of optimizations that could be done to these methods but I haven't had the need to yet. Just something to keep in mind.

\$\endgroup\$
2
  • \$\begingroup\$ I would like to ask you another question but first I want to thank you for your previous answer. It really did help. How can I make the same thing with the collision between a circle and a rectangle, with a perfect pixel collision detection? \$\endgroup\$ May 18, 2014 at 19:27
  • 1
    \$\begingroup\$ Instead of tacking on more questions, like a forum, you should ask this question separately. \$\endgroup\$
    – igrad
    May 19, 2014 at 4:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .