xxxxxxxxxx
54
class Ray{
constructor(pos,vec,length,offSet){
this.unit = createVector(-1,0);
this.vec = vec;
this.length = length;
this.offSet = radians(offSet);
this.angle = this.unit.angleBetween(this.vec);
//this.angle += this.offSet;
this.pos = pos;
this.dir = p5.Vector.fromAngle(this.angle);
}
lookAt(){
this.dir.x = this.vec.x - this.pos.x;
this.dir.y = this.vec.y - this.pos.y;
this.dir.normalize();
}
cast(wall) {
const x1 = wall.a.x;
const y1 = wall.a.y;
const x2 = wall.b.x;
const y2 = wall.b.y;
const x3 = this.pos.x;
const y3 = this.pos.y;
const x4 = this.pos.x + this.dir.x;
const y4 = this.pos.y + this.dir.y;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (den == 0) {
return;
}
let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
let u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
if (t > 0 && t < 1 && u > 0) {
const pt = createVector();
pt.x = x1 + t * (x2 - x1);
pt.y = y1 + t * (y2 - y1);
return pt;
} else {
return;
}
}
}