let shark = ['S', 'H', 'A', 'R', 'K'];
p5.disableFriendlyErrors = true;
birds = [loadImage('b1.png'),
canvas = createCanvas(windowWidth, windowHeight);
canvas.style("z-index", "-1");
json = getItem('boids_params');
if(json === null) json = {};
for (var i = 0; i < population; i++) {
rows = floor(height/scl);
flowField = new Array(rows*cols);
boid_p_slider = createSlider(0, 500, json.boid_p, 10);
boid_s_slider = createSlider(0, 5, json.boid_s, 0.5);
boid_a_slider = createSlider(0, 5, json.boid_a, 0.5);
boid_c_slider = createSlider(0, 5, json.boid_c, 0.5);
boid_f_slider = createSlider(0, 5, json.boid_f, 0.5);
boid_ds_slider = createSlider(0, 100, json.boid_ds, 5);
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
function updatePerception() {
for (let boid of flock) {
boid.perception = boid_p_slider.value();
boid.separationForce = boid_s_slider.value();
boid.alignmentForce = boid_a_slider.value();
boid.cohesionForce = boid_c_slider.value();
boid.flightForce = boid_f_slider.value();
boid.desiredSeparation = boid_ds_slider.value();
json.boid_a = boid_a_slider.value();
json.boid_c = boid_c_slider.value();
json.boid_s = boid_s_slider.value();
json.boid_p = boid_p_slider.value();
json.boid_f = boid_f_slider.value();
json.boid_ds = boid_ds_slider.value();
storeItem("boids_params", json);
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("boids settings", 0, 60);
text("perception radius: " + boid_p_slider.value(), 0, 75);
text("alignment force: " + boid_a_slider.value(), 0, 90);
text("cohesion force: " + boid_c_slider.value(), 0, 105);
text("separation force: " + boid_s_slider.value(), 0, 120);
text("flight force: " + boid_f_slider.value(), 0, 135);
text("desired separation: " + boid_ds_slider.value(), 0, 150);
boid_p_slider.position(120, 60);
boid_p_slider.style("width", "80px");
boid_p_slider.input(updatePerception);
boid_a_slider.position(120, 75);
boid_a_slider.style("width", "80px");
boid_a_slider.input(updatePerception);
boid_c_slider.position(120, 90);
boid_c_slider.style("width", "80px");
boid_c_slider.input(updatePerception);
boid_s_slider.position(120, 105);
boid_s_slider.style("width", "80px");
boid_s_slider.input(updatePerception);
boid_f_slider.position(120, 120);
boid_f_slider.style("width", "80px");
boid_f_slider.input(updatePerception);
boid_ds_slider.position(120, 135);
boid_ds_slider.style("width", "80px");
boid_ds_slider.input(updatePerception);
console.log("key: " + key);
console.log("state: " + state);
let fr = floor(frameRate());
if (frameCount % 60 == 0) {
qtree = QuadTree.create();
ptree = QuadTree.create();
for(let y=0; y<rows; y++){
let index = (x + y * cols);
let r = noise(xoff, yoff, zoff);
let angle = r * TWO_PI * 0.5;
let v= p5.Vector.fromAngle(angle);
for (let boid of flock) {
let point = new Point(boid.position.x, boid.position.y, boid);
let point = new Point(pred.position.x, pred.position.y, pred);
for (let boid of flock) {
boid.flock(flock, flowField);
function mouseDragged() {
flock.push(new Boid(mouseX, mouseY));
if (flock.length > 200) flock.shift();
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();
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);
if (d < this.perception) {
steering.add(other.position);
steering.sub(this.position);
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
let q = ptree.query(range);
let steering = createVector();
let preds = this.findpredators();
for (let pred of preds) {
if (pred === this) continue;
let diff = p5.Vector.sub(this.position, pred.position);
dist(this.position.x, this.position.y, pred.position.x, pred.position.y)
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
this.acceleration.set(0, 0);
let c = this.chase().mult(1.0);
let a = this.avoid().mult(2.0);
this.acceleration.add(c);
this.acceleration.add(a);
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.desiredSeparation = 35.0;
this.separationForce = 1.5;
this.acceleration.set(0, 0);
let n = this.findNeighbors(boids);
let alignment = this.align(n).mult(this.alignmentForce);
let cohesion = this.cohesion(n).mult(this.cohesionForce);
let separation = this.separation(n).mult(this.separationForce);
let flightResponse = this.flight().mult(this.flightForce);
let follow = this.follow(field).mult(3);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
this.acceleration.add(separation);
this.acceleration.add(flightResponse);
this.acceleration.add(follow);
let steering = createVector();
var x = floor(this.position.x / scl);
var y = floor(this.position.y / scl)
var force = vectors[index];
steering.setMag(this.maxSpeed);
steering.add(this.velocity);
steering.limit(this.maxForce);
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 range = new Circle(this.position.x, this.position.y, this.perception);
let q = ptree.query(range);
let steering = createVector();
let preds = this.findpredators();
for (let pred of preds) {
let diff = p5.Vector.sub(this.position, pred.position);
let distance = dist(this.position.x, this.position.y, pred.position.x, pred.position.y);
if(distance<10){ let i = flock.indexOf(this); console.log(i); flock.splice(i,1);}
steering.setMag(this.maxSpeed);
steering.sub(this.velocity);
steering.limit(this.maxForce);
let steering = createVector();
for (let other of boids) {
var v = p5.Vector.sub(other.position, this.position);
if (degrees(v.heading()) < 15 && degrees(v.heading()) > -15) {
steering.add(p5.Vector.sub(this.position, other.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();
for (let other of boids) {
if (d > 0 && d < this.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);