createCanvas(windowWidth, windowHeight);
flow = new Flowfield(15);
for(var i =0; i < agent_pop; i++){
agents.push(new Agent());
background(240, 230, 200, 15);
for(var agent of agents){
constructor(x = random(width), y = random(height)){
this.position = createVector(x, y);
this.velocity = createVector(0,0);
this.acceleration = createVector(0,0);
randomizeColor(r = 255, g = 255, b = 255){
this.r = constrain(random(this.r - 5, this.r + 5), 0, 255);
this.g = constrain(random(this.g - 5, this.g + 5), 0, 255);
this.b = constrain(random(this.b - 5, this.b + 5), 0, 255);
fill(this.r, this.g, this.b, 100);
translate(this.position.x, this.position.y);
rotate(random(0, TWO_PI));
if(this.position.x < 0 || this.position.x > width || this.position.y < 0 || this.position.y > height ){
this.position = createVector(random(width), random(height));
this.acceleration.add(force);
var desired = flow.lookup(this.position);
desired.mult(this.maxSpeed);
var steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxForce);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
this.acceleration.set(0, 0);
constructor(resolution = 10){
this.resolution = resolution;
this.rows = height/this.resolution;
this.cols = width/this.resolution;
for(var i = 0; i < this.cols; i++){
for(var j = 0; j < this.rows; j++){
var theta = map(noise(xoff,yoff,this.zoff),0,1 ,0,TWO_PI);
this.field[i][j] = createVector(cos(theta), sin(theta));
var col = Math.floor(constrain(v1.x/this.resolution, 0, this.cols-1));
var row = Math.floor(constrain(v1.y/this.resolution, 0, this.rows-1));
return this.field[col][row].copy();
for(var i = 0; i < this.cols; i++){
for(var j = 0; j < this.rows; j++){
var x = i*this.resolution + this.resolution/2;
var y = j*this.resolution + this.resolution/2;
rotate(this.field[i][j].heading());