xxxxxxxxxx
39
class Ray{
constructor(pos,angle){
this.pos = pos;
this.dir = p5.Vector.fromAngle(angle);
}
lookAt(x,y){
this.dir.x = x - this.pos.x;
this.dir.y = y - this.pos.y;
this.dir.normalize();
}
show(){
stroke(255,255,255,50);
push();
translate(this.pos.x,this.pos.y);
line(0,0,this.dir.x,this.dir.y);
pop();
}
cast(wall){
const den = (wall.a.x - wall.b.x) * (this.pos.y - (this.pos.y + this.dir.y)) - (wall.a.y - wall.b.y) * (this.pos.x - (this.pos.x + this.dir.x));
if(den == 0){
return;
}
const t = ((wall.a.x - this.pos.x) * (this.pos.y - (this.pos.y + this.dir.y)) - (wall.a.y - this.pos.y) * (this.pos.x - (this.pos.x + this.dir.x))) / den;
const u = -((wall.a.x - wall.b.x) * (wall.a.y - this.pos.y) - (wall.a.y - wall.b.y) * (wall.a.x - this.pos.x)) / den;
if(t > 0 && t < 1 && u > 0){
const pt = createVector(wall.a.x + t *(wall.b.x - wall.a.x),wall.a.y + t * (wall.b.y-wall.a.y));
return pt;
}else{
return;
}
}
}