xxxxxxxxxx
38
class Boid
{
constructor(x, y) {
this.pos = createVector(x,y);
this.vel = createVector(1, 0);
this.acc = createVector(0, 0);
this.angle = 0;
}
display() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.angle);
triangle(0, -20, -15, 15, 15, 15);
pop();
}
applyForce(force) {
this.acc.add(force);
}
update(target) {
let desired = p5.Vector.sub(target, this.pos).setMag(4);
let steer = p5.Vector.sub(desired, this.vel).limit(0.1);
this.applyForce(steer);
this.angle = this.vel.heading() + 90;
this.vel.add(this.acc);
this.vel.x = constrain(this.vel.x, -5, 5);
this.vel.y = constrain(this.vel.y, -5, 5);
this.acc.mult(0);
this.pos.add(this.vel);
}
}