-
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathtileCollision.test.mjs
More file actions
59 lines (51 loc) · 2.24 KB
/
tileCollision.test.mjs
File metadata and controls
59 lines (51 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { TileCollisionLayer, tile, vec2 } from '../dist/littlejs.esm.js';
// Regression for the negative-edge ghost-collision bug:
// collisionTest() clamped minX/minY to 0 and then forced maxX/maxY to at least
// minX+1, which caused an AABB that was entirely off the negative edge of the
// layer to incorrectly check column/row 0. The positive edge was already safe
// because maxX was capped at this.size.x and an out-of-range minX produced an
// empty loop.
function makeLayer()
{
// 4x4 layer rooted at world origin, with a solid tile only at (0, 0).
const layer = new TileCollisionLayer(vec2(0, 0), vec2(4, 4), tile(0, 16), 0, false);
layer.setCollisionData(vec2(0, 0), 1);
return layer;
}
test('collisionTest: AABB entirely off the negative X edge does not collide', () =>
{
const layer = makeLayer();
// 1x1 AABB centered at x=-10: spans x in [-10.5, -9.5], nowhere near the layer.
assert.equal(layer.collisionTest(vec2(-10, 0.5), vec2(1, 1)), false);
});
test('collisionTest: AABB entirely off the negative Y edge does not collide', () =>
{
const layer = makeLayer();
assert.equal(layer.collisionTest(vec2(0.5, -10), vec2(1, 1)), false);
});
test('collisionTest: AABB straddling the negative X edge still collides with col 0', () =>
{
const layer = makeLayer();
// 2-wide AABB centered at x=0 spans x in [-1, 1] — half is inside the layer
// and overlaps cell (0,0), which holds the solid tile.
assert.equal(layer.collisionTest(vec2(0, 0.5), vec2(2, 1)), true);
});
test('collisionTest: point test on cell (0,0) collides', () =>
{
const layer = makeLayer();
// point-test mode (size=0); pos inside the solid tile
assert.equal(layer.collisionTest(vec2(0.5, 0.5), vec2(0, 0)), true);
});
test('collisionTest: AABB entirely off the positive X edge does not collide', () =>
{
const layer = makeLayer();
// confirm the previously-working side stays working
assert.equal(layer.collisionTest(vec2(100, 0.5), vec2(1, 1)), false);
});
test('collisionTest: AABB entirely off the positive Y edge does not collide', () =>
{
const layer = makeLayer();
assert.equal(layer.collisionTest(vec2(0.5, 100), vec2(1, 1)), false);
});