createCanvas(windowWidth, windowHeight);
for (var i = 0; i < 100; i++) {
qtree = QuadTree.create();
for (let boid of flock) {
let point = new Point(boid.position.x, boid.position.y, boid);
for (let boid of flock) {
constructor(x = random(width), y = random(height)) {
this.position = createVector(x, y);
this.velocity = createVector(random(-1, 1), random(-1, 1));
this.velocity.setMag(random(2, 4));
this.acceleration = createVector();
this.acceleration.set(0, 0);
let n = this.findNeighbors(boids);
let separation = this.separation(n).mult(5);
let flightResponse = this.flight().mult(1);
this.acceleration.add(separation);
this.acceleration.add(flightResponse);
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;
let range = new Circle(this.position.x, this.position.y, this.perception);
let q = qtree.query(range)
if (other.userData != this) {
let steering = createVector();
let mouse = createVector(mouseX, mouseY);
let point = createVector( windowWidth * 2/3, windowHeight/2);
let d1 = dist(this.position.x, this.position.y, mouse.x, mouse.y) + 0.1;
let d2 = dist(this.position.x, this.position.y, point.x, point.y) + 0.1;
let diff1 = p5.Vector.sub(mouse ,this.position); diff1.div(d1);
let diff2 = p5.Vector.sub(point ,this.position); diff2.div(d2);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
let steering = createVector();
for (let other of boids) {
steering.add(other.velocity);
steering.div(boids.length);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
let steering = createVector();
for (let other of boids) {
steering.add(other.position);
steering.div(boids.length);
steering.sub(this.position);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
let steering = createVector();
var desiredseparation = 25.0;
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);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
translate(this.position.x, this.position.y);
rotate(this.velocity.heading());
triangle(0, -3, 0, 3, 5, 0);