xxxxxxxxxx
215
let population = 200;
let allTeams = [];
let team1 = [];
let team2 = [];
let team3 = [];
function setup() {
createCanvas(800, 800);
for(let i = 0; i <= 50; i++){
team1.push(new Particle(random(width),random(height),1));
}
for(let i = 0; i <= 50; i++){
team2.push(new Particle(random(width),random(height),2));
}
for(let i = 0; i <= 50; i++){
team3.push(new Particle(random(width),random(height),3));
}
allTeams.push(team1);
allTeams.push(team2);
allTeams.push(team3);
}
function draw() {
background(220);
for(let team of allTeams){
for(let p of team){
p.run(allTeams);
}
}
}
//NEED TO DO: CHANGE SO THAT IT FINDS CLOSEST RATHER THAN AVERAGE
class Particle {
constructor(x, y, t) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(0, 0);
this.position = createVector(x, y);
this.r = 10;
this.maxspeed = 3;
this.maxforce = 0.05;
this.team = t;
//rules of the game
if(this.team == 1){
this.team1 = 2;
this.team2 = 3;
}else if(this.team == 2){
this.team1 = 1;
this.team2 = 3;
}else if(this.team ==3){
this.team1 = 1;
this.team2 = 2;
}
//own team
this.teamSeperate = this.r;
//team1
this.team1SeperateRange = 20;
this.team1SeperateForce = random(0.5,1.5);
this.team1AlignRange = 80;
this.team1AlignForce = random(0.5,1.5);
//team2
this.team2SeperateRange = 20;
this.team2SeperateForce = random(0.5,1.5);
this.team2AlignRange = 80;
this.team2AlignForce = random(0.5,1.5);
}
run(boids) {
this.flock(boids);
this.update();
this.borders();
this.render();
}
applyForce(force) {
this.acceleration.add(force);
}
flock(teams) {
for(let t of teams){
if(t[0].team == this.team){
let sep = this.separate(t,this.teamSeperate);
sep.mult(1.5);
this.applyForce(sep);
}else if(t[0].team == this.team1){
let sep = this.separate(t,this.team1SeperateRange);
let ali = this.align(t,this.team1AlignRange);
sep.mult(this.team1SeperateForce);
ali.mult(this.team1AlignForce);
this.applyForce(sep);
this.applyForce(ali);
}else if(t[0].team == this.team2){
let sep = this.separate(t,this.team2SeperateRange);
let ali = this.align(t,this.team2AlignRange);
sep.mult(this.team2SeperateForce);
ali.mult(this.team2AlignForce);
this.applyForce(sep);
this.applyForce(ali);
}
// let sep = this.separate(t);
// let ali = this.align(t);
// let coh = this.cohesion(t);
// sep.mult(1.5);
// ali.mult(1.0);
// coh.mult(1.0);
// this.applyForce(sep);
// this.applyForce(ali);
// this.applyForce(coh);
}
}
update() {
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
this.velocity.mult(0.99);
}
seek(target) {
let desired = p5.Vector.sub(target, this.position);
desired.normalize();
desired.mult(this.maxspeed);
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce);
return steer;
}
render() {
stroke(0);
if(this.team == 1){
fill(200,0,0);
}else if(this.team == 2){
fill(0,0,200);
}else if(this.team == 3){
fill(0,200,0);
}
circle(this.position.x,this.position.y,this.r);
}
borders() {
if (this.position.x < -this.r) this.position.x = width + this.r;
if (this.position.y < -this.r) this.position.y = height + this.r;
if (this.position.x > width + this.r) this.position.x = -this.r;
if (this.position.y > height + this.r) this.position.y = -this.r;
}
separate(boids,s) {
let desiredseparation = s;
let steer = createVector(0, 0);
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < desiredseparation)) {
let diff = p5.Vector.sub(this.position, boids[i].position);
diff.normalize();
diff.div(d);
steer.add(diff);
count++;
}
}
if (count > 0) {
steer.div(count);
}
if (steer.mag() > 0) {
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
align(boids) {
let closest;
let record = Infinity;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if (d < record) {
d = record;
closest = boids[i];
}
}
let steer = p5.Vector.sub(closest.position, this.velocity);
steer.limit(this.maxforce);
return steer;
}
}