xxxxxxxxxx
var start = [];
var b;
function setup() {
createCanvas(windowHeight, windowHeight);
b = color(100,100,100,255);
background(b);
noStroke();
start.push(new Agent(width>>1, height>>1, 0));
}
function draw() {
for(var i=0; i < start.length; i++)
{
start[i].move();
start[i].draw();
}
}
class Agent{
constructor (x, y, angle){
this.x = x;
this.y = y;
this.angle = angle;
this.speed = random(1,5);
this.isRunning = true;
}
move(){
if(!this.isRunning) return;
this.changeAngle();
this.split();
this.x = this.x + this.speed*cos(this.angle);
this.y = this.y + this.speed*sin(this.angle);
this.hasReachedBorder();
this.isColliding();
return;
}
changeAngle() {
var num = random(1)
if (num < 0.05) this.angle += PI/4;
else if(num < 0.1) this.angle -= PI/4;
}
split(){
var num = random(1);
if (num < 0.1) {
this.angle += PI/2
start.push(new Agent(this.x + this.speed*cos(this.angle-PI/2),
this.y + this.speed*sin(this.angle-PI/2),
this.angle-PI/2));
}
}
hasReachedBorder(){
if (this.x > windowHeight) this.x = 0;
else if (this.x < 0) this.x = windowHeight-1;
if (this.y > windowHeight) this.y = 0;
else if (this.y < 0) this.y = windowHeight-1;
}
isColliding(){
if (red(get(this.x + this.speed*cos(this.angle-PI/4),
this.y + this.speed*sin(this.angle-PI/4))) != red(b)) this.isRunning= false;
return;
}
draw(){
if(!this.isRunning) return;
ellipse(this.x, this.y, 5, 5);
return;}
}