xxxxxxxxxx
181
class Boid {
constructor() {
this.position = createVector(random(width), random(height));
this.velocity = p5.Vector.random2D();
this.velocity.setMag(random(2, 4));
this.acceleration = createVector();
this.maxForce = 0.2;
this.maxSpeed = 5;
this.c = color(random(255), random(255), random(255), 100);
this.start = 0;
this.end = 0;
this.pos = 0;
}
edges() {
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;
}
}
setColor(boids) {
let closest;
let perceptionRadius = 50;
for (let current of boids) {
let d = dist(this.position.x, this.position.y, current.position.x, current.position.y);
if (d < perceptionRadius) {
closest = current;
let r = red(closest.c);
let g = green(closest.c);
let b = blue(closest.c);
let c = color(r, g, b, 100);
return c;
}
}
}
align(boids) {
let perceptionRadius = 75;
let steering = createVector();
let total = 0;
for (let other of boids) {
let d = dist(this.position.x,
this.position.y,
other.position.x,
other.position.y);
if (other != this && d < perceptionRadius && this.start < other.pos && other.pos < this.end) {
steering.add(other.velocity);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
separation(boids) {
let perceptionRadius = 25;
let steering = createVector();
let total = 0;
for (let other of boids) {
let d = dist(this.position.x,
this.position.y,
other.position.x,
other.position.y);
if (other != this && d < perceptionRadius && this.start < other.pos && other.pos < this.end) {
let diff = p5.Vector.sub(this.position, other.position);
if(d > 0) {
diff.div(d * d);
steering.add(diff);
total++;
}
}
}
if (total > 0) {
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
steering.mult(1.1);
}
return steering;
}
cohesion(boids) {
let perceptionRadius = 100;
let steering = createVector();
let total = 0;
for (let other of boids) {
let d = dist(this.position.x,
this.position.y,
other.position.x,
other.position.y);
if (other != this && d < perceptionRadius && this.start < other.pos && other.pos < this.end) {
steering.add(other.position);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.sub(this.position);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
}
return steering;
}
flock(boids) {
let alignment = this.align(boids);
let cohesion = this.cohesion(boids);
let separation = this.separation(boids);
let c = this.setColor(boids);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
this.acceleration.add(separation);
if (c) {
this.c = c;
}
}
update() {
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.acceleration.mult(0);
this.start = (this.velocity.heading() - coneView/2) +- HALF_PI/2;
this.end = (this.velocity.heading() + coneView/2) + HALF_PI/2;
this.pos = this.position.heading();
}
show() {
push();
translate(this.position.x, this.position.y);
strokeWeight(1);
stroke(this.c);
fill(this.c);
rotate(this.velocity.heading() + HALF_PI);
triangle(0, 0, -4, 15, 4, 15);
stroke(255);
strokeWeight(3);
point(0, 0);
pop();
}
check(o) {
if (dist(this.position.x, this.position.y, o.x, o.y) < o.r) {
let target = createVector(o.x, o.y);
let desire = p5.Vector.sub(target, this.position);
desire.setMag(this.maxSpeed);
let steer = p5.Vector.sub(desire, this.velocity);
steer.limit(this.maxSpeed);
steer.mult(-1);
this.velocity = steer;
}
}
}
function addBoids(total) {
for (let i = 0; i < total; i++) {
flock[i] = new Boid();
}
}