@inglorious/utils 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +10 -11
  2. package/package.json +4 -4
  3. package/src/algorithms/decision-tree.js +24 -0
  4. package/src/algorithms/decision-tree.test.js +153 -0
  5. package/src/algorithms/path-finding.js +150 -0
  6. package/src/algorithms/path-finding.test.js +151 -0
  7. package/src/algorithms/types.d.ts +28 -0
  8. package/src/data-structures/array.js +93 -0
  9. package/src/data-structures/array.test.js +185 -0
  10. package/src/data-structures/board.js +159 -0
  11. package/src/data-structures/board.test.js +242 -0
  12. package/src/data-structures/boolean.js +9 -0
  13. package/src/data-structures/heap.js +164 -0
  14. package/src/data-structures/heap.test.js +103 -0
  15. package/src/data-structures/object.js +138 -0
  16. package/src/data-structures/object.test.js +218 -0
  17. package/src/data-structures/objects.js +66 -0
  18. package/src/data-structures/objects.test.js +99 -0
  19. package/src/data-structures/set.js +9 -0
  20. package/src/data-structures/set.test.js +15 -0
  21. package/src/data-structures/tree.js +36 -0
  22. package/src/data-structures/tree.test.js +33 -0
  23. package/src/data-structures/types.d.ts +4 -0
  24. package/src/functions/functions.js +19 -0
  25. package/src/functions/functions.test.js +23 -0
  26. package/src/math/geometry/circle.js +69 -0
  27. package/src/math/geometry/circle.test.js +97 -0
  28. package/src/math/geometry/hitmask.js +70 -0
  29. package/src/math/geometry/hitmask.test.js +155 -0
  30. package/src/math/geometry/line.js +34 -0
  31. package/src/math/geometry/line.test.js +49 -0
  32. package/src/math/geometry/point.js +78 -0
  33. package/src/math/geometry/point.test.js +81 -0
  34. package/src/math/geometry/rectangle.js +76 -0
  35. package/src/math/geometry/rectangle.test.js +42 -0
  36. package/src/math/geometry/segment.js +72 -0
  37. package/src/math/geometry/segment.test.js +183 -0
  38. package/src/math/geometry/triangle.js +15 -0
  39. package/src/math/geometry/triangle.test.js +11 -0
  40. package/src/math/geometry/types.d.ts +23 -0
  41. package/src/math/linear-algebra/2d.js +28 -0
  42. package/src/math/linear-algebra/2d.test.js +17 -0
  43. package/src/math/linear-algebra/quaternion.js +22 -0
  44. package/src/math/linear-algebra/quaternion.test.js +25 -0
  45. package/src/math/linear-algebra/quaternions.js +20 -0
  46. package/src/math/linear-algebra/quaternions.test.js +29 -0
  47. package/src/math/linear-algebra/types.d.ts +4 -0
  48. package/src/math/linear-algebra/vector.js +337 -0
  49. package/src/math/linear-algebra/vector.test.js +265 -0
  50. package/src/math/linear-algebra/vectors.js +122 -0
  51. package/src/math/linear-algebra/vectors.test.js +65 -0
  52. package/src/math/linear-interpolation.js +9 -0
  53. package/src/math/numbers.js +90 -0
  54. package/src/math/numbers.test.js +137 -0
  55. package/src/math/rng.js +44 -0
  56. package/src/math/rng.test.js +39 -0
  57. package/src/math/statistics.js +43 -0
  58. package/src/math/statistics.test.js +47 -0
  59. package/src/math/trigonometry.js +89 -0
  60. package/src/math/trigonometry.test.js +52 -0
  61. package/src/physics/acceleration.js +56 -0
  62. package/src/physics/friction.js +24 -0
  63. package/src/physics/friction.test.js +42 -0
  64. package/src/physics/gravity.js +69 -0
  65. package/src/physics/gravity.test.js +77 -0
  66. package/src/physics/jump.js +31 -0
  67. package/src/physics/velocity.js +32 -0
