Circle collision detection

I have a simple Java applet with two user-managed balls drawn using java.awt . I need a way to detect a collision between them. I have a wall collision detection algorithm:

 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; 
  • Applet sized 300 pixels by 300 pixels.
  • radius stores the radius of circles.
  • xPosition and xPosition2 store x coordinates for two balls.
  • yPosition and yPosition store the y coordinates for two balls,
  • xSpeed and xSpeed2 store x speeds for two balls.
  • ySpeed and ySpeed2 store y speeds for two balls.
+4
source share
4 answers

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

EDIT: Err, less radius * 2, sorry

+5
source

There Point2D in Java, or you can do it yourself, is trivially easy for circle / circle collisions or merges / spheres.

 int distXX = (xPosition1 - xPosition2) * (xPosition1 - xPosition2); int distYY = (yPosition1 - yPosition2) * (yPosition1 - yPosition2); if ( radius*radius > distXX * distYY ) { ... // There a collision } 
+1
source
  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); } 
-1
source

This link is very helpful!

Collisions of round circles

It is very detailed and didactic.


There are still links at the bottom of this page, even more detailed information!


I used the Distance Between Centers --- Circles method

By measuring the distance between each center, you can tell if they collide. The distance must never be greater than the sum of radius 2.

Here is 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! } 
-1
source

Source: https://habr.com/ru/post/1307492/


All Articles