I tried it. But it’s not working. I think because of the condition I’ve written for a bubble to pop, each bubble is getting popped by itself. Haven’t been able to rectify it though.
Bubble [] bubbles = new Bubble [100] ;
void setup () {
size (640, 480);
for (int indices = 0; indices < bubbles.length; indices += 1) {
bubbles [indices] = new Bubble (random (width), height, random (80));
}
}
void draw () {
background (0);
for (int indices = 0; indices < bubbles.length; indices += 1) {
bubbles[indices].ascend ();
if (bubbles [indices].together (bubbles [indices])) {
bubbles[indices].pops ();
}
bubbles[indices].display ();
bubbles[indices].top ();
}
}
class Bubble {
float x, y;
float d;
float driftX;
float speedY;
float alpha;
Bubble (float tempX, float tempY, float tempD) {
x = tempX;
y = tempY;
d = tempD;
driftX = 2;
speedY = random (0.25, 2);
alpha = random (20, 107);
}
void display () {
fill (255, 223 + (random (-20, 0)), 0, alpha);
noStroke ();
ellipse (x, y, d, d);
}
void ascend () {
x += random (driftX*-1, driftX);
y -= speedY;
}
void top () {
if (y+d < 0) {
y = height;
}
}
boolean together (Bubble other) {
float distance = dist((x + d/2), (y+d/2), (other.x+((other.d)/2)), (other.y+((other.d)/2)));
if (distance == 0) {
return true;
} else {
return false;
}
}
void pops () {
d = 0;
}
}