p5.disableFriendlyErrors = true;
createCanvas(960, 540, WEBGL);
beans = Array(numOfBeans)
directionalLight(color("white"), 0, 0, -1);
directionalLight(color("gray"), 0,0, -1);
beans.forEach((r) => r.wrap());
beans.forEach((r) => r.update());
beans.forEach((r) => r.show());
function avoidOverlapping() {
for (let i = beans.length; i--; ) {
const delta = A.pos.copy().sub(B.pos);
const distance = sqrt(delta.x ** 2 + delta.y ** 2);
const sumRadius = A.radius + B.radius;
if (distance < sumRadius) {
const k = 1 - distance / sumRadius;
const force = delta.mult(k);
return random(2) < 1 ? type[0] : type[1];
this.pos = createVector(random(-1, 1), random(-1, 1));
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.radius = random(minRadius, maxRadius);
this.direction = random(2 * PI);
const { rotation, color } = type.select();
this.rotation = rotation;
translate(this.pos.x, this.pos.y, 0);
this.direction += this.rotation;
const unitVector = createVector(1, 0).rotate(this.direction);
this.vel.add(unitVector);
this.vel.add(this.acc.mult(0.5));
const r = this.radius + margin;
this.pos.x = constrain(this.pos.x, r - width / 2, width / 2 - r);
this.pos.y = constrain(this.pos.y, r - height / 2, height / 2 - r);