xxxxxxxxxx
64
//https://discourse.processing.org/t/drawing-triangle-to-face-toward-its-velocity/16642
let b;
function setup() {
createCanvas(400, 400);
b = new Boid();
}
function draw() {
background(200,200,0);
translate(width/2,height/2);
b.draw();
}
class Boid {
constructor() {
this.loc = createVector();
this.vel = p5.Vector.random2D();
this.vel.setMag(1);//random(1, 2));
this.acc = createVector();
this.maxSpeed = 1;
}
/*
draw() {
this.update();
stroke(255);
strokeWeight(2)
push();
beginShape();
rotate(this.vel.heading());
vertex(this.loc.x, this.loc.y);
vertex(this.loc.x - 3, this.loc.y + 10);
vertex(this.loc.x, this.loc.y + 8);
vertex(this.loc.x + 3, this.loc.y + 10);
endShape(CLOSE);
pop();
}
*/
// @glv
draw() {
this.update();
stroke(255);
strokeWeight(2)
push();
translate(this.loc.x, this.loc.y);
rotate(this.vel.heading() + TAU / 4);
beginShape();
vertex(0, 0);
vertex(0 - 3, 0 + 10);
vertex(0, 0 + 8);
vertex(0 + 3, 0 + 10);
endShape(CLOSE);
pop();
}
update() {
this.loc.add(this.vel);
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed)
}
}