xxxxxxxxxx
111
let d;
let pIndex;
let count = 10000;
function setup() {
createCanvas(900, 900);
d = pixelDensity();
cols = ["#FFCF16"]
molds = [];
for(i = 0; i < count; i++){
molds[molds.length] = new Mold(random(width / 2 - 20, width / 2 + 20), random(height / 2 - 20, height / 2 + 20));
}
strokeWeight(1);
background(30);
}
function draw() {
background(0, 5);
stroke(255);
noFill();
strokeWeight(0.1)
for(i = 0; i < 19; i++){
stroke(cols[floor(random(cols.length))]);
// circle(width / 2, height / 2, 50 * i);
}
strokeWeight(1);
loadPixels()
for(i = 0; i < molds.length; i++){
molds[i].update();
}
}
class Food{
constructor(x, y){
this.pos = createVector(x, y);
this.size = random(20, 50);
}
}
class Mold{
constructor(x, y){
this.pos = createVector(x, y);
this.vel = createVector(1, 0);
this.vel.setHeading(random(TWO_PI));
this.senseDist = createVector(10, 0);
this.senseDist.setHeading(this.vel.heading() - QUARTER_PI);
this.lSense = p5.Vector.add(this.pos, this.senseDist);
this.senseDist.setHeading(this.vel.heading() + QUARTER_PI);
this.rSense = p5.Vector.add(this.pos, this.senseDist);
this.senseDist.setHeading(this.vel.heading());
this.fSense = p5.Vector.add(this.pos, this.senseDist);
this.lV = 0;
this.rV = 0;
this.fV = 0;
this.c = floor(random(cols.length));
this.col = color(cols[this.c]);
}
update(){
if(this.lSense.x > 0 && this.lSense.x < width && this.lSense.y > 0 && this.lSense.y < height){
pIndex = 4 * (d * floor(this.lSense.y)) * (d * width) + 4 * (d * floor(this.lSense.x));
this.lV = pixels[pIndex];
}
if(this.rSense.x > 0 && this.rSense.x < width && this.rSense.y > 0 && this.rSense.y < height){
pIndex = 4 * (d * floor(this.rSense.y)) * (d * width) + 4 * (d * floor(this.rSense.x));
this.rV = pixels[pIndex];
}
if(this.fSense.x > 0 && this.fSense.x < width && this.fSense.y > 0 && this.fSense.y < height){
pIndex = 4 * (d * floor(this.fSense.y)) * (d * width) + 4 * (d * floor(this.fSense.x));
this.fV = pixels[pIndex];
}
if(this.lV > this.rV && this.lV > this.fV){
this.vel.setHeading(this.vel.heading() - QUARTER_PI)
} else if(this.rV > this.lV && this.rV > this.fV){
this.vel.setHeading(this.vel.heading() + QUARTER_PI)
}
if(this.lV == this.fV && this.rV == this.fV && this.lV == this.rV){
this.vel.setHeading(this.vel.heading() + random(-QUARTER_PI, QUARTER_PI))
}
this.pos.add(this.vel);
this.pos.x = this.pos.x % width;
this.pos.y = this.pos.y % height;
// if(this.pos.x < 0 || this.pos.x > width || this.pos.y < 0 || this.pos.y > height){
// this.vel.setMag(-1);
// }
this.senseDist.setHeading(this.vel.heading() - QUARTER_PI);
this.lSense = p5.Vector.add(this.pos, this.senseDist);
this.senseDist.setHeading(this.vel.heading() + QUARTER_PI);
this.rSense = p5.Vector.add(this.pos, this.senseDist);
this.senseDist.setHeading(this.vel.heading());
this.fSense = p5.Vector.add(this.pos, this.senseDist);
stroke(this.col);
point(this.pos.x, this.pos.y);
}
}