Bob Twinkles
Bob Twinkles

Reputation: 41

Collision detection for circles

I have a simple Java applet that has two user-controlled balls, drawn using java.awt. I need a way to detect a collision with between them. I have an algorithm for detecting collision with the walls:

if (xPosition > (300 - radius)){
   xSpeed = -xSpeed; 
}
else if (xPosition < radius){
   xSpeed = -xSpeed; 
}
else if (yPosition > (300 - radius)) {
   ySpeed = -ySpeed;
}
else if (yPosition < radius){
   ySpeed = -ySpeed;
}
xPosition += xSpeed;
yPosition += ySpeed;

and for the second ball:

if (xPosition2 > (300 - radius)){
   xSpeed2 = -xSpeed2; 
}
else if (xPosition2 < radius){
   xSpeed2 = -xSpeed2; 
}
else if (yPosition2 > (300 - radius)) {
   ySpeed2 = -ySpeed2;
}
else if (yPosition2 < radius){
   ySpeed2 = -ySpeed2;
}
xPosition2 += xSpeed2;
yPosition2 += ySpeed2;

Upvotes: 4

Views: 1802

Answers (4)

hjuste
hjuste

Reputation: 175

This Link is pretty useful!

Circle-Circle Collisions

It's very detailed and didatic


At the bottom of that page there are another links, to even more detailed stuff!


I used the Distance Between Centers method --- Circles

By measuring the distance between each center you can say if they are colliding. The distance should never be more then the sum of the 2 radius.

Here's what I did:

private boolean checkDrawContains(ShapeDrawable newHole) 
{
    long newCenterX = newHole.getBounds().left + (newHole.getBounds().width()/2); //Get the center of my shapes
    long newCenterY = newHole.getBounds().top + (newHole.getBounds().height()/2);

    for(ShapeDrawable hole: mHoles) // I was storing the circles in an ArrayList
    { 
        long centerX = hole.getBounds().left + (hole.getBounds().width()/2); //Get the center of my shapes
        long centerY = hole.getBounds().top + (hole.getBounds().height()/2);
        long x = centerX - newCenterX;
        long y = centerY - newCenterY;
        long aux = (long) ((Math.pow(Math.abs(x),2)) + (Math.pow(Math.abs(y),2))); //Pythagoras the hard way :P
        long distance = (long) Math.sqrt(aux);
        long sRads = (newHole.getBounds().width()/2) + (hole.getBounds().width()/2);

        if(distance <=  sRads ) {
            return true;  //Is Colliding!
        }
    }
    return false; // Is not Colliding!
}

Upvotes: -1

upt1me
upt1me

Reputation: 1

 public boolean colliding(Ball anotherBall) {
    double xDelta = (this.x + this.ballSize/2 + this.dx) - (anotherBall.x + anotherBall.ballSize/2 + anotherBall.dx);
    double YDelta = (this.y + this.ballSize/2 + this.dy) - (anotherBall.y + anotherBall.ballSize/2 + anotherBall.dy);
    double distance = Math.sqrt(Math.pow(xDelta, 2) + Math.pow(YDelta, 2));

    return (distance <= this.ballSize/2 + anotherBall.ballSize/2);

}

Upvotes: 0

Beothorn
Beothorn

Reputation: 1317

Use http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/Point2D.html, there's a distance method there, if it's less than the radius they're colliding.

EDIT: Err, less than the radius * 2 , sorry

Upvotes: 6

SyntaxT3rr0r
SyntaxT3rr0r

Reputation: 28333

There's Point2D in Java or you can do it yourself, it is trivially easy for circle/circle collisions or sphere/sphere collisions.

int distXX = (xPosition1 - xPosition2) * (xPosition1 - xPosition2);
int distYY = (yPosition1 - yPosition2) * (yPosition1 - yPosition2);
if ( radius*radius > distXX * distYY ) {
   ...  // There's a collision
}

Upvotes: 1

Related Questions