Help Please! Collision detection of circles.
Hi. I attemped to implement the collision of circles but it did not worl.
I aim to make the white circles remove(set the out-side of the screen) when the circles collided with the big circle.
Here is what Ive got so far with lots of helps.
// Global (i.e. PApplet-wide) constants and variables
final color BACKGROUND = color (0, 0, 0) ; // the background colour(BLACK)
Bin bin ;
int numCircle = 3 ; // the number of circles
Circle[] circles = new Circle[numCircle] ; // declare and create the array
// -------------------------------------------------
void setup() {
size(960, 600) ; // set the mianpanel size as 960 * 600)
background(BACKGROUND) ; // set the background colour
smooth() ;
noStroke() ;
bin = new Bin(100, 300, 100) ;
for (int i = 0; i < circles.length; i++) {
// set the position of the circles (in this case, random)
float x = random(100, 700) ;
float y = random(300, 600) ;
circles[i] = new Circle(x, y, 50) ; // create each object
}
}
// ------------------------------------------------------------------------------ Main methods
// Display the elements
void draw() {
background(BACKGROUND) ;
bin.paint() ; // draw the circle(bin)
for (int i = 0; i < circles.length; i++) {
circles[i].paint() ; // draw the objects consist in the Circle paint() function
}
}
// -------------------------------------------------
void mousePressed() {
for (int i = 0; i < numCircle; i++) {
circles[i].dragPressed() ; // when the mouse is pressed, do the drawPressed() function
}
}
void mouseReleased() {
for (int i = 0; i < numCircle; i++) {
circles[i].dragReleased() ; // when the mouse is pressed, do the drawPressed() function
}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Bin {
// ----------------------------------------------------------------- Constants and variables
private float bx, by ; // the x and y-coordinate of the circle(object)
private int radius ; // the radius of the circle
// ---------------------------------------------------------------------------- Constructors
Bin(float bxPos, float byPos, int radi) {
this.bx = bxPos ;
this.by = byPos ;
this.radius = radi ;
}
// --------------------------------------------------------------------------- Public methods
public void paint() {
// Draw the circle
fill(255, 0, 0) ;
ellipse(bx, by, radius, radius) ;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Circle {
// ----------------------------------------------------------------- Constants and variables
private float x, y ; // the x and y-coordinate of the circle(object)
private float dx, dy ; // the difference between the circle's position and the mouse position
private int radius ; // the radius of the circle
private boolean startDrag ; // prepare for drag signal
Bin bin ;
// ---------------------------------------------------------------------------- Constructors
Circle(float xPos, float yPos, int radi) {
this.x = xPos ;
this.y = yPos ;
this.radius = radi ;
}
// --------------------------------------------------------------------------- Public methods
public void paint() {
// when the mouse drag starts, starDrag turns "true" and implement the function
if (startDrag == true) {
x = mouseX + dx;
y = mouseY + dy;
}
// Draw the circles
fill(255) ;
for (int i = 0; i < numCircle; i++) {
ellipse(x, y, radius, radius) ;
}
}
// ------------------------------------------------------- Getters and setters
// ------------------------------------------------------- Setters
// ------------------------------------------------------ Other public methods
public void collisionDetection() {
// if the centre of the cicle and the bin(circle) position is less than the radius,
if (dist(x, y, bin.bx, bin. by) <= radius) {
// remove the circles which collieded with the bin(circle)
for (int i = 0; i < numCircle; i++) {
ellipse(-100, -100, radius, radius) ; // outside of the screen
}
}
}
public void dragPressed() {
// if the centre of the cicle and the mouse position is less than the radius of the circle,
if (dist(x, y, mouseX, mouseY) <= radius) {
startDrag = true ; // the drag turns "true"
// calculate the distance between the circle's position and that of mouse
dx = x - mouseX;
dy = y - mouseY;
}
}
public void dragReleased() {
startDrag = false ; // the darg turns "false"
}
// -------------------------------------------------------------------------------------------
}
// -------------------------------------------------------------------------------------------