xxxxxxxxxx
235
var cols,rows;
var scl = 20;
var grid = [];
var stack = [];
var current;
let walls = [];
let ray;
let particle;
var gate = false;
const sceneW = 400;
const sceneH = 400;
let sliderFOV;
function setup() {
createCanvas(800, 400);
cols = floor(sceneW/scl);
rows = floor(sceneH/scl);
//maze
for(var j = 0; j < rows; j++){
for(var i = 0; i< cols; i++){
var cell = new Cell(i,j);
grid.push(cell)
}
}
current = grid[0];
//ray
particle = new Particle();
sliderFOV = createSlider(1,180,45);
sliderFOV.input(changeFOV);
}
var gate1 = false;
function draw() {
if(keyIsDown(LEFT_ARROW)){
particle.rotate(-0.05);
}
if(keyIsDown(RIGHT_ARROW)){
particle.rotate(0.05);
}
if(keyIsDown(UP_ARROW)){
particle.move(0.5);
}
if(keyIsDown(DOWN_ARROW)){
particle.move(-0.5);
}
background(51);
if(!gate){
gate = backtracking();
}
if(gate){
for(let wall of walls){
//console.log(wall)
wall.show();
}
particle.show();
fill(255,0,0,10);
rect(sceneW,0,width,sceneH/2);
var scene = particle.look(walls);
var w = sceneW / scene.length;
push();
translate(sceneW,0);
for(let i = 0; i < scene.length; i++){
noStroke();
let b = map(scene[i]*6,0,sceneW,255,0);
fill(b);
let h = map(scene[i],0,sceneW,sceneH,0)
rectMode(CENTER);
rect(i * w + w / 2,sceneH/2,w,h);
}
pop();
}
}
function backtracking(){
for(let i = 0; i < 100; i++){
current.visited = true;
//current.highlight();
//Step 1
var next = current.checkNeigbors();
if (next) {
next.visited = true;
//Step 2
stack.push(current);
//Step 3
removeWalls(current,next);
//Step 4
current = next;
} else if(stack.length > 0){
current = stack.pop();
}
}
if(current == grid[0]){
for(let gridP of grid){
gridP.show();
for(let i = 0; i < gridP.points.length; i+=2){
walls.push(new Boundary(gridP.points[i],gridP.points[i+1]))
}
}
return true;
}else{
return false;
}
}
function index(i,j){
if(i < 0 || j < 0 || i > cols-1|| j > rows-1){
return -1;
}
return i + j * cols;
}
function removeWalls(a,b) {
var x = a.i - b.i;
if(x === 1){
a.walls[3] = false;
b.walls[1] = false;
} else if (x === -1){
a.walls[1] = false;
b.walls[3] = false;
}
var y = a.j - b.j;
if(y === 1){
a.walls[0] = false;
b.walls[2] = false;
} else if (y === -1){
a.walls[2] = false;
b.walls[0] = false;
}
}
function changeFOV(){
let fov = sliderFOV.value();
particle.updateFOV(fov);
}
class Particle{
constructor(){
this.fov = 45;
this.rez = 1;
this.pos = createVector(10,10);
this.rays = [];
this.heading = 0;
for(let a = -this.fov; a < this.fov; a+= this.rez){
this.rays.push(new Ray(this.pos,radians(a)));
}
}
updateFOV(fov){
this.fov = fov;
this.rays = [];
for(let a = -this.fov; a < this.fov; a+= this.rez){
this.rays.push(new Ray(this.pos,radians(a) + this.heading));
}
}
rotate(angle){
this.heading += angle;
let index = 0;
for(let a = -this.fov; a < this.fov; a+= this.rez){
this.rays[index].setAngle(radians(a) + this.heading);
index ++;
}
}
move(m){
var vel = p5.Vector.fromAngle(this.heading);
vel.setMag(m);
this.pos.add(vel);
}
update(x,y){
this.pos.set(x,y);
}
look(walls){
var scene = [];
for(let ray of this.rays){
let closest = null;
let record = Infinity;
for(let wall of walls){
const pt = ray.cast(wall);
if(pt){
let d = p5.Vector.dist(this.pos,pt);
const a = ray.dir.heading() - this.heading;
d *= cos(a);
if(d < record){
record = d;
closest = pt;
}
}
}
if(closest){
stroke(255,10)
line(this.pos.x,this.pos.y,closest.x,closest.y);
}
scene.push(record);
}
return scene;
}
show() {
fill(255);
ellipse(this.pos.x,this.pos.y,16);
//for(let ray of this.rays){
// ray.show();
//}
}
}