xxxxxxxxxx
99
let bees = [];
let attractor;
function setup() {
createCanvas(800, 600);
attractor = new Attractor(width / 2, height / 2); //have attractor in the middle of the screen
for (let i = 0; i < 500; i++) {
bees.push(new Bee(random(width), random(height)));
}
}
function draw() {
background(123);
for (let bee of bees) {
let attraction = attractor.attract(bee);
bee.applyForce(attraction);
bee.update();
bee.constrainToEdges();
bee.display();
}
attractor.walk();
attractor.display();
}
class Bee {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.acc = createVector();
this.maxSpeed = 100; //speed of the particles
}
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
constrainToEdges() {
this.pos.x = constrain(this.pos.x, 0, width); //have it conbeeines around the edges
this.pos.y = constrain(this.pos.y, 0, height);
}
display() {
let distance = p5.Vector.dist(attractor.pos, this.pos);
let alphaValue = map(distance, 100, 500, 255, 0); // Map distance to alpha (transparency)
let choice = random();
if (choice > 0 && choice < 1/3) {
fill(255, 215, 0, alphaValue);
} else if(choice > 1/3 && choice < 2/3){
fill(255, alphaValue);
}
else{
fill(0,alphaValue)}
noStroke();
ellipse(this.pos.x, this.pos.y, 5, 5);
}
}
class Attractor {
constructor(x, y) {
this.pos = createVector(x, y);
this.mass = 50;
}
attract(bee) {
let force = p5.Vector.sub(this.pos, bee.pos);
let distance = force.mag();
distance = constrain(distance, 100, 500);
force.normalize();
let strength = (1 * this.mass * bee.mass) / (distance * distance);
force.mult(strength);
return force;
}
walk() {
this.pos.add(p5.Vector.random2D().mult(5));
}
display() {
// noFill();
noStroke();
fill(237, 208, 92);
ellipse(this.pos.x, this.pos.y, 20, 20);
}
}