xxxxxxxxxx
197
let total = 5;
let totalParticles = [];
let d = 10;
let drag = 0.98;
let overPop = 20;
let underPop = 2;
function setup() {
createCanvas(400, 400);
for(let i = 0; i < total; i++){
let particles = [];
let c = color(random(255),random(255),random(255));
for(let i = 0; i <= 10; i++){
particles.push(new Particle(random(width),random(height),i,c));
}
totalParticles.push(particles);
}
}
function draw() {
background(220);
for(let team of totalParticles){
if(team.length > 0){
for(let j = team.length-1; j >= 0; j--){
let t = team[j];
t.look(totalParticles);
t.update();
t.boundaries();
let count = 0;
for(let other of totalParticles){
if(other != team){
t.seek(other,t.dna.seekRange[count],t.dna.seekForce[count]);
t.seperate(other,t.dna.sepRange[count],t.dna.sepForce[count]);
count++;
}
}
t.rules();
t.seperate(team,t.size,20);
t.display();
if(!t.alive){
team.splice(j);
}
}
}
}
}
class DNA {
constructor(){
this.seekRange = [];
this.seekForce = [];
this.sepRange = [];
this.sepForce = [];
for(let i = 0; i < total - 1; i++){
this.seekRange.push(random(25,50));
this.seekForce.push(random(0.1,0.5));
this.sepRange.push(random(5,10));
this.sepForce.push(random(0.5,1.5));
}
}
}
class Particle {
constructor(x, y, t, c) {
this.acceleration = createVector(0, 0);
this.velocity = createVector(0,0);
this.position = createVector(x, y);
this.r = 5;
this.team = t;
this.color = c;
this.close = [];
this.alive = true;
this.maxspeed = 1.5;
this.maxforce = 0.7;
this.dna = new DNA();
}
look(particles){
for(let team of particles){
for(let t of team){
if(t != this){
let d = this.position.dist(t.position);
if(d < this.r){
this.close.push(t);
}
}
}
}
}
rules(){
if(this.close.length > overPop){
this.alive = false;
}
// }else if(this.close < underPop){
// this.alive = false;
// }
}
boundaries() {
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;
}
// Method to update location
update() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset accelerationelertion to 0 each cycle
this.acceleration.mult(0);
this.velocity.mult(drag);
}
applyForce(force) {
// We could add mass here if we want A = F / M
this.acceleration.add(force);
}
// A method that calculates a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
seek(targets,range,mult) {
let closest;
let record = range;
for(let t of targets){
let d = this.position.dist(t.position);
if(d <= record){
record = d;
closest = t;
}
}
if(closest){
var desired = p5.Vector.sub(closest.position, this.position); // A vector pointing from the location to the target
// Scale to maximum speed
desired.setMag(this.maxspeed);
// Steering = Desired minus velocity
var steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce * mult); // Limit to maximum steering force
this.applyForce(steer);
}
}
seperate(targets,range,mult) {
let closest;
let record = range;
for(let t of targets){
let d = this.position.dist(t.position);
if(d <= record){
record = d;
closest = t;
}
}
if(closest){
var desired = p5.Vector.sub(this.position, closest.position); // A vector pointing from the location to the target
// Scale to maximum speed
desired.setMag(this.maxspeed);
// Steering = Desired minus velocity
var steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce * mult); // Limit to maximum steering force
this.applyForce(steer);
}
}
display() {
stroke(0);
fill(this.color);
circle(this.position.x,this.position.y,this.r);
}
}