xxxxxxxxxx
91
let bugs = [];
function setup() {
createCanvas(700, 500);
init();
}
function mousePressed() {
init();
}
function init() {
bugs = [];
noiseSeed(millis());
background(255);
for (let i = 0; i < 10; i++) {
var px = random(width);
var py = random(height);
var heading = random(TWO_PI);
bugs.push(new Bug(px, py, heading));
bugs.push(new Bug(px, py, heading + PI));
}
}
function draw() {
updatePixels();
var bDidIt = false;
for (let i = 0; i < bugs.length; i++) {
if (!bDidIt && bugs[i].plive) {
if (random(1.0) < 0.05) {
var px = bugs[i].px;
var py = bugs[i].py;
var pheading = bugs[i].pheading;
if (!isNaN(px) && !isNaN(py)) {
var sig = (random(1) < 0.5) ? -1:1;
bugs.push(new Bug(px, py, pheading + sig*HALF_PI));
bDidIt = true;
}
}
}
}
stroke(0);
strokeWeight(1);
strokeCap(SQUARE); // important
for (let i = 0; i < bugs.length; i++) {
bugs[i].update();
}
}
class Bug {
constructor(ipx, ipy, iheading) {
this.px = ipx;
this.py = ipy;
this.pheading = iheading;
this.plive = true;
}
update() {
if (this.px > 0 && this.px < width && this.py > 0 && this.py < height) {
} else {
this.plive = false;
}
if (this.plive) {
var noiseInfluence = 0.05;
var noiseScale = 0.1;
this.pheading +=
noiseInfluence *
(noise(this.px * noiseScale, this.py * noiseScale) - 0.5);
var vx = cos(this.pheading);
var vy = sin(this.pheading);
//print(this.px + " " + this.py);
this.px += vx;
this.py += vy;
;
if (!isNaN(this.px) && !isNaN(this.py)) {
var fpx = this.px;
var fpy = this.py;
var pcol = get(fpx, fpy);
if (pcol[0] < 200) {
this.plive = false;
}
line(this.px, this.py, this.px - vx, this.py - vy);
}
}
}
}