Circle Collision Detection

edited February 2014 in How To...

I am a new user to processing and created this.

The only thing is, I want to be able to take the black ball that you can throw and have it run into other balls when I throw it. It'd be cool if I could get the other balls to do something when they get hit by the black ball, but for now, I am just trying to figure out how to get the other circles to react to the black ball. Anybody know what I mean?

Here's the code I have right now.

int numBalls = 50;
float tbposX; // position variables
float tbposY;
   
float speedtbX; // speed variables
float speedtbY;


Ball[] myBall = new Ball[numBalls];


void setup() {
  size(1500, 900);
  for(int i = 0; i < numBalls; i++) {
        
    float xPos = random(width);
    float yPos = random(height);
    
    float xVel = random(2, 5);
    float yVel = random(2, 5);
    
    int bSize = int(random( 5, 100 ));
    
    float r = random(255);
    float g = random(255);
    float b = random(255);
    
    color myColor = color(r, g, b);
    myBall[i] = new Ball( xPos, yPos, xVel, yVel, bSize );
    
    myBall[i] = new Ball(xPos, yPos, xVel, yVel, bSize, myColor);

  tbposX = width / 2;
  tbposY = width / 2;
  speedtbX = 0;
  speedtbY = 0;  

}
}

void draw() {
 background(200);
//  myBall.update();    // moves it
//  myBall.display();   // draws it

  for(int i = 0; i < numBalls; i++) {
    myBall[i].update();
    myBall[i].display();
}
if (mousePressed) {
    if ((mouseX > (tbposX - 20) && mouseX < (tbposX + 20)) &&
        (mouseY > (tbposY - 20) && mouseY < (tbposY + 20))) {
      speedtbX = mouseX - pmouseX;
      speedtbY = mouseY - pmouseY;
    }
  }
  
 // Calculate the new position
  tbposX = tbposX + speedtbX;
  tbposY = tbposY + speedtbY;
   
  
  if (tbposX > width - 30 || tbposX <= 30) {
    speedtbX = -speedtbX;
  }
  if (tbposY > height - 30 || tbposY <= 30) {
    speedtbY = -speedtbY;
  }
    
  speedtbX = speedtbX * 0.995;
  speedtbY = speedtbY * 0.995;
   
  
  fill (0);
  ellipse(tbposX, tbposY,100,100);
  


}


class Ball {

  // some variables that define
  // the x and y position
  // the velocity x and y
  // the ball height/width
  
  float positionX;
  float positionY;
  
  float velocityX;
  float velocityY;
  
  int ballSize;
  
  color ballColor;
  

  Ball(float iposX, float iposY, float ivelX, float ivelY, int iballSize) {
    // assign incoming values to internal class variables
    
    positionX = iposX;
    positionY = iposY;
    
    velocityX = ivelX;
    velocityY = ivelY;
    
    ballSize = iballSize;
    
    ballColor = #ffffff;
    
  }

  Ball(float iposX, float iposY, float ivelX, float ivelY, int iballSize, color iballColor) {
    // assign incoming values to internal class variables
    
    positionX = iposX;
    positionY = iposY;
    
    velocityX = ivelX;
    velocityY = ivelY;
    
    ballSize = iballSize;
    
    ballColor = iballColor;
    
  }


  void display() {
    // draws a ball to the screen
    fill(ballColor);
    ellipse(positionX, positionY, ballSize, ballSize);
  }

  void update() {
    // moves the ball around, 
    // and keeps it on the screen
    if (positionX > width - ballSize/2) {
      velocityX *= -1;
      positionX = width - ballSize/2; // kicking it back into play
    }
    if (positionX < 0 + ballSize/2) {
      velocityX *= -1;
      positionX = 0 + ballSize/2; // kicking it back into play
    }
    if (positionY > height - ballSize/2) {
      velocityY *= -1;
      positionY = height - ballSize/2;
    }    
    if (positionY < 0 + ballSize/2) {
      velocityY *= -1;
      positionY = 0 + ballSize/2;
    }    


    positionX += velocityX;
    positionY += velocityY;
  }
}

Answers

  • Please, highlight your posted code and hit CTRL+K to format it! :-w
    Anyways, here's a fast circle colliding detection method: *-:)

    boolean hasCollided(Ball b) {
      return sq(b.x - x) + sq(b.y - y) < sq(b.rad + rad);
    }
    
  • edited February 2014

    Here is an example of how to tell when two circles have collided using the mouse. The dist() function gives the distance between the balls as if they were points (no radius) so you have to include the radius yourself:

    PVector b;
    
    void setup() {
      size(400, 400);
      b = new PVector(width/2, height/2);
    }
    
    void draw() {
      background(255);
    
      fill(255);
      ellipse(mouseX, mouseY, 50, 50);
    
      // First find the distance (a length) between the centers of the two balls
      // The distance must be less than the combined radii of the two balls
      // Radius A = 25, radius B = 40, distance must be less than 25+40
      if (dist(mouseX, mouseY, b.x, b.y) < 25+40) fill(255, 0, 0);
      else fill(0);
    
      ellipse(b.x, b.y, 80, 80);
    }
    
  • Please, read the sticky: To newcomers in this forum: read attentively these instructions

    And remember it is a good idea to explore the examples shipped with Processing. There are several ball collision sketches there.

  • Yea, newcomers should look at the examples a lot!

Sign In or Register to comment.