Split The code is public; the app answers only to the crew.

The geometry nothing tests: `trace.ts` gets its first test, and reports the exit fraction it already computes #83

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/cs:scout/78-the-geometry-nothing-tests-trace-ts-gets → main 1 commit fork

  • Merged. scout/78-the-geometry-nothing-tests-trace-ts-gets was merged into main as 5921387.

Review

Sign in to review this change.
2 files changed +90 −3
src/client/trace.test.ts added +87
@@ -0,0 +1,87 @@
1 import { expect, test } from "bun:test";
2 import { boxes as mapBoxes, sites, spawns } from "./map.ts";
3 import type { Vec3 } from "./math.ts";
4 import { rayBox, traceWorld, visible } from "./trace.ts";
5
6 const box = { min: [0, 0, 0] as Vec3, max: [10, 10, 10] as Vec3 };
7
8 const FACES: { axis: 0 | 1 | 2; sign: -1 | 1; from: Vec3; to: Vec3 }[] = [
9 { axis: 0, sign: -1, from: [-10, 5, 5], to: [20, 5, 5] },
10 { axis: 0, sign: 1, from: [20, 5, 5], to: [-10, 5, 5] },
11 { axis: 1, sign: -1, from: [5, -10, 5], to: [5, 20, 5] },
12 { axis: 1, sign: 1, from: [5, 20, 5], to: [5, -10, 5] },
13 { axis: 2, sign: -1, from: [5, 5, -10], to: [5, 5, 20] },
14 { axis: 2, sign: 1, from: [5, 5, 20], to: [5, 5, -10] },
15 ];
16
17 test("rayBox finds the entry face and fraction on all six sides of a box", () => {
18 for (const { axis, sign, from, to } of FACES) {
19 const hit = rayBox(from, to, box.min, box.max);
20 expect(hit).not.toBeNull();
21 expect(hit?.t).toBeCloseTo(1 / 3, 6);
22 const normal: Vec3 = [0, 0, 0];
23 normal[axis] = sign;
24 expect(hit?.normal).toEqual(normal);
25 }
26 });
27
28 test("a ray parallel to an axis and outside that axis's slab misses, even if the other axes would cross", () => {
29 // y is constant at 50, outside the box's [0, 10] on y; x alone would enter.
30 expect(rayBox([-5, 50, 5], [5, 50, 5], box.min, box.max)).toBeNull();
31 });
32
33 test("a ray parallel to two axes and inside both slabs still finds the hit on the axis that moves", () => {
34 // y and z are constant at 5, inside the box's [0, 10] on both; only x moves.
35 const hit = rayBox([-5, 5, 5], [15, 5, 5], box.min, box.max);
36 expect(hit).not.toBeNull();
37 expect(hit?.normal).toEqual([-1, 0, 0]);
38 });
39
40 test("a segment that stops short of the box never reaches it", () => {
41 expect(rayBox([-5, 5, 5], [-1, 5, 5], box.min, box.max)).toBeNull();
42 });
43
44 test("a ray starting inside the box today reports t = 0 and the axis-0 face, whichever way it leaves", () => {
45 // tMin starts at 0 and every axis's entry fraction from inside is negative
46 // (behind the start), so the slab loop never moves it off its initial axis.
47 // This pins that behaviour rather than inventing a "correct" one.
48 const hit = rayBox([5, 5, 5], [20, 5, 5], box.min, box.max);
49 expect(hit).not.toBeNull();
50 expect(hit?.t).toBe(0);
51 expect(hit?.normal).toEqual([-1, 0, 0]);
52 });
53
54 test("rayBox also returns the exit fraction, without disturbing the entry one", () => {
55 const hit = rayBox([-5, 5, 5], [15, 5, 5], box.min, box.max);
56 expect(hit?.t).toBeCloseTo(0.25, 6);
57 expect(hit?.tExit).toBeCloseTo(0.75, 6);
58 });
59
60 test("traceWorld finds the nearest of several boxes, and what is the box itself", () => {
61 const near = { min: [10, -1, -1] as Vec3, max: [12, 1, 1] as Vec3, mat: 0 };
62 const far = { min: [20, -1, -1] as Vec3, max: [22, 1, 1] as Vec3, mat: 0 };
63 const behind = { min: [-12, -1, -1] as Vec3, max: [-10, -1, -1] as Vec3, mat: 0 };
64 const hit = traceWorld([0, 0, 0], [30, 0, 0], [far, near, behind]);
65 expect(hit).not.toBeNull();
66 expect(hit?.what).toBe(near);
67 expect(hit?.point[0]).toBeCloseTo(10, 6);
68 });
69
70 test("visible is false through a wall and true down an open corridor, on the real map", () => {
71 const eye = 40;
72 // A and B are on opposite sides of the map with walls and doors between
73 // them: no straight line between the two sites' centres is ever clear.
74 expect(visible(
75 [sites.A[0], sites.A[1], eye],
76 [sites.B[0], sites.B[1], eye],
77 mapBoxes,
78 )).toBe(false);
79 // CT spawn and A site sit at opposite ends of the same open run (ct mid,
80 // then the link into the site), with nothing solid staged between them.
81 const ctSpawn = spawns.ct[12] as Vec3;
82 expect(visible(
83 [ctSpawn[0], ctSpawn[1], eye],
84 [sites.A[0], sites.A[1], eye],
85 mapBoxes,
86 )).toBe(true);
87 });
src/client/trace.ts +3 −3
@@ -8,13 +8,13 @@ import { sub, length, type Vec3 } from "./math.ts";
88
99 export type Hit = { t: number; point: Vec3; normal: Vec3; what: unknown };
1010
11 /** Slab test. Returns the entry fraction along `from → to`, or null. */
11 /** Slab test. Returns the entry and exit fractions along `from → to`, or null. */
1212 export function rayBox(
1313 from: Vec3,
1414 to: Vec3,
1515 min: Vec3,
1616 max: Vec3,
17 ): { t: number; normal: Vec3 } | null {
17 ): { t: number; tExit: number; normal: Vec3 } | null {
1818 let tMin = 0, tMax = 1;
1919 let axis = 0, sign = -1;
2020 for (let i = 0; i < 3; i++) {
@@ -34,7 +34,7 @@ export function rayBox(
3434 }
3535 const normal: Vec3 = [0, 0, 0];
3636 normal[axis] = sign;
37 return { t: tMin, normal };
37 return { t: tMin, tExit: tMax, normal };
3838 }
3939
4040 /** The nearest solid hit along a segment, or null if the segment is clear. */

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs