xxxxxxxxxx
41
function Walker(x, y, stuck) {
this.pos = createVector(x || random(width), y || random(height));
this.stuck = stuck;
this.walk = function () {
let vel = p5.Vector.random2D();
this.pos.add(vel);
this.pos.x = constrain(this.pos.x, 0, width);
this.pos.y = constrain(this.pos.y, 0, height);
};
this.checkStuck = function (others) {
for (let i = 0; i < others.length; i++) {
let d = distSq(this.pos, others[i].pos);
if (d < (r * r * 4)) {
this.stuck = true;
return true;
// break;
}
}
return false;
}
this.show = function () {
stroke(255, 100);
if (this.stuck){
fill(255, 0 , 100);
} else {
fill(0);
}
ellipse(this.pos.x, this.pos.y, r*2, r*2);
// sphere(r);
}
}
function distSq(a, b) {
let dx = b.x - a.x;
let dy = b.y - a.y;
return dx * dx + dy * dy;
}