3

Here is the circle class:

public class Circle {
    private double radius;

    private double x;
    private double y;
}

How can I tell if two objects from this class (circles) are colliding?

P.S. Can you use the method that avoids taking a square root?

Conner Ruhl
  • 1,693
  • 4
  • 20
  • 31
  • 5
    This question got a detailed answer here: http://stackoverflow.com/questions/1736734/circle-circle-collision – Tom Neyland Dec 19 '11 at 19:29
  • A method like circle1.collide(circle2) needs access to circle2's radius, x and y but those are private. – Steve C Dec 19 '11 at 19:36
  • 1
    @SteveC: assuming `circle1` and `circle2` are both instances of the same type of object, then that's obviously not an issue. – Edward Thomson Dec 19 '11 at 19:39
  • @instanceOfTom: as far as I can tell this is not the same question at all. That other questions you linked to asks how to determine **where** two circles do collide. Here the OP simply asks if two circles collides or not, which is even simpler. In a lot of games, for example, it really doesn't matter **where** two circles collides but simply if they collide or not. – TacticalCoder Dec 19 '11 at 19:57
  • @user988052 if you look at the 2nd answer: http://stackoverflow.com/a/1736815/64301 is has the exact equations needed. The answers offer additional details, but they **do** answer the question of how to tell **if** two circles collide. – Tom Neyland Dec 19 '11 at 21:24

4 Answers4

9
double xDif = x1 - x2;
double yDif = y1 - y2;
double distanceSquared = xDif * xDif + yDif * yDif;
boolean collision = distanceSquared < (radius1 + radius2) * (radius1 + radius2);
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • 2
    if the x,y,radius have approximation errors (which is often the case when working with floating-point numbers) then floating-point comparisons are best done using an epsilon... – TacticalCoder Dec 19 '11 at 19:59
4
dx = x2 - x1;
dy = y2 - y1;
radiusSum = radius1 + radius2;
return dx * dx + dy * dy <= radiusSum * radiusSum; // true if collision

The link from @instanceofTom in the comments is better... with pictures.

toto2
  • 5,306
  • 21
  • 24
1

Here's the updated Java solution:

public boolean hasCollision(Circle circle){
    double xDiff = x - circle.getX();
    double yDiff = y - circle.getY;

    double distance = Math.sqrt((Math.pow(xDiff, 2) + Math.pow(yDiff, 2)));

    return distance < (radius + circle.getRadius());
}
Briguy37
  • 8,342
  • 3
  • 33
  • 53
  • You're comparing square of distance between centres to either radius. – Pete Kirkham Dec 19 '11 at 19:31
  • @PeteKirkham Yes, sorry about that. I had thrown the JavaScript solution out there before reviewing it, and then noticed it was incorrect as I was updating it to Java. – Briguy37 Dec 19 '11 at 19:50
1

The circles will touch when the distance between their centres is equal to the sum of their radii, or collide when the distance is less.

Since we are using absolute distance, it is Ok to compare the square of the distance between centres with the square of the sum of the radii.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171