xxxxxxxxxx
69
let player, world;
let objects = [];
const fov = 1.35;
const res = 20; // PLAY WITH THIS (pixel resolution)
const speed = 0.1; // PLAY WITH THIS (ship speed)
function setup() {
frameRate(50); // PLAY WITH THIS (framerate max)
createCanvas(600, 600);
player = new Player([speed,speed,speed,speed,speed], res, fov);
objects.push(new WorldObject(0, 0, -1, 0, 0.2));
}
function draw() {
//background(220, 50);
background(0);
if (keyIsDown(32)) player.moveForw(); // SPACEBAR
if (keyIsDown(96)) player.moveBack(); // 0
if (keyIsDown(65)) player.lookLeft(); // a
if (keyIsDown(68)) player.lookRight(); // d
if (keyIsDown(87)) player.lookUp(); // w
if (keyIsDown(83)) player.lookDown(); // s
if (keyIsDown(79)) player.rollLeft(); // o
if (keyIsDown(80)) player.rollRight(); // p
if (keyIsDown(LEFT_ARROW)) player.strafeLeft(); // Left Arrow
if (keyIsDown(RIGHT_ARROW)) player.strafeRight(); // Right Arrow
if (keyIsDown(UP_ARROW)) player.strafeUp(); // Left Arrow
if (keyIsDown(DOWN_ARROW)) player.strafeDown(); // Right Arrow
// Ray tracing section:
//objects.push(player);
push();
noStroke();
rectMode(CENTER);
let marcher = new Marcher(player.phi);
for (let mi = 0; mi < res; mi++) {
for (let ni = 0; ni < res; ni++) {
marcher.reset();
marcher.turnRight(player.fov / 2 - (ni+0.5) * player.fov / player.res);
marcher.turnDown(player.fov / 2 - (mi+0.5) * player.fov / player.res);
marcher.step(player.r + 0.001);
let col = 255 * (1 - player.rayMarch(marcher, objects, 0) / TWO_PI);
fill(col);
rect((ni + 0.5) * width / res, (mi + 0.5) * height / res, width / res, height / res);
}
}
pop();
//objects.pop();
noStroke();
fill(128);
text("FR: " + frameRate().toFixed(1), width - 55, 20);
text("x: " + player.phi[0][0].toFixed(2), width - 50, 40);
text("y: " + player.phi[1][0].toFixed(2), width - 50, 60);
text("z: " + player.phi[2][0].toFixed(2), width - 50, 80);
text("w: " + player.phi[3][0].toFixed(2), width - 50, 100);
text("dx: " + player.phi[0][1].toFixed(2), width - 50, 130);
text("dy: " + player.phi[1][1].toFixed(2), width - 50, 150);
text("dz: " + player.phi[2][1].toFixed(2), width - 50, 170);
text("dw: " + player.phi[3][1].toFixed(2), width - 50, 190);
}
function keyPressed() {
if (keyCode === ENTER) {
objects.push(new WorldObject(player.phi[0][0], player.phi[1][0], player.phi[2][0], player.phi[3][0], 0.05));
} else if (keyCode === BACKSPACE) {
objects.pop();
}
}