xxxxxxxxxx
let a = [], amount = 20;
function setup() {
createCanvas(500, 500);
for(let i=0; i<amount; i++)
a.push(new createCircle(random(0, 500), random(0, 500)));
}
function draw() {
background(100);
for(let i=0; i<amount; i++) {
a[i].move();
a[i].collision(a, i);
}
}
function createCircle(x, y) {
this.r = random(10, 30);
this.x = x;
this.y = y;
var Vx = random(-1, 1);
var Vy = random(-1, 1);
let st = true;
this.color = [random(0,255), random(0,255), random(0,255), random(50,100)];
this.draw = function(sw=1) {
strokeWeight(sw);
if(st) noStroke();
else stroke(0);
fill(this.color);
circle(this.x, this.y, 2*this.r);
}
this.move = function(vx = Vx, vy = Vy) {
this.x += vx;
this.y += vy;
this.draw();
if(this.x > width + this.r) this.x -= width + 2*this.r;
else if(this.x < 0 - this.r) this.x += width + 2*this.r;
if(this.y > height + this.r) this.y -= height + 2*this.r;
else if(this.y < 0 - this.r) this.y += height + 2*this.r;
}
this.collision = function(circle, now) {
for(let i=0; i<amount; i++) {
let speed = random(0.1);
if(dis(circle[i], this) < circle[i].r + this.r && i != now) {
if(this.r > random(1, 10)) {
this.r -= 1;
Vx += speed;Vy += speed;
}
st = false;
this.color = [random(0,255), random(0,255), random(0,255), random(50,100)];
break;
}
else {
st = true;
if(this.r < random(10, 50)) {
this.r += 1;
Vx -= speed;Vy -= speed;
}
}
}
this.draw(random(3));
}
}
function dis(a, b) {
return pow(pow((a.x - b.x), 2) + pow((a.y - b.y), 2), 0.5);
}