xxxxxxxxxx
103
// let bubble1;
// let bubble2;
let bubbles = [];
// let kingbub;
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 100; i++) {
let x = random(width);
let y = random(height);
let r = random(10, 50);
bubbles[i] = new Bubble(x, y, r);
}
// bubble1 = new Bubble(200, 200);
// bubble2 = new Bubble(400, 200, 100);
// kingbub = new Bubble(300, 200, 30);
}
function draw() {
background(0);
// if (bubble1.intersects(bubble2)) {
// background(200, 0, 100);
// }
// for (let i = 0; i < bubbles.length; i++) {
// bubbles[i].move();
// bubbles[i].show();
// }
// kingbub.x = mouseX;
// kingbub.y = mouseY;
// kingbub.show();
// kingbub.move();
for (let b of bubbles) {
b.move();
b.show();
let overlapping = false;
for (let other of bubbles) {
if (b !== other && b.intersects(other)) {
overlapping = true;
}
}
if (overlapping){
b.changeColor(255);
} else{
b.changeColor(0);
}
}
// bubble1.show();
// bubble2.show();
// bubble1.move();
// //bubble2.move();
// bubble2.x = mouseX;
// bubble2.y = mouseY;
}
class Bubble {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.brightness = 0;
}
intersects(other) {
let d = dist(this.x, this.y, other.x, other.y);
// return d < this.r / 2 + other.r / 2;
if (d < this.r / 2 + other.r / 2) {
return true;
} else {
return false;
}
}
changeColor(bright) {
this.brightness = bright;
}
contains(px, py) {
let d = dist(px, py, this.x, this.y);
if (d < this.r) {
return true;
} else {
return false;
}
}
move() {
this.x = this.x + random(-2, 2);
this.y = this.y + random(-2, 2);
}
show() {
stroke(255);
strokeWeight(4);
fill(this.brightness, 125);
ellipse(this.x, this.y, this.r);
}
}