xxxxxxxxxx
78
let walls = [];
let ray;
let particle;
const sceneW = 700;
const sceneH = 700;
let sliderFOV;
function setup() {
createCanvas(700, 1400);
for (let i = 0; i < 6; i++) {
let x1 = random(sceneW);
let x2 = random(sceneW);
let y1 = random(sceneH);
let y2 = random(sceneH);
let r = random(255);
let g = random(255);
let b = random(255);
walls[i] = new Boundary(x1, y1, x2, y2, r, g, b);
}
walls.push(new Boundary(0, 0, sceneW, 0, 255, 255, 255));
walls.push(new Boundary(sceneW, 0, sceneW, sceneH, 255, 255, 255));
walls.push(new Boundary(sceneW, sceneH, 0, sceneH, 255, 255, 255));
walls.push(new Boundary(0, sceneH, 0, 0, 255, 255, 255));
particle = new Particle();
sliderFOV = createSlider(0, 120, 90);
sliderFOV.input(changeFOV);
sliderFOV.position(10, 10);
}
function changeFOV() {
const fov = sliderFOV.value();
particle.updateFOV(fov);
}
function draw() {
if (keyIsDown(LEFT_ARROW)) {
particle.rotate(-0.01);
} else if (keyIsDown(RIGHT_ARROW)) {
particle.rotate(0.01);
} else if (keyIsDown(UP_ARROW)) {
particle.move(1);
} else if (keyIsDown(DOWN_ARROW)) {
particle.move(-1);
}
background(0);
for (let wall of walls) {
wall.show();
}
particle.show();
const scene = particle.look(walls);
const sliceWidth = sceneW / scene.length;
push();
for (let i = 0; i < scene.length; i++) {
noStroke();
// Calculate how far away the wall is relative to whole scene
// We can look as far as the sceneW * sceneH (which is a square in this case)
// (inverse square law)
const sq = scene[i][0] * scene[i][0];
const wSq = sceneW * sceneH;
const brightness = map(sq, 0, wSq, 255, 0);
const sliceHeight = map(scene[i][0], 0, sceneW, sceneH, 0);
// Add the view
col = scene[i][1].levels;
fill(col[0] * (brightness / 255), col[1] * (brightness / 255), col[2] * (brightness / 255));
rectMode(CENTER);
rect(i * sliceWidth, sceneH * (3/2), sliceWidth + 1, sliceHeight);
}
pop();
}