@@ -0,0 +1,99 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { extend, merge } from "./objects.js"
4
+
5
+ test("it should extend an object with another, producing a new object as a result", () => {
6
+ const obj1 = {
7
+ primitiveKept: 1,
8
+ primitiveMerged: 2,
9
+ primitiveArrayKept: [1, 2],
10
+ primitiveArrayMerged: [3, 4],
11
+ primitiveObjectKept: { a: 1 },
12
+ primitiveObjectMerged: { b: 2, c: 3 },
13
+ nestedArrayKept: [{ a: 1 }],
14
+ nestedArrayMerged: [{ b: 2, c: 3 }],
15
+ nestedObjectKept: { a: { b: 2 } },
16
+ nestedObjectMerged: { c: { d: 4 } },
17
+ }
18
+ const obj2 = {
19
+ primitiveMerged: 3,
20
+ primitiveAdded: 4,
21
+ primitiveArrayMerged: [5],
22
+ primitiveArrayAdded: [6, 7],
23
+ primitiveObjectMerged: { c: 4 },
24
+ primitiveObjectAdded: { d: 4 },
25
+ nestedArrayMerged: [{ d: 4 }],
26
+ nestedArrayAdded: [{ e: 5 }],
27
+ nestedObjectMerged: { c: { e: 5 } },
28
+ nestedObjectAdded: { f: { g: 7 } },
29
+ }
30
+ const expectedResult = {
31
+ primitiveKept: 1,
32
+ primitiveMerged: 3,
33
+ primitiveAdded: 4,
34
+ primitiveArrayKept: [1, 2],
35
+ primitiveArrayMerged: [5, 4],
36
+ primitiveArrayAdded: [6, 7],
37
+ primitiveObjectKept: { a: 1 },
38
+ primitiveObjectMerged: { b: 2, c: 4 },
39
+ primitiveObjectAdded: { d: 4 },
40
+ nestedArrayKept: [{ a: 1 }],
41
+ nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
42
+ nestedArrayAdded: [{ e: 5 }],
43
+ nestedObjectKept: { a: { b: 2 } },
44
+ nestedObjectMerged: { c: { d: 4, e: 5 } },
45
+ nestedObjectAdded: { f: { g: 7 } },
46
+ }
47
+
48
+ const result = extend(obj1, obj2)
49
+ expect(result).toStrictEqual(expectedResult)
50
+ expect(result).not.toBe(obj1)
51
+ })
52
+
53
+ test("it should deep merge an two objects, changing the first object in place", () => {
54
+ const obj1 = {
55
+ primitiveKept: 1,
56
+ primitiveMerged: 2,
57
+ primitiveArrayKept: [1, 2],
58
+ primitiveArrayMerged: [3, 4],
59
+ primitiveObjectKept: { a: 1 },
60
+ primitiveObjectMerged: { b: 2, c: 3 },
61
+ nestedArrayKept: [{ a: 1 }],
62
+ nestedArrayMerged: [{ b: 2, c: 3 }],
63
+ nestedObjectKept: { a: { b: 2 } },
64
+ nestedObjectMerged: { c: { d: 4 } },
65
+ }
66
+ const obj2 = {
67
+ primitiveMerged: 3,
68
+ primitiveAdded: 4,
69
+ primitiveArrayMerged: [5],
70
+ primitiveArrayAdded: [6, 7],
71
+ primitiveObjectMerged: { c: 4 },
72
+ primitiveObjectAdded: { d: 4 },
73
+ nestedArrayMerged: [{ d: 4 }],
74
+ nestedArrayAdded: [{ e: 5 }],
75
+ nestedObjectMerged: { c: { e: 5 } },
76
+ nestedObjectAdded: { f: { g: 7 } },
77
+ }
78
+ const expectedResult = {
79
+ primitiveKept: 1,
80
+ primitiveMerged: 3,
81
+ primitiveAdded: 4,
82
+ primitiveArrayKept: [1, 2],
83
+ primitiveArrayMerged: [5, 4],
84
+ primitiveArrayAdded: [6, 7],
85
+ primitiveObjectKept: { a: 1 },
86
+ primitiveObjectMerged: { b: 2, c: 4 },
87
+ primitiveObjectAdded: { d: 4 },
88
+ nestedArrayKept: [{ a: 1 }],
89
+ nestedArrayMerged: [{ b: 2, c: 3, d: 4 }],
90
+ nestedArrayAdded: [{ e: 5 }],
91
+ nestedObjectKept: { a: { b: 2 } },
92
+ nestedObjectMerged: { c: { d: 4, e: 5 } },
93
+ nestedObjectAdded: { f: { g: 7 } },
94
+ }
95
+
96
+ const result = merge(obj1, obj2)
97
+ expect(result).toStrictEqual(expectedResult)
98
+ expect(result).toBe(obj1)
99
+ })
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Ensures that the given value is a Set. If it's not a Set, it creates one from the value.
3
+ *
4
+ * @param {*} value - The value to check.
5
+ * @returns {Set} - The original Set or a new Set created from the value.
6
+ */
7
+ export function ensureSet(value) {
8
+ return value instanceof Set ? value : new Set(value)
9
+ }
@@ -0,0 +1,15 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { ensureSet } from "./set.js"
4
+
5
+ test("it should return the same array if the value is already an array", () => {
6
+ const set = new Set([1, "two", { three: 3 }])
7
+
8
+ expect(ensureSet(set)).toBe(set)
9
+ })
10
+
11
+ test("it should wrap the value in an array if it is not an array", () => {
12
+ const str = "hello"
13
+
14
+ expect(ensureSet(str)).toStrictEqual(new Set(["h", "e", "l", "o"]))
15
+ })
@@ -0,0 +1,36 @@
1
+ /** @typedef {import('./types').Tree} Tree */
2
+
3
+ /**
4
+ * Performs a breadth-first search (BFS) traversal on a tree structure.
5
+ * @param {Tree} tree - The root node of the tree.
6
+ * @returns {Array} An array of values in BFS order.
7
+ */
8
+ export function bfs(tree) {
9
+ const result = []
10
+ const queue = [tree]
11
+
12
+ while (queue.length) {
13
+ const node = queue.shift()
14
+ result.push(node.value)
15
+ if (node.children) {
16
+ queue.push(...node.children)
17
+ }
18
+ }
19
+
20
+ return result
21
+ }
22
+
23
+ /**
24
+ * Performs a depth-first search (DFS) traversal on a tree structure.
25
+ * @param {Tree} tree - The root node of the tree.
26
+ * @returns {Array} An array of values in DFS order.
27
+ */
28
+ export function dfs(tree) {
29
+ const result = [tree.value]
30
+
31
+ if (tree.children) {
32
+ result.push(...tree.children.flatMap(dfs))
33
+ }
34
+
35
+ return result
36
+ }
@@ -0,0 +1,33 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { bfs, dfs } from "./tree.js"
4
+
5
+ test("it should perform Breadth-First Search on a tree", () => {
6
+ const tree = {
7
+ value: 1,
8
+ children: [
9
+ { value: 2, children: [{ value: 4 }, { value: 5 }] },
10
+ { value: 3, children: [{ value: 6 }, { value: 7 }] },
11
+ ],
12
+ }
13
+ const expectedResult = [1, 2, 3, 4, 5, 6, 7]
14
+
15
+ const result = bfs(tree)
16
+
17
+ expect(result).toStrictEqual(expectedResult)
18
+ })
19
+
20
+ test("it should perform Depth-First Search on a tree", () => {
21
+ const tree = {
22
+ value: 1,
23
+ children: [
24
+ { value: 2, children: [{ value: 3 }, { value: 4 }] },
25
+ { value: 5, children: [{ value: 6 }, { value: 7 }] },
26
+ ],
27
+ }
28
+ const expectedResult = [1, 2, 3, 4, 5, 6, 7]
29
+
30
+ const result = dfs(tree)
31
+
32
+ expect(result).toStrictEqual(expectedResult)
33
+ })
@@ -0,0 +1,4 @@
1
+ export interface Tree {
2
+ value: any
3
+ children: Tree[]
4
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Composes multiple functions from right to left, as if every function wraps the next one.
3
+ *
4
+ * @param {...Function} fns - Functions to compose.
5
+ * @returns {Function} A function that takes an initial value and applies the composed functions.
6
+ */
7
+ export function compose(...fns) {
8
+ return (x) => fns.reduceRight((acc, fn) => fn(acc), x)
9
+ }
10
+
11
+ /**
12
+ * Pipes multiple functions from left to right, as if the functions are applied one by one.
13
+ *
14
+ * @param {...Function} fns - Functions to pipe.
15
+ * @returns {Function} A function that takes an initial value and applies the piped functions.
16
+ */
17
+ export function pipe(...fns) {
18
+ return (x) => fns.reduce((acc, fn) => fn(acc), x)
19
+ }
@@ -0,0 +1,23 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { compose, pipe } from "./functions.js"
4
+
5
+ test("it should compose functions", () => {
6
+ const shout = (x) => x.toUpperCase()
7
+ const punctuate = (mark) => (x) => `${x}${mark}`
8
+ const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
9
+
10
+ const fn = compose(html("p"), punctuate("!"), shout)
11
+
12
+ expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
13
+ })
14
+
15
+ test("it should pipe functions", () => {
16
+ const shout = (x) => x.toUpperCase()
17
+ const punctuate = (mark) => (x) => `${x}${mark}`
18
+ const html = (tag) => (x) => `<${tag}>${x}</${tag}>`
19
+
20
+ const fn = pipe(shout, punctuate("!"), html("p"))
21
+
22
+ expect(fn("Hello world")).toBe("<p>HELLO WORLD!</p>")
23
+ })
@@ -0,0 +1,69 @@
1
+ /**
2
+ * @typedef {import("./types").Point} Point
3
+ * @typedef {import("./types").Circle} Circle
4
+ * @typedef {import("./types").Rectangle} Rectangle
5
+ * @typedef {import("./types").Platform} Platform
6
+ */
7
+
8
+ import { clamp } from "../linear-algebra/vector.js"
9
+ import { subtract } from "../linear-algebra/vectors.js"
10
+ import { intersectsCircle as pointIntersectsCircle } from "./point.js"
11
+ import { hypothenuse } from "./triangle.js"
12
+
13
+ const HALF = 2
14
+ const SQUARED = 2
15
+ const INITIAL_SUM = 0
16
+
17
+ /**
18
+ * Checks if a circle intersects with a point.
19
+ * @param {Circle} circle - The circle to check.
20
+ * @param {Point} point - The point to check.
21
+ * @returns {boolean} True if the point intersects the circle, false otherwise.
22
+ */
23
+ export function intersectsPoint(circle, point) {
24
+ return pointIntersectsCircle(point, circle)
25
+ }
26
+
27
+ /**
28
+ * Checks if two circles intersect.
29
+ * @param {Circle} circle1 - The first circle.
30
+ * @param {Circle} circle2 - The second circle.
31
+ * @returns {boolean} True if the circles intersect, false otherwise.
32
+ */
33
+ export function intersectsCircle(circle1, circle2) {
34
+ return (
35
+ hypothenuse(...subtract(circle1.position, circle2.position)) <=
36
+ circle1.radius + circle2.radius
37
+ )
38
+ }
39
+
40
+ /**
41
+ * Checks if a circle intersects with a rectangle.
42
+ * @param {Circle} circle - The circle to check.
43
+ * @param {Rectangle} rectangle - The rectangle to check.
44
+ * @returns {boolean} True if the circle intersects the rectangle, false otherwise.
45
+ */
46
+ export function intersectsRectangle(circle, rectangle) {
47
+ const [rectX, rectY, rectZ] = rectangle.position
48
+ const [width, height, depth] = rectangle.size
49
+
50
+ const left = rectX - width / HALF
51
+ const right = rectX + width / HALF
52
+ const bottom = rectY - height / HALF
53
+ const top = rectY + height / HALF
54
+ const back = rectZ - depth / HALF
55
+ const front = rectZ + depth / HALF
56
+
57
+ const closestPoint = clamp(
58
+ circle.position,
59
+ [left, bottom, back],
60
+ [right, top, front],
61
+ )
62
+
63
+ const distanceSquared = subtract(circle.position, closestPoint).reduce(
64
+ (sum, value) => sum + value ** SQUARED,
65
+ INITIAL_SUM,
66
+ )
67
+
68
+ return distanceSquared <= circle.radius ** SQUARED
69
+ }
@@ -0,0 +1,97 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import {
4
+ intersectsCircle,
5
+ intersectsPoint,
6
+ intersectsRectangle,
7
+ } from "./circle.js"
8
+
9
+ test("it should prove that a circle around a point intersects with it", () => {
10
+ const circle = {
11
+ position: [1, 1, 0],
12
+ radius: 1,
13
+ }
14
+ const point = [1.5, 1.5, 0]
15
+
16
+ expect(intersectsPoint(circle, point)).toBe(true)
17
+ })
18
+
19
+ test("it should prove that two equal circles intersect", () => {
20
+ const circle1 = { position: [1, 1, 0], radius: 1 }
21
+ const circle2 = { position: [1, 1, 0], radius: 1 }
22
+
23
+ expect(intersectsCircle(circle1, circle2)).toBe(true)
24
+ })
25
+
26
+ test("it should prove that two circles shifted by a small x intersect", () => {
27
+ const circle1 = { position: [1, 1, 0], radius: 1 }
28
+ const circle2 = { position: [2, 1, 0], radius: 1 }
29
+
30
+ expect(intersectsCircle(circle1, circle2)).toBe(true)
31
+ })
32
+
33
+ test("it should prove that two touching circles intersect", () => {
34
+ const circle1 = { position: [1, 1, 0], radius: 1 }
35
+ const circle2 = { position: [3, 1, 0], radius: 1 }
36
+
37
+ expect(intersectsCircle(circle1, circle2)).toBe(true)
38
+ })
39
+
40
+ test("it should prove that two non-touching circles do not intersect", () => {
41
+ const circle1 = { position: [1, 1, 0], radius: 1 }
42
+ const circle2 = { position: [4, 1, 0], radius: 1 }
43
+
44
+ expect(intersectsCircle(circle1, circle2)).toBe(false)
45
+ })
46
+
47
+ test("it should prove that a circle inside of a rectangle intersects with it", () => {
48
+ const circle = {
49
+ position: [1, 1, 0],
50
+ radius: 1,
51
+ }
52
+ const rectangle = {
53
+ position: [-1, -1, 0],
54
+ size: [4, 4, 0],
55
+ }
56
+
57
+ expect(intersectsRectangle(circle, rectangle)).toBe(true)
58
+ })
59
+
60
+ test("it should prove that a circle touching the border of a rectangle intersects with it", () => {
61
+ const circle = {
62
+ position: [1, 1, 0],
63
+ radius: 1,
64
+ }
65
+ const rectangle = {
66
+ position: [-1, 1, 0], // Rectangle's right edge is at x=0, touching the circle's left edge.
67
+ size: [2, 2, 0],
68
+ }
69
+
70
+ expect(intersectsRectangle(circle, rectangle)).toBe(true)
71
+ })
72
+
73
+ test("it should prove that a circle crossing a rectangle intersects with it", () => {
74
+ const circle = {
75
+ position: [1, 1, 0],
76
+ radius: 1,
77
+ }
78
+ const rectangle = {
79
+ position: [0, 0, 0],
80
+ size: [2, 2, 0],
81
+ }
82
+
83
+ expect(intersectsRectangle(circle, rectangle)).toBe(true)
84
+ })
85
+
86
+ test("it should prove that a circle outside of a rectangle does not intersect with it", () => {
87
+ const circle = {
88
+ position: [1, 1, 0],
89
+ radius: 1,
90
+ }
91
+ const rectangle = {
92
+ position: [-3, 0, 0],
93
+ size: [2, 2, 0],
94
+ }
95
+
96
+ expect(intersectsRectangle(circle, rectangle)).toBe(false)
97
+ })
@@ -0,0 +1,70 @@
1
+ // @see https://jonathanwhiting.com/tutorial/collision/
2
+
3
+ import { intersectsRectangle } from "./rectangle.js"
4
+
5
+ const FIRST_TILE = 0
6
+ const HALF = 2
7
+ const LAST_ROW = 1
8
+
9
+ export function findCollisions(hitmask, target) {
10
+ const [tilemapX, tilemapY, tilemapZ] = hitmask.position
11
+ const [tileWidth, tileDepth] = hitmask.tileSize
12
+ const halfTileWidth = tileWidth / HALF
13
+ const halfTileDepth = tileDepth / HALF
14
+
15
+ const dRows = Math.ceil(hitmask.heights.length / hitmask.columns)
16
+ const tilemapWidth = hitmask.columns * tileWidth
17
+ const tilemapDepth = dRows * tileDepth
18
+
19
+ const tilemapLeft = tilemapX - tilemapWidth / HALF
20
+ const tilemapBack = tilemapZ - tilemapDepth / HALF
21
+
22
+ const [targetX, , targetZ] = target.position
23
+ const [targetWidth, , targetDepth] = target.size
24
+ const targetHalfWidth = targetWidth / HALF
25
+ const targetHalfDepth = targetDepth / HALF
26
+
27
+ const targetLeft = targetX - targetHalfWidth
28
+ const targetRight = targetX + targetHalfWidth
29
+ const targetBack = targetZ - targetHalfDepth
30
+ const targetFront = targetZ + targetHalfDepth
31
+
32
+ const minTileX = Math.floor((targetLeft - tilemapLeft) / tileWidth)
33
+ const maxTileX = Math.floor((targetRight - tilemapLeft) / tileWidth)
34
+ const minTileZ = Math.floor((targetBack - tilemapBack) / tileDepth)
35
+ const maxTileZ = Math.floor((targetFront - tilemapBack) / tileDepth)
36
+
37
+ for (let i = minTileX; i <= maxTileX; i++) {
38
+ for (let j = minTileZ; j <= maxTileZ; j++) {
39
+ if (
40
+ i < FIRST_TILE ||
41
+ i >= hitmask.columns ||
42
+ j < FIRST_TILE ||
43
+ j >= dRows
44
+ ) {
45
+ continue
46
+ }
47
+
48
+ const invertedRow = dRows - LAST_ROW - j
49
+ const tileIndex = invertedRow * hitmask.columns + i
50
+ const tileHeightValue = hitmask.heights[tileIndex]
51
+
52
+ if (tileHeightValue) {
53
+ const tileRectangle = {
54
+ position: [
55
+ tilemapLeft + i * tileWidth + halfTileWidth,
56
+ tilemapY,
57
+ tilemapBack + j * tileDepth + halfTileDepth,
58
+ ],
59
+ size: [tileWidth, tileHeightValue, tileDepth],
60
+ }
61
+
62
+ if (intersectsRectangle(target, tileRectangle)) {
63
+ return true
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ return false
70
+ }
@@ -0,0 +1,155 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { findCollisions } from "./hitmask"
4
+
5
+ /**
6
+ * Given a rectangle located at (x=1.5, y=0, z=2.5) with a size of (1, 1, 1),
7
+ * which is touching a wall tile, this test proves a collision is detected.
8
+ *
9
+ * Tilemap layout:
10
+ * ⬛️🟩🟩⬛️
11
+ * ⬛️⬛️⬛️⬛️
12
+ * ⬛️⬛️⬛️⬛️
13
+ *
14
+ * Rectangle position (X,Z): R at (1.5, 2.5)
15
+ *
16
+ * Expected outcome: true
17
+ */
18
+ test("it should prove that a rectangle touching a wall intersects with it", () => {
19
+ const hitmask = {
20
+ position: [2, 0, 1.5],
21
+ tileSize: [1, 1],
22
+ columns: 4,
23
+ // The top row (indices 0, 1, 2, 3) now has the walls.
24
+ heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
25
+ }
26
+ const rectangle = {
27
+ shape: "rectangle",
28
+ position: [1.5, 0, 2.5],
29
+ size: [1, 1, 1],
30
+ }
31
+
32
+ expect(findCollisions(hitmask, rectangle)).toBe(true)
33
+ })
34
+
35
+ /**
36
+ * Given the same scenario as the previous test, but all coordinates
37
+ * are shifted by a constant value, this test proves the outcome remains
38
+ * unchanged, ensuring the collision detection is relative.
39
+ *
40
+ * Tilemap layout:
41
+ * ⬛️🟩🟩⬛️
42
+ * ⬛️⬛️⬛️⬛️
43
+ * ⬛️⬛️⬛️⬛️
44
+ *
45
+ * Rectangle position (X,Z): R at (11.5, 12.5)
46
+ *
47
+ * Expected outcome: true
48
+ */
49
+ test("it should prove that shifting operands does not change the outcome", () => {
50
+ const SHIFT = 10
51
+ const hitmask = {
52
+ position: [2 + SHIFT, 0, 1.5 + SHIFT],
53
+ tileSize: [1, 1],
54
+ columns: 4,
55
+ // The top row (indices 0, 1, 2, 3) now has the walls.
56
+ heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
57
+ }
58
+ const rectangle = {
59
+ shape: "rectangle",
60
+ position: [11.5, 0, 12.5],
61
+ size: [1, 1, 1],
62
+ }
63
+
64
+ expect(findCollisions(hitmask, rectangle)).toBe(true)
65
+ })
66
+
67
+ /**
68
+ * Given a rectangle located at (x=0.5, y=0, z=0.5) with a size of (1, 1, 1),
69
+ * which is not touching any walls, this test proves no collision is detected.
70
+ *
71
+ * Tilemap layout:
72
+ * ⬛️🟩🟩⬛️
73
+ * ⬛️⬛️⬛️⬛️
74
+ * ⬛️⬛️⬛️⬛️
75
+ *
76
+ * Rectangle position (X,Z): R at (0.5, 0.5)
77
+ *
78
+ * Expected outcome: false
79
+ */
80
+ test("it should prove that a rectangle not touching a wall does not intersect with it", () => {
81
+ const hitmask = {
82
+ position: [2, 0, 1.5],
83
+ tileSize: [1, 1],
84
+ columns: 4,
85
+ // The top row (indices 0, 1, 2, 3) now has the walls.
86
+ heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
87
+ }
88
+ const rectangle = {
89
+ shape: "rectangle",
90
+ position: [0.5, 0, 0.5],
91
+ size: [1, 1, 1],
92
+ }
93
+
94
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
95
+ })
96
+
97
+ /**
98
+ * Given a rectangle located above the tile, this test proves no
99
+ * collision is detected even when the X and Z positions overlap.
100
+ *
101
+ * Tilemap layout:
102
+ * ⬛️🟩🟩⬛️
103
+ * ⬛️⬛️⬛️⬛️
104
+ * ⬛️⬛️⬛️⬛️
105
+ *
106
+ * Rectangle position: R at (x=1.5, y=1.6, z=2.5)
107
+ *
108
+ * Expected outcome: false
109
+ */
110
+ test("it should prove that a rectangle above the tile of a hitmask does not intersect with it", () => {
111
+ const hitmask = {
112
+ position: [2, 0, 1.5],
113
+ tileSize: [1, 1],
114
+ columns: 4,
115
+ // The top row (indices 0, 1, 2, 3) now has the walls.
116
+ heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
117
+ }
118
+ const rectangle = {
119
+ shape: "rectangle",
120
+ position: [1.5, 1.6, 2.5],
121
+ size: [1, 1, 1],
122
+ }
123
+
124
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
125
+ })
126
+
127
+ /**
128
+ * Given a rectangle located outside of the tilemap, this test proves
129
+ * no collision is detected.
130
+ *
131
+ * Tilemap layout:
132
+ * ⬛️🟩🟩⬛️
133
+ * ⬛️⬛️⬛️⬛️
134
+ * ⬛️⬛️⬛️⬛️
135
+ *
136
+ * Rectangle position: R at (x=5.5, y=0.5, z=1.5)
137
+ *
138
+ * Expected outcome: false
139
+ */
140
+ test("it should prove that a rectangle outside of a hitmask does not intersect with it", () => {
141
+ const hitmask = {
142
+ position: [2, 0, 1.5],
143
+ tileSize: [1, 1],
144
+ columns: 4,
145
+ // The top row (indices 0, 1, 2, 3) now has the walls.
146
+ heights: [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
147
+ }
148
+ const rectangle = {
149
+ shape: "rectangle",
150
+ position: [5.5, 0.5, 1.5],
151
+ size: [1, 1, 1],
152
+ }
153
+
154
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
155
+ })
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @typedef {import("./types").Line} Line
3
+ * @typedef {import("./types").Point} Point
4
+ * @typedef {import("./types").Circle} Circle
5
+ */
6
+
7
+ import { abs } from "../numbers.js"
8
+ import { hypothenuse } from "./triangle.js"
9
+
10
+ /**
11
+ * Calculates the shortest distance from a point to a line in 2D space.
12
+ *
13
+ * @param {Line} line - The line represented by the equation ax + bz + c = 0, where `line` is [a, b, c].
14
+ * @param {Point} point - The point in 3D space represented as [x, y, z].
15
+ * @returns {number} The shortest distance from the point to the line.
16
+ */
17
+ export function distanceFromPoint(line, point) {
18
+ const [a, b, c] = line
19
+ const [x, , z] = point
20
+
21
+ return abs(a * x + b * z + c) / hypothenuse(a, b)
22
+ }
23
+
24
+ // @see https://math.stackexchange.com/questions/275529/check-if-line-intersects-with-circles-perimeter
25
+ /**
26
+ * Determines whether a line intersects with the perimeter of a circle.
27
+ *
28
+ * @param {Line} line - The line represented by the equation ax + bz + c = 0, where `line` is [a, b, c].
29
+ * @param {Circle} circle - The circle defined by its position (center) and radius.
30
+ * @returns {boolean} `true` if the line intersects the circle's perimeter, otherwise `false`.
31
+ */
32
+ export function intersectsCircle(line, circle) {
33
+ return distanceFromPoint(line, circle.position) <= circle.radius
34
+ }