xxxxxxxxxx
82
var bubbles = [];
function setup() {
createCanvas(400, 400);
for (var i=0; i<10; i++) {
bubbles[i] = new Bubble(random(20,380),random(20,380));
}
}
function draw() {
background(75,150,220);
for (var i=0; i<bubbles.length; i++) {
bubbles[i].display();
bubbles[i].update();
for (var j=0; j<bubbles.length; j++){
if (i != j && bubbles[i].intersects(bubbles[j])){
// bubbles[i].changeColor();
// bubbles[j].changeColor();
// bubbles[i].baby();
bubbles[i].energy();
bubbles[j].energy();
}
// else {
// bubbles[i].colorReset();
// bubbles[j].colorReset();
// }
}
}
}
function mousePressed() {
let b = new Bubble(mouseX, mouseY);
bubbles.push(b);
}
function Bubble (x,y) {
this.x = x;
this.y = y;
this.r = 20;
this.col = color(255);
this.display = function() {
noStroke();
// stroke(this.col);
// strokeWeight(2);
fill (this.col);
ellipse (this.x,this.y,this.r*2,this.r*2);
}
this.update = function() {
this.x = this.x + random(-2,2);
this.y = this.y + random(-2,2);
}
this.energy = function() {
this.x = this.x + random(-5,5);
this.y = this.y + random(-5,5);
}
this.intersects = function(other){
var d = dist(this.x,this.y,other.x,other.y);
if (d<=this.r+other.r+2) {
return true;
} else {
return false;
}
}
this.changeColor = function() {
this.col = color(255,0,0);
}
// this.colorReset = function() {
// this.col = color(255);
// }
// this.baby = function() {
// bubbles.push(new Bubble(200,200));
// }
}