xxxxxxxxxx
75
class Particle {
constructor(walls) {
this.pos = createVector(width / 2, height / 2);
this.walls = walls;
this.rays = [];
this.target = width / 2, height / 2;
}
update(x, y) {
this.pos.set(x, y);
}
calcRays() {
this.rays.splice(0);
for (let wall of this.walls) {
const rayA = new Ray(this.pos);
rayA.lookAt(wall.a);
const rayB = new Ray(this.pos);
rayB.lookAt(wall.b);
this.rays.push(rayA);
this.rays.push(rayB);
}
}
look() {
const ends = [];
for (let i = 0; i < this.rays.length; i++) {
const ray = this.rays[i];
let closest = null;
let record = Infinity;
for (let wall of this.walls) {
const pt = ray.cast(wall);
if (pt) {
const d = p5.Vector.dist(this.pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
}
if (closest) {
// colorMode(HSB);
// stroke((i + frameCount * 2) % 360, 255, 255, 50);
ends.push(closest);
stroke(255, 165, 0);
line(this.pos.x, this.pos.y, closest.x, closest.y);
}
}
ends.sort((_a, _b) => {
const a = p5.Vector.sub(_a, this.pos);
const b = p5.Vector.sub(_b, this.pos);
return a.heading() - b.heading();
});
fill(255, 255, 0, 102);
noStroke();
beginShape();
for (let pt of ends) {
vertex(pt.x, pt.y);
}
endShape(CLOSE);
}
// show() {
// for (let ray of this.rays) {
// ray.show();
// }
// fill(0);
// ellipse(this.pos.x, this.pos.y, 4);
// }
}