"use strict"; const { describe, it } = require("node:test"); const assert = require("node:assert/strict"); const Camera = require("../src/camera.js"); describe("Camera projection (no fish-eye)", () => { it("plane is perpendicular to forward and length tan(halfFov)", () => { const rot = 0.3; const f = Camera.forward(rot); const p = Camera.plane(rot); assert.ok(Math.abs(f.x * p.x + f.y * p.y) < 1e-9); const plen = Math.sqrt(p.x * p.x + p.y * p.y); assert.ok(Math.abs(plen - Camera.tanHalfFov()) < 1e-9); }); it("straight wall columns share equal perp depth (flat wall ahead)", () => { // Player at origin facing +X; wall at x=100 from y=-50..50. // Hits along camera-plane rays to the wall plane x=100 have perp depth = 100. const rot = 0; const wallX = 100; for (const camX of [-0.9, -0.5, 0, 0.5, 0.9]) { const d = Camera.rayDir(rot, camX); // intersect x = wallX: player.x + d.x * t = wallX → t = wallX/d.x const t = wallX / d.x; const hitX = d.x * t; const hitY = d.y * t; const euclid = Math.sqrt(hitX * hitX + hitY * hitY); const perp = Camera.perpFromEuclid(euclid, rot, camX); assert.ok(Math.abs(perp - wallX) < 1e-6, "camX=" + camX + " perp=" + perp); const depth = Camera.perpDepth(hitX, hitY, 0, 0, rot); assert.ok(Math.abs(depth - wallX) < 1e-6); } }); it("sprite screenX moves smoothly; depth stays perp (not Euclidean)", () => { // NPC at (100, 30), player at origin facing +X: euclid > perp const proj = Camera.projectSprite(100, 30, 0, 0, 0, 320); assert.ok(proj); assert.ok(Math.abs(proj.depth - 100) < 1e-9); const euclid = Math.sqrt(100 * 100 + 30 * 30); assert.ok(proj.depth < euclid); // Rotating in place should keep depth = forward·offset const rot = 0.2; const p2 = Camera.projectSprite(100, 30, 0, 0, rot, 320); const expected = 100 * Math.cos(rot) + 30 * Math.sin(rot); assert.ok(Math.abs(p2.depth - expected) < 1e-9); }); it("NPC in front of wall keeps smaller depth than wall when only rotating", () => { // Wall at x=120, NPC at (80, 10). Player at origin, rotate ±0.4. const wallX = 120; const npc = { x: 80, y: 10 }; for (const rot of [-0.4, -0.2, 0, 0.2, 0.4]) { const npcDepth = Camera.perpDepth(npc.x, npc.y, 0, 0, rot); // Wall hit for the column under the NPC const proj = Camera.projectSprite(npc.x, npc.y, 0, 0, rot, 320); assert.ok(proj && proj.depth > 0); const camX = (2 * proj.screenX / 320) - 1; const d = Camera.rayDir(rot, camX); const t = wallX / d.x; // player at 0 const hitY = d.y * t; const wallDepth = Camera.perpDepth(wallX, hitY, 0, 0, rot); assert.ok(npcDepth < wallDepth, "rot=" + rot + " npc=" + npcDepth + " wall=" + wallDepth); } }); });