for (let i = 0; i < 50; i++) {
boids.push(new Boid(random(width), random(height)));
evilBoid = new EvilBoid(width / 2, height / 2);
for (let i = boids.length - 1; i >= 0; i--) {
if (evilBoid.eat(boids[i])) {
this.position = createVector(x, y);
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random(2, 4));
this.acceleration = createVector();
let separation = this.separate(boids).mult(1.5);
let alignment = this.align(boids).mult(1.0);
let cohesion = this.cohere(boids).mult(1.0);
this.acceleration.add(separation);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
let desiredSeparation = 25;
let steer = createVector();
for (let other of boids) {
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (d > 0 && d < desiredSeparation) {
let diff = p5.Vector.sub(this.position, other.position);
steer.setMag(this.maxSpeed);
steer.sub(this.velocity);
steer.limit(this.maxForce);
let sum = createVector();
for (let other of boids) {
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (d > 0 && d < neighborDist) {
sum.setMag(this.maxSpeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxForce);
let sum = createVector();
for (let other of boids) {
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (d > 0 && d < neighborDist) {
let desired = p5.Vector.sub(target, this.position);
desired.setMag(this.maxSpeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.acceleration.mult(0);
if (this.position.x > width) this.position.x = 0;
if (this.position.x < 0) this.position.x = width;
if (this.position.y > height) this.position.y = 0;
if (this.position.y < 0) this.position.y = height;
point(this.position.x, this.position.y);
this.position = createVector(x, y);
this.velocity = createVector();
this.acceleration = createVector();
if (boids.length === 0) return;
let closestDist = Infinity;
for (let boid of boids) {
let d = dist(this.position.x, this.position.y, boid.position.x, boid.position.y);
this.acceleration = this.seek(closest.position);
let desired = p5.Vector.sub(target, this.position);
desired.setMag(this.maxSpeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
let d = dist(this.position.x, this.position.y, boid.position.x, boid.position.y);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
point(this.position.x, this.position.y);