xxxxxxxxxx
109
function changeFOV(){
let fov = sliderFOV.value();
particle.updateFOV(fov);
}
function intersectLineCircle(p1, p2, cpt, r) {
let x1 = p1.copy().sub(cpt);
let x2 = p2.copy().sub(cpt);
let dv = x2.copy().sub(x1)
let dr = dv.mag();
let D = x1.x*x2.y - x2.x*x1.y;
let di = r*r*dr*dr - D*D;
if (di < 0.0){
return false;
}else{
return true;
}
}
class Particle{
constructor(){
this.fov = 60;
this.rez = 1;
this.pos = createVector(10,10);
this.rays = [];
this.heading = 0;
this.step = this.fov * da;
for (let a = -this.fov / 2; a < this.fov / 2; a += this.step) {
this.rays.push(new Ray(this.pos, radians(a) + this.heading, radians(a)));
}
}
updateFOV(fov) {
this.fov = fov;
this.rays = [];
for (let a = -this.fov / 2; a < this.fov / 2; a += this.step) {
this.rays.push(new Ray(this.pos, radians(a) + this.heading, radians(a)));
}
}
rotate(angle) {
this.heading += angle;
let index = 0;
for (let a = -this.fov / 2; a < this.fov / 2; a += this.step) {
this.rays[index].setAngle(radians(a) + this.heading, radians(a));
index++;
}
}
move(m){
let gate = true;
for(let wall of walls){
if(intersectLineCircle(wall.a,wall.b,this.pos,1)){
gate = false;
}else{
gate = true;
}
}
if(gate){
var vel = p5.Vector.fromAngle(this.heading);
vel.setMag(m);
this.pos.add(vel);
}
}
update(x,y){
this.pos.set(x,y);
}
look(walls,dr){
var scene = [];
var index = 0;
for(let ray of this.rays){
let closest = null;
let record = Infinity;
let cWall = null;
for(let wall of walls){
const pt = ray.cast(wall);
if(pt){
let d = p5.Vector.dist(this.pos,pt);
if(d < record){
record = d;
closest = pt;
cWall = wall;
}
}
}
if(closest){
cWall.seenByPlayer();
if(dr && (index == (this.fov/this.step)/2 || index == 0 || index == this.rays.length-1)){
stroke(255,0,0,80);
line(this.pos.x,this.pos.y,closest.x,closest.y);
}
}
scene.push(record * cos(ray.angleFromCenter));
index++;
}
return scene;
}
show() {
fill(60);
ellipse(this.pos.x,this.pos.y,16);
}
}