36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { expect, test } from "vitest";
|
|
import { type AABB, vec } from "./given.ts";
|
|
import { sweptAABB } from "./swept.ts";
|
|
|
|
test("box moving right into a wall -> hits at the real gap", () => {
|
|
// A right edge at x=2, B left edge at x=5 -> gap 3, speed 10 -> t=0.3
|
|
const a: AABB = { x: 0, y: 0, w: 2, h: 2 };
|
|
const b: AABB = { x: 5, y: 0, w: 2, h: 2 };
|
|
const hit = sweptAABB(a, vec(10, 0), b)!;
|
|
expect(hit.time).toBeCloseTo(0.3);
|
|
expect(hit.normal).toEqual({ x: -1, y: 0 });
|
|
});
|
|
|
|
test("box passes below the wall -> null", () => {
|
|
const a: AABB = { x: 0, y: 10, w: 2, h: 2 };
|
|
const b: AABB = { x: 5, y: 0, w: 2, h: 2 };
|
|
expect(sweptAABB(a, vec(10, 0), b)).toBeNull();
|
|
});
|
|
|
|
test("box moving left into a wall -> normal points right", () => {
|
|
// A left edge at x=10, B right edge at x=2 -> gap 8, speed 10 -> t=0.8
|
|
const a: AABB = { x: 10, y: 0, w: 2, h: 2 };
|
|
const b: AABB = { x: 0, y: 0, w: 2, h: 2 };
|
|
const hit = sweptAABB(a, vec(-10, 0), b)!;
|
|
expect(hit.time).toBeCloseTo(0.8);
|
|
expect(hit.normal).toEqual({ x: 1, y: 0 });
|
|
});
|
|
|
|
test("diagonal where y blocks first -> horizontal normal", () => {
|
|
const a: AABB = { x: 0, y: 0, w: 2, h: 2 };
|
|
const b: AABB = { x: 5, y: 8, w: 2, h: 2 };
|
|
const hit = sweptAABB(a, vec(10, 10), b)!;
|
|
expect(hit.time).toBeCloseTo(0.6);
|
|
expect(hit.normal).toEqual({ x: 0, y: -1 });
|
|
});
|