0

What do I have to do so that when I find out about the collision between two circles, I can prevent the two circles from going through one another?

public class CircleVsCircle
{
    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 void CircleVsCircle(CircleBody body1, CircleBody body2)
{
    float radius = (body1.Radius + body2.Radius);
    radius *=radius;
    float distance = ((body1.CenterX - body2.CenterX) *
        (body1.CenterX - body2.CenterX)) + 
        ((body1.CenterY - body2.CenterY) * 
        (body1.CenterY - body2.CenterY));

    if(radius > distance)
    {
        //solve the collision here
    }
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
user3008254
  • 105
  • 4
  • 10
  • 1
    What do you mean by 'solve' it? And do the circles have velocities? – eshs May 11 '14 at 19:11
  • possible duplicate of [circle-circle collision](http://stackoverflow.com/questions/1736734/circle-circle-collision) – ale May 11 '14 at 19:26

2 Answers2

0

I'm assuming your CircleBody class already implement attribute like x position and y position

This is a possible dublicate of circle-circle collision

public void CircleVsCircle(CircleBody body1, CircleBody body2)
{
    float radius = (body1.Radius + body2.Radius);
    radius *=radius;
    float distance = ((body1.CenterX - body2.CenterX) *
        (body1.CenterX - body2.CenterX)) + 
        ((body1.CenterY - body2.CenterY) * 
        (body1.CenterY - body2.CenterY));

    if(radius > distance)
    {
        double deltaXSquared = body1.x - body2.x; // calc. delta X
        deltaXSquared *= deltaXSquared; // square delta X
        double deltaYSquared = body1.y - body2.y; // calc. delta Y
        deltaYSquared *= deltaYSquared; // square delta Y

        // Calculate the sum of the radix, then square it
        double sumRadiiSquared = body1.radius + body2.radius; 
        sumRadiiSquared *= sumRadiiSquared;

        if(deltaXSquared + deltaYSquared <= sumRadiiSquared){
          // body1 and body2 are touching
        }
    }
}
Community
  • 1
  • 1
ale
  • 10,012
  • 5
  • 40
  • 49
0

Here is a class I wrote that deals with circle collision: link

This is the code that deals with the collision of two circles:

public static boolean intersects(Circle circle1, Circle circle2) {

        // Use Pythagorean theorem
        int a = circle1.getCenterX() - circle2.getCenterX();
        int b = circle1.getCenterY() - circle2.getCenterY();
        float c = (float) FloatMath.sqrt((float) (Math.pow(a, 2) + Math.pow(b, 2)));

        if((circle1.getRadius() + circle2.getRadius()) == c || (circle1.getRadius() + circle2.getRadius()) > c) {
            return true;

        }
        else {
            return false;

        }

    }

This basically takes the distance between the two centers of the two circles (using Pythagoras) and makes sure it's less than then two radii of the circle added together. If it is less then we know a collision has happened.

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73