xxxxxxxxxx
127
const flock = []; //flock array
let speedSlider;
let forceSlider;
let visionSlider;
function setup() {
createCanvas(640, 640);
label1 = createDiv('Speed');
label1.position(10, 2);
speedSlider = createSlider(0, 50, 2);
speedSlider.position(10, 10);
speedSlider.style('width', '80px');
speedSlider.parent(label1);
label2 = createDiv('Force');
label2.position(10, 40);
forceSlider = createSlider(0, 10, 0.2, 1);
forceSlider.position(10, 50);
forceSlider.style('width', '80px');
label3 = createDiv('Field of Vision');
label3.position(10, 80);
visionSlider = createSlider(0, 1000, 50);
visionSlider.position(10, 90);
visionSlider.style('width', '80px');
for (let i = 0; i < 100; i++) { //generate 100 humanoids
flock.push(new humanoid());
}
}
function draw() {
background(220);
for(let humanoid of flock){
humanoid.edges();
humanoid.flock(flock);
humanoid.update();
humanoid.show();
}
}
class humanoid{ //create humanoid class
constructor() {
this.position = createVector(random(width), random(height));
this.velocity = p5.Vector.random2D(); //acquire random velocity vec
this.acceleration = createVector();
this.maxForce = 0.2;
this.maxSpeed = 5;
}
align(humanoids){ //aligning direction to match other humanoids
let fieldofView = 50; //only want velocity of humanoid w/in a dist
let desiredVel = createVector();
let total = 0;
for (let other of humanoids){
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (other != this && d < fieldofView){ //dist relative to others
desiredVel.add(other.velocity); //get avg velocity
total++;
}
}
if (total > 0){ //if there is total, set parameters to other hums
desiredVel.div(total);
desiredVel.setMag(this.maxSpeed);
desiredVel.sub(this.velocity);
desiredVel.limit(this.maxForce);
}
return desiredVel;
}
edges(){ //allow humanoids to reappear within canvas
if (this.position.x > width){
this.position.x = 0;
} else if (this.position.x < 0){
this.position.x = width;
}
if (this.position.y > height){
this.position.y = 0;
} else if (this.position.y < 0){
this.position.y = height;
}
}
flock(humanoids){
this.acceleration.set(0,0);
let alignment = this.align(humanoids);
let cohesion = this.cohesion(humanoids);
this.acceleration.add(alignment);
this.acceleration.add(cohesion);
}
update(){ //make humanoid move
this.position.add(this.velocity);
this.velocity.add(this.acceleration);
this.maxSpeed = speedSlider.value();
this.velocity.limit(this.maxSpeed);
}
show(){ //draw the ellipse of humanoid
noStroke();
fill(10);
ellipse(this.position.x,this.position.y,9,9);
}
cohesion(humanoids){ //aligning direction to match other humanoids
let fieldofView = visionSlider.value();
let desiredVel = createVector();
let total = 0;
for (let other of humanoids){
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (other != this && d < fieldofView){
desiredVel.add(other.position); //instead of vel, check oth pos
total++;
}
}
if (total > 0){
desiredVel.div(total);
desiredVel.sub(this.position); //subtract location of humanoid with avg location of other humanoids
desiredVel.setMag(this.maxSpeed);
desiredVel.sub(this.velocity);
this.maxForce = forceSlider.value();
desiredVel.limit(this.maxForce);
}
return desiredVel;
}
}