collision detection/chain reaction
in
Programming Questions
•
11 months ago
I'm not totally new to processing, but I am a bit stuck on a program/game. The goal is to create a one-time event upon mouseClicked (here an "exploding" circle that expands and then loses opacity). There are forty circle objects bouncing randomly around the screen. When the circle(s) collide with the explosion and only the explosion while it is occurring, not other unexploded or already exploded circles, they become explosions themselves, thus starting a chain reaction.
So far I have implemented the mouseclicked explosion but am having trouble with the collision detection. I have added a boolean intersects function but am not sure where to call it (still fuzzy on calling elements in classes and getters/setters, but working on it). Any ideas or suggestions would be very helpful, I think I just need a push in the right direction. Thank you!
//tab 1
MovingCircles[] myCirclesArray = new MovingCircles[41];
int myLength = 40;
void setup() {
size(700, 400);
smooth();
frameRate = 100;
for(int i=0; i<myLength; i++) {
myCirclesArray[i] = new MovingCircles(300, 200, 20);
}
}
void draw() {
background(255);
for(int i=0; i<myLength; i++) {
myCirclesArray[i].drawCircles();
myCirclesArray[i].update();
myCirclesArray[i].checkCollisions();
}
}
void mouseClicked(){
myCirclesArray[myLength] = new MovingCircles(mouseX, mouseY, 0);
myCirclesArray[myLength].setExplode(true);
myCirclesArray[myLength].setSpeed(0, 0);
myLength = myLength + 1;
}
//circles and explosion class
class MovingCircles{
float x; //y position of circles
float y; //xposition of circles
float xSpeed; //x speed of circles
float ySpeed; //y speed of circles
float w; //width of ellipse exploding
float h; //height of ellipse exploding
float velo; //speed of expansion
float cSize;
int myRed;
int myGreen;
int myBlue;
int myOpacity = 100;
boolean exploding = false;
MovingCircles(float xpos, float ypos, float csize){
x = xpos;
y = ypos;
cSize = csize;
myRed= (int) random(255);
myGreen= (int) random(255);
myBlue= (int) random(255);
xSpeed = random(-3, 3);
ySpeed = random(-3, 3);
}
void setExplode(boolean isExploding) { exploding = isExploding;}
void setX(float newX) { x = newX;}
void setY(float newY) { y = newY;}
void setcSize(float csize) { cSize = csize;}
boolean getExplode() { return exploding; }
float getX() { return x; }
float getY() { return y; }
float getcSize() { return cSize; }
void drawCircles(){
fill(myRed, myGreen, myBlue);
ellipse(x, y, cSize, cSize);
}
void update() {
x += xSpeed;
y += ySpeed;
if(exploding){
velo = 1;
fill(myRed, myGreen, myBlue, myOpacity);
ellipse(x, y, w, h);
w = w + velo;
h = h + velo;
if((w >= 100) && (h >= 100)){
myOpacity = myOpacity - 1;
w = w - velo;
h = h - velo;
}
}
}
void checkCollisions() {
float r = cSize/2;
if ( (x<r) || (x>width-r)){
xSpeed = -xSpeed;
}
if( (y<r) || (y>height-r)) {
ySpeed = -ySpeed;
}
// if(intersects){
// exploding();
// }
}
void setSpeed( int anXSpeed, int anYSpeed){
xSpeed = anXSpeed;
ySpeed = anYSpeed;
}
void drawExplosion(){
velo = 1;
fill(myRed, myGreen, myBlue, myOpacity);
ellipse(mouseX, mouseY, w, h);
w = w + velo;
h = h + velo;
if((w >= 100) && (h >= 100)){
myOpacity = myOpacity - 1;
w = w - velo;
h = h - velo;
}
}
//this is the boolean mentioned above.
boolean intersects(MovingCircles c)
{
if (dist(x, y, c.getX(), c.getY()) <
cSize/2 + c.getcSize()/2){
return true;
}
return false;
}
}
1