xxxxxxxxxx
53
balls = [];
posSun = 0;
function setup() {
createCanvas(600, 600);
posSun = createVector(300, 300);
}
function draw() {
background(0, 10);
push();
noStroke();
circle(width / 2, height / 2, 50);
pop();
for (let i = 0; i < balls.length; i++) {
let b = balls[i];
let force = p5.Vector.sub(posSun, b.p);
let intensity = 0.0003;
b.acc = force.mult(intensity);
b.hp -= 0.2;
b.update();
if (b.hp < 0) {
balls.splice(i, 1);
}
}
}
class Ball {
constructor(x, y, na, nb) {
this.p = createVector(x, y);
this.vel = createVector(na, nb);
this.acc = createVector(0, 0);
this.hp = 255;
}
update() {
this.vel.add(this.acc);
this.p.add(this.vel);
push();
noStroke();
fill(240, 237, 207, this.hp);
circle(this.p.x, this.p.y, 20);
pop();
}
}
function mousePressed() {
let angle = random();
let va = sin(angle);
let vb = sin(angle);
let intensity = random(1.5, 4);
balls.push(new Ball(mouseX, mouseY, va * intensity, vb * intensity));
}