createCanvas(windowWidth, windowHeight);
for (var i = 0; i < 100; i++) {
boid_p_slider = createSlider(0, 500, 100, 10);
function updatePerception() {
for (let boid of flock) {
boid.perception = boid_p_slider.value();
function displayDebug() {
text('Press any key to exit debug', 0, 10);
text('avg. frame rate: ' + af, 0, 23);
text('boids: ' + flock.length, 0, 36);
text('boid perception: ' + boid_p_slider.value(), 0, 49);
boid_p_slider.position(120, 34);
boid_p_slider.style('width', '80px');
boid_p_slider.input(updatePerception);
let fr = floor(frameRate());
if (frameCount % 60 == 0) {
qtree = QuadTree.create();
for (let boid of flock) {
let point = new Point(boid.position.x, boid.position.y, boid);
for (let boid of flock) {
function mouseDragged() {
flock.push(new Boid(mouseX, mouseY))
if (flock.length > 200) flock.shift();
this.position = createVector(random(width), random(height));
this.velocity = createVector(random(-1, 1), random(-1, 1));
this.velocity.setMag(random(2, 4));
this.acceleration = createVector();
translate(this.position.x, this.position.y);
rotate(this.velocity.heading());
triangle(0, -6, 0, 6, 8, 0);
let steering = createVector();
let range = new Circle(this.position.x, this.position.y, this.perception);
let n = qtree.query(range)
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (d < this.perception) {
steering.add(other.position);
steering.sub(this.position);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
this.acceleration.set(0, 0);
let cohesion = this.chase().mult(1.0);
this.acceleration.add(cohesion);
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
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;
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 alignment = this.align(n).mult(1.0);
let cohesion = this.cohesion(n).mult(1.0);
let separation = this.separation(n).mult(1.5);
let flightResponse = this.flight().mult(5);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
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 d = dist(this.position.x, this.position.y, p.position.x, p.position.y);
if (d <= this.perception) {
let diff = p5.Vector.sub(this.position, p.position);
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);