xxxxxxxxxx
158
// Ray Casting and Marching Version 3
// Created By NSB
// Note for Dan:<3 your videos. Keep up the great work.
// Use the projected distance to get rid of the fisheye
// Matt Godbolt has an excelent video on this topic.
// Look around 3:00 and 10:05 for more info.
// https://www.youtube.com/watch?v=eOCQfxRQ2pY
let totalWalls = 5;
let scale = 50;
let slider;
let check;
let button;
let col;
let MantaRay;
let rays = [];
let walls = [];
let lines = [];
let pos;
function setup() {
createCanvas(800, 400);
angleMode(DEGREES);
stroke(255);
slider = createSlider(0, 180, 100, 2);
slider.position(85);
check = createCheckbox();
check.position(25);
button = createButton('New Walls');
button.position(310);
button.mousePressed(makeWalls);
col = createColorPicker('red');
col.position(250);
pos = createVector(width / 4, height / 2);
MantaRay = new Ray(pos, 0);
//Raylet Generation
makeRays();
//Wall Generation
makeWalls();
}
function draw() {
background(0);
// noLoop();
lines = []
ui();
// MantaRay.show();
// MantaRay.lookAt(mouseX, mouseY);
makeRays();
for (let wall of walls) {
wall.show();
}
if (check.checked()) {
castMagic2();
// castMagic();
} else {
castMagic2();
}
scene();
}
function castMagic() {
for (let ray of rays) {
let closest = null;
let record = Infinity;
for (let wall of walls) {
let pt = ray.cast(wall);
if (pt) {
let d = p5.Vector.dist(pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
}
if (closest) {
line(pos.x, pos.y, closest.x, closest.y);
render(closest);
}
}
}
function marchMagic() {
for (let ray of rays) {
let closest = null;
let record = Infinity;
for (let wall of walls) {
let pt = ray.march(wall);
if (pt) {
let d = p5.Vector.dist(pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
}
if (closest) {
line(pos.x, pos.y, closest.x, closest.y);
render(closest);
}
}
}
function makeWalls() {
//Wall Generation
walls = []
for (let j = 0; j < totalWalls; j++) {
// walls[j] = new Barrier(200, 100, 300, 250);
walls[j] = new Barrier(random(width / 4, width / 2), random(0, height / 2), random(width / 4, width / 2), random(height / 2, height));
let x1 = random(width - sceneW);
let y1 = random(height);
let x2 = random(width - sceneW);
let y2 = random(height);
let type = 1;
walls[j] = new Barrier(x1, y1, x2, y2, type);
}
walls.push(new Barrier(0, 0, width / 2, 0));
walls.push(new Barrier(width / 2, 0, width / 2, height));
walls.push(new Barrier(0, 0, 0, height));
walls.push(new Barrier(0, height, width / 2, height));
// walls.push(new Barrier(0,190,width/2,190));
// walls.push(new Barrier(230,0,290,height));
// for (let i = 200; i > 0; i--) {
// lines.push(i);
// }
}
function makeRays() {
rays = [];
for (let a = -(slider.value()) / 2; a <= slider.value() / 2; a += 1) {
rays.push(new Ray(pos, a));
}
}
function ui() {
push();
fill(255);
text('FOV: ' + slider.value(), 100, height - 10);
text('Wall Color', 250, height - 10);
if (check.checked()) {
// text('Not Working', 5, height - 25);
text('Fisheye', 15, height - 10);
} else {
text('Cast', 22, height - 10);
pop();
}
}