@inglorious/engine 0.1.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 (256) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +72 -0
  3. package/package.json +76 -0
  4. package/src/docs/ai/movement/dynamic/align.js +131 -0
  5. package/src/docs/ai/movement/dynamic/arrive.js +88 -0
  6. package/src/docs/ai/movement/dynamic/dynamic.mdx +99 -0
  7. package/src/docs/ai/movement/dynamic/dynamic.stories.js +58 -0
  8. package/src/docs/ai/movement/dynamic/evade.js +72 -0
  9. package/src/docs/ai/movement/dynamic/face.js +90 -0
  10. package/src/docs/ai/movement/dynamic/flee.js +38 -0
  11. package/src/docs/ai/movement/dynamic/look-where-youre-going.js +114 -0
  12. package/src/docs/ai/movement/dynamic/match-velocity.js +92 -0
  13. package/src/docs/ai/movement/dynamic/pursue.js +72 -0
  14. package/src/docs/ai/movement/dynamic/seek.js +37 -0
  15. package/src/docs/ai/movement/dynamic/wander.js +71 -0
  16. package/src/docs/ai/movement/kinematic/align.js +122 -0
  17. package/src/docs/ai/movement/kinematic/arrive.js +78 -0
  18. package/src/docs/ai/movement/kinematic/face.js +82 -0
  19. package/src/docs/ai/movement/kinematic/flee.js +36 -0
  20. package/src/docs/ai/movement/kinematic/kinematic.mdx +67 -0
  21. package/src/docs/ai/movement/kinematic/kinematic.stories.js +42 -0
  22. package/src/docs/ai/movement/kinematic/seek.js +34 -0
  23. package/src/docs/ai/movement/kinematic/wander-as-seek.js +62 -0
  24. package/src/docs/ai/movement/kinematic/wander.js +28 -0
  25. package/src/docs/bounds.js +7 -0
  26. package/src/docs/code-reuse.js +35 -0
  27. package/src/docs/collision/circles.js +58 -0
  28. package/src/docs/collision/collision.mdx +27 -0
  29. package/src/docs/collision/collision.stories.js +22 -0
  30. package/src/docs/collision/platform.js +76 -0
  31. package/src/docs/collision/tilemap.js +181 -0
  32. package/src/docs/empty.js +1 -0
  33. package/src/docs/engine.mdx +81 -0
  34. package/src/docs/engine.stories.js +37 -0
  35. package/src/docs/event-handlers.js +68 -0
  36. package/src/docs/framerate.js +37 -0
  37. package/src/docs/game.jsx +15 -0
  38. package/src/docs/image/image.js +19 -0
  39. package/src/docs/image/image.stories.js +22 -0
  40. package/src/docs/image/sprite.js +39 -0
  41. package/src/docs/image/tilemap.js +84 -0
  42. package/src/docs/input/controls.js +67 -0
  43. package/src/docs/input/gamepad.js +67 -0
  44. package/src/docs/input/input.mdx +55 -0
  45. package/src/docs/input/input.stories.js +27 -0
  46. package/src/docs/input/keyboard.js +58 -0
  47. package/src/docs/input/mouse.js +32 -0
  48. package/src/docs/instances.js +49 -0
  49. package/src/docs/player/dynamic/double-jump.js +90 -0
  50. package/src/docs/player/dynamic/dynamic.stories.js +32 -0
  51. package/src/docs/player/dynamic/jump.js +83 -0
  52. package/src/docs/player/dynamic/modern-controls.js +57 -0
  53. package/src/docs/player/dynamic/shooter-controls.js +51 -0
  54. package/src/docs/player/dynamic/tank-controls.js +44 -0
  55. package/src/docs/player/kinematic/double-jump.js +90 -0
  56. package/src/docs/player/kinematic/jump.js +82 -0
  57. package/src/docs/player/kinematic/kinematic.stories.js +32 -0
  58. package/src/docs/player/kinematic/modern-controls.js +56 -0
  59. package/src/docs/player/kinematic/shooter-controls.js +48 -0
  60. package/src/docs/player/kinematic/tank-controls.js +42 -0
  61. package/src/docs/quick-start/first-game.js +49 -0
  62. package/src/docs/quick-start/hello-world.js +1 -0
  63. package/src/docs/quick-start.mdx +127 -0
  64. package/src/docs/quick-start.stories.js +17 -0
  65. package/src/docs/recipes/add-and-remove.js +71 -0
  66. package/src/docs/recipes/add-instance.js +42 -0
  67. package/src/docs/recipes/decision-tree.js +169 -0
  68. package/src/docs/recipes/random-instances.js +25 -0
  69. package/src/docs/recipes/recipes.mdx +81 -0
  70. package/src/docs/recipes/recipes.stories.js +37 -0
  71. package/src/docs/recipes/remove-instance.js +52 -0
  72. package/src/docs/recipes/states.js +64 -0
  73. package/src/docs/ui/button.js +28 -0
  74. package/src/docs/ui/form.stories.js +55 -0
  75. package/src/docs/ui-chooser.jsx +6 -0
  76. package/src/docs/utils/data-structures/object.mdx +47 -0
  77. package/src/docs/utils/data-structures/objects.mdx +30 -0
  78. package/src/docs/utils/functions/functions.mdx +34 -0
  79. package/src/docs/utils/math/geometry/circle.mdx +55 -0
  80. package/src/docs/utils/math/geometry/point.mdx +38 -0
  81. package/src/docs/utils/math/geometry/rectangle.mdx +24 -0
  82. package/src/docs/utils/math/geometry/segment.mdx +55 -0
  83. package/src/docs/utils/math/geometry/triangle.mdx +22 -0
  84. package/src/docs/utils/math/linear-algebra/2d.mdx +22 -0
  85. package/src/docs/utils/math/linear-algebra/quaternion.mdx +21 -0
  86. package/src/docs/utils/math/linear-algebra/quaternions.mdx +22 -0
  87. package/src/docs/utils/math/linear-algebra/vector.mdx +177 -0
  88. package/src/docs/utils/math/linear-algebra/vectors.mdx +58 -0
  89. package/src/docs/utils/math/numbers.mdx +76 -0
  90. package/src/docs/utils/math/random.mdx +35 -0
  91. package/src/docs/utils/math/statistics.mdx +38 -0
  92. package/src/docs/utils/math/trigonometry.mdx +85 -0
  93. package/src/docs/utils/physics/friction.mdx +20 -0
  94. package/src/docs/utils/physics/gravity.mdx +28 -0
  95. package/src/engine/ai/movement/dynamic/align.js +63 -0
  96. package/src/engine/ai/movement/dynamic/arrive.js +43 -0
  97. package/src/engine/ai/movement/dynamic/evade.js +38 -0
  98. package/src/engine/ai/movement/dynamic/face.js +20 -0
  99. package/src/engine/ai/movement/dynamic/flee.js +45 -0
  100. package/src/engine/ai/movement/dynamic/look-where-youre-going.js +17 -0
  101. package/src/engine/ai/movement/dynamic/match-velocity.js +50 -0
  102. package/src/engine/ai/movement/dynamic/pursue.js +38 -0
  103. package/src/engine/ai/movement/dynamic/seek.js +44 -0
  104. package/src/engine/ai/movement/dynamic/wander.js +32 -0
  105. package/src/engine/ai/movement/kinematic/align.js +37 -0
  106. package/src/engine/ai/movement/kinematic/arrive.js +42 -0
  107. package/src/engine/ai/movement/kinematic/face.js +20 -0
  108. package/src/engine/ai/movement/kinematic/flee.js +26 -0
  109. package/src/engine/ai/movement/kinematic/seek.js +26 -0
  110. package/src/engine/ai/movement/kinematic/seek.test.js +42 -0
  111. package/src/engine/ai/movement/kinematic/wander-as-seek.js +31 -0
  112. package/src/engine/ai/movement/kinematic/wander.js +27 -0
  113. package/src/engine/collision/detection.js +115 -0
  114. package/src/engine/loop/animation-frame.js +26 -0
  115. package/src/engine/loop/elapsed.js +23 -0
  116. package/src/engine/loop/fixed.js +28 -0
  117. package/src/engine/loop/flash.js +14 -0
  118. package/src/engine/loop/lag.js +27 -0
  119. package/src/engine/loop.js +15 -0
  120. package/src/engine/movement/dynamic/modern.js +24 -0
  121. package/src/engine/movement/dynamic/tank.js +43 -0
  122. package/src/engine/movement/kinematic/modern.js +16 -0
  123. package/src/engine/movement/kinematic/modern.test.js +27 -0
  124. package/src/engine/movement/kinematic/tank.js +27 -0
  125. package/src/engine/store.js +174 -0
  126. package/src/engine/store.test.js +256 -0
  127. package/src/engine.js +74 -0
  128. package/src/game/animation.js +26 -0
  129. package/src/game/bounds.js +66 -0
  130. package/src/game/decorators/character.js +5 -0
  131. package/src/game/decorators/clamp-to-bounds.js +15 -0
  132. package/src/game/decorators/collisions.js +24 -0
  133. package/src/game/decorators/controls/dynamic/modern.js +48 -0
  134. package/src/game/decorators/controls/dynamic/shooter.js +47 -0
  135. package/src/game/decorators/controls/dynamic/tank.js +55 -0
  136. package/src/game/decorators/controls/kinematic/modern.js +49 -0
  137. package/src/game/decorators/controls/kinematic/shooter.js +45 -0
  138. package/src/game/decorators/controls/kinematic/tank.js +52 -0
  139. package/src/game/decorators/debug/collisions.js +32 -0
  140. package/src/game/decorators/double-jump.js +70 -0
  141. package/src/game/decorators/fps.js +30 -0
  142. package/src/game/decorators/fsm.js +27 -0
  143. package/src/game/decorators/fsm.test.js +56 -0
  144. package/src/game/decorators/game.js +11 -0
  145. package/src/game/decorators/image/image.js +5 -0
  146. package/src/game/decorators/image/sprite.js +5 -0
  147. package/src/game/decorators/image/tilemap.js +5 -0
  148. package/src/game/decorators/input/controls.js +27 -0
  149. package/src/game/decorators/input/gamepad.js +74 -0
  150. package/src/game/decorators/input/input.js +41 -0
  151. package/src/game/decorators/input/keyboard.js +49 -0
  152. package/src/game/decorators/input/mouse.js +65 -0
  153. package/src/game/decorators/jump.js +72 -0
  154. package/src/game/decorators/platform.js +5 -0
  155. package/src/game/decorators/ui/button.js +21 -0
  156. package/src/game/sprite.js +119 -0
  157. package/src/main.js +5 -0
  158. package/src/ui/canvas/absolute-position.js +17 -0
  159. package/src/ui/canvas/character.js +35 -0
  160. package/src/ui/canvas/form/button.js +25 -0
  161. package/src/ui/canvas/fps.js +18 -0
  162. package/src/ui/canvas/image/hitmask.js +37 -0
  163. package/src/ui/canvas/image/image.js +37 -0
  164. package/src/ui/canvas/image/sprite.js +49 -0
  165. package/src/ui/canvas/image/tilemap.js +64 -0
  166. package/src/ui/canvas/mouse.js +37 -0
  167. package/src/ui/canvas/shapes/circle.js +31 -0
  168. package/src/ui/canvas/shapes/rectangle.js +31 -0
  169. package/src/ui/canvas.js +81 -0
  170. package/src/ui/react/game/character/character.module.scss +17 -0
  171. package/src/ui/react/game/character/index.jsx +30 -0
  172. package/src/ui/react/game/cursor/cursor.module.scss +47 -0
  173. package/src/ui/react/game/cursor/index.jsx +20 -0
  174. package/src/ui/react/game/form/fields/field/field.module.scss +5 -0
  175. package/src/ui/react/game/form/fields/field/index.jsx +56 -0
  176. package/src/ui/react/game/form/fields/fields.module.scss +48 -0
  177. package/src/ui/react/game/form/fields/index.jsx +12 -0
  178. package/src/ui/react/game/form/form.module.scss +18 -0
  179. package/src/ui/react/game/form/index.jsx +22 -0
  180. package/src/ui/react/game/fps/index.jsx +16 -0
  181. package/src/ui/react/game/game.jsx +71 -0
  182. package/src/ui/react/game/index.jsx +29 -0
  183. package/src/ui/react/game/platform/index.jsx +30 -0
  184. package/src/ui/react/game/platform/platform.module.scss +7 -0
  185. package/src/ui/react/game/scene/index.jsx +25 -0
  186. package/src/ui/react/game/scene/scene.module.scss +9 -0
  187. package/src/ui/react/game/sprite/index.jsx +58 -0
  188. package/src/ui/react/game/sprite/sprite.module.css +3 -0
  189. package/src/ui/react/game/stats/index.jsx +22 -0
  190. package/src/ui/react/hocs/with-absolute-position/index.jsx +20 -0
  191. package/src/ui/react/hocs/with-absolute-position/with-absolute-position.module.scss +5 -0
  192. package/src/ui/react/index.jsx +9 -0
  193. package/src/utils/algorithms/decision-tree.js +24 -0
  194. package/src/utils/algorithms/decision-tree.test.js +102 -0
  195. package/src/utils/algorithms/path-finding.js +155 -0
  196. package/src/utils/algorithms/path-finding.test.js +151 -0
  197. package/src/utils/algorithms/types.d.ts +28 -0
  198. package/src/utils/data-structures/array.js +83 -0
  199. package/src/utils/data-structures/array.test.js +173 -0
  200. package/src/utils/data-structures/board.js +159 -0
  201. package/src/utils/data-structures/board.test.js +242 -0
  202. package/src/utils/data-structures/boolean.js +9 -0
  203. package/src/utils/data-structures/heap.js +164 -0
  204. package/src/utils/data-structures/heap.test.js +103 -0
  205. package/src/utils/data-structures/object.js +102 -0
  206. package/src/utils/data-structures/object.test.js +121 -0
  207. package/src/utils/data-structures/objects.js +48 -0
  208. package/src/utils/data-structures/objects.test.js +99 -0
  209. package/src/utils/data-structures/tree.js +36 -0
  210. package/src/utils/data-structures/tree.test.js +33 -0
  211. package/src/utils/data-structures/types.d.ts +4 -0
  212. package/src/utils/functions/functions.js +19 -0
  213. package/src/utils/functions/functions.test.js +23 -0
  214. package/src/utils/math/geometry/circle.js +117 -0
  215. package/src/utils/math/geometry/circle.test.js +97 -0
  216. package/src/utils/math/geometry/hitmask.js +39 -0
  217. package/src/utils/math/geometry/hitmask.test.js +84 -0
  218. package/src/utils/math/geometry/line.js +35 -0
  219. package/src/utils/math/geometry/line.test.js +49 -0
  220. package/src/utils/math/geometry/platform.js +42 -0
  221. package/src/utils/math/geometry/platform.test.js +133 -0
  222. package/src/utils/math/geometry/point.js +71 -0
  223. package/src/utils/math/geometry/point.test.js +81 -0
  224. package/src/utils/math/geometry/rectangle.js +45 -0
  225. package/src/utils/math/geometry/rectangle.test.js +42 -0
  226. package/src/utils/math/geometry/segment.js +80 -0
  227. package/src/utils/math/geometry/segment.test.js +183 -0
  228. package/src/utils/math/geometry/triangle.js +15 -0
  229. package/src/utils/math/geometry/triangle.test.js +11 -0
  230. package/src/utils/math/geometry/types.d.ts +23 -0
  231. package/src/utils/math/linear-algebra/2d.js +28 -0
  232. package/src/utils/math/linear-algebra/2d.test.js +17 -0
  233. package/src/utils/math/linear-algebra/quaternion.js +22 -0
  234. package/src/utils/math/linear-algebra/quaternion.test.js +25 -0
  235. package/src/utils/math/linear-algebra/quaternions.js +20 -0
  236. package/src/utils/math/linear-algebra/quaternions.test.js +29 -0
  237. package/src/utils/math/linear-algebra/types.d.ts +4 -0
  238. package/src/utils/math/linear-algebra/vector.js +302 -0
  239. package/src/utils/math/linear-algebra/vector.test.js +257 -0
  240. package/src/utils/math/linear-algebra/vectors.js +122 -0
  241. package/src/utils/math/linear-algebra/vectors.test.js +65 -0
  242. package/src/utils/math/numbers.js +90 -0
  243. package/src/utils/math/numbers.test.js +137 -0
  244. package/src/utils/math/rng.js +44 -0
  245. package/src/utils/math/rng.test.js +39 -0
  246. package/src/utils/math/statistics.js +43 -0
  247. package/src/utils/math/statistics.test.js +47 -0
  248. package/src/utils/math/trigonometry.js +89 -0
  249. package/src/utils/math/trigonometry.test.js +52 -0
  250. package/src/utils/physics/acceleration.js +63 -0
  251. package/src/utils/physics/friction.js +30 -0
  252. package/src/utils/physics/friction.test.js +44 -0
  253. package/src/utils/physics/gravity.js +71 -0
  254. package/src/utils/physics/gravity.test.js +80 -0
  255. package/src/utils/physics/jump.js +41 -0
  256. package/src/utils/physics/velocity.js +38 -0
@@ -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,117 @@
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 { subtract } from "@inglorious/utils/math/linear-algebra/vectors.js"
9
+
10
+ import { clamp } from "../numbers.js"
11
+ import { intersectsCircle as platformIntersectsCircle } from "./platform.js"
12
+ import {
13
+ intersectsCircle as pointIntersectsCircle,
14
+ intersectsRectangle as pointIntersectsRectangle,
15
+ } from "./point.js"
16
+ import { intersectsCircle as segmentIntersectsCircle } from "./segment.js"
17
+ import { hypothenuse } from "./triangle.js"
18
+
19
+ const SQUARED = 2
20
+ const INITIAL_SUM = 0
21
+
22
+ /**
23
+ * Checks if a circle intersects with a point.
24
+ * @param {Circle} circle - The circle to check.
25
+ * @param {Point} point - The point to check.
26
+ * @returns {boolean} True if the point intersects the circle, false otherwise.
27
+ */
28
+ export function intersectsPoint(circle, point) {
29
+ return pointIntersectsCircle(point, circle)
30
+ }
31
+
32
+ /**
33
+ * Checks if two circles intersect.
34
+ * @param {Circle} circle1 - The first circle.
35
+ * @param {Circle} circle2 - The second circle.
36
+ * @returns {boolean} True if the circles intersect, false otherwise.
37
+ */
38
+ export function intersectsCircle(circle1, circle2) {
39
+ return (
40
+ hypothenuse(...subtract(circle1.position, circle2.position)) <=
41
+ circle1.radius + circle2.radius
42
+ )
43
+ }
44
+
45
+ /**
46
+ * Checks if a circle intersects with a rectangle.
47
+ * @param {Circle} circle - The circle to check.
48
+ * @param {Rectangle} rectangle - The rectangle to check.
49
+ * @returns {boolean} True if the circle intersects the rectangle, false otherwise.
50
+ */
51
+ export function intersectsRectangle(circle, rectangle) {
52
+ const [left, top, front] = rectangle.position
53
+ const [width, height, depth] = rectangle.size
54
+
55
+ const ltf = [left, top, front]
56
+ const rtf = [left + width, top, front]
57
+ const lbf = [left, top, front + depth]
58
+ const rbf = [left + width, top, front + depth]
59
+
60
+ const ltb = [left, top + height, front]
61
+ const rtb = [left + width, top + height, front]
62
+ const lbb = [left, top + height, front + depth]
63
+ const rbb = [left + width, top + height, front + depth]
64
+
65
+ return (
66
+ // Center
67
+ pointIntersectsRectangle(circle.position, rectangle) ||
68
+ // Front face
69
+ segmentIntersectsCircle({ from: ltf, to: rtf }, circle) ||
70
+ segmentIntersectsCircle({ from: rtf, to: rbf }, circle) ||
71
+ segmentIntersectsCircle({ from: rbf, to: lbf }, circle) ||
72
+ segmentIntersectsCircle({ from: lbf, to: ltf }, circle) ||
73
+ // Back face
74
+ segmentIntersectsCircle({ from: ltb, to: rtb }, circle) ||
75
+ segmentIntersectsCircle({ from: rtb, to: rbb }, circle) ||
76
+ segmentIntersectsCircle({ from: rbb, to: lbb }, circle) ||
77
+ segmentIntersectsCircle({ from: lbb, to: ltb }, circle) ||
78
+ // Connecting edges
79
+ segmentIntersectsCircle({ from: ltf, to: ltb }, circle) ||
80
+ segmentIntersectsCircle({ from: rtf, to: rtb }, circle) ||
81
+ segmentIntersectsCircle({ from: lbf, to: lbb }, circle) ||
82
+ segmentIntersectsCircle({ from: rbf, to: rbb }, circle) ||
83
+ // Corners
84
+ isCircleWithinRectangleRadius(circle, rectangle)
85
+ )
86
+ }
87
+
88
+ /**
89
+ * Checks if a circle intersects with a platform.
90
+ * @param {Circle} circle - The circle to check.
91
+ * @param {Platform} platform - The platform to check.
92
+ * @returns {boolean} True if the circle intersects the platform, false otherwise.
93
+ */
94
+ export function intersectsPlatform(circle, platform) {
95
+ return platformIntersectsCircle(platform, circle)
96
+ }
97
+
98
+ function isCircleWithinRectangleRadius(circle, rectangle) {
99
+ const [left, top, front] = rectangle.position
100
+ const [width, height, depth] = rectangle.size
101
+
102
+ // Find the closest point on the rectangle to the circle's center
103
+ const [x, y, z] = circle.position
104
+ const closestPoint = [
105
+ clamp(x, left, left + width),
106
+ clamp(y, top, top + height),
107
+ clamp(z, front, front + depth),
108
+ ]
109
+
110
+ // Calculate the distance from the circle's center to the closest point
111
+ const distanceSquared = subtract(circle.position, closestPoint).reduce(
112
+ (sum, value) => sum + value ** SQUARED,
113
+ INITIAL_SUM,
114
+ )
115
+
116
+ return distanceSquared <= circle.radius ** SQUARED
117
+ }
@@ -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 on 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: [-2, 0, 0],
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: [-1, -1, 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,39 @@
1
+ // @see https://jonathanwhiting.com/tutorial/collision/
2
+
3
+ const LOWER_BOUND = 0
4
+ const TOP_TILE_OFFSET = 1 // Offset to include the top boundary tile
5
+
6
+ export function findCollisions(hitmask, target) {
7
+ const { position, tileSize, columns, heights } = hitmask
8
+ const [left, top, front] = position
9
+ const [tileWidth, tileHeight] = tileSize
10
+ const rows = heights.length / columns
11
+
12
+ const [x, y, z] = target.position
13
+ const [width, height, depth] = target.size
14
+
15
+ const leftTile = Math.floor((x - left) / tileWidth)
16
+ const rightTile = Math.floor((x - left + width) / tileWidth)
17
+ const bottomTile = Math.floor((z - front) / tileHeight)
18
+ const topTile = Math.floor((z - front + depth) / tileHeight)
19
+
20
+ if (
21
+ leftTile < LOWER_BOUND ||
22
+ rightTile > columns ||
23
+ bottomTile < LOWER_BOUND ||
24
+ topTile > rows
25
+ ) {
26
+ return false
27
+ }
28
+
29
+ for (let i = leftTile; i < rightTile; i++) {
30
+ for (let j = bottomTile; j <= topTile + TOP_TILE_OFFSET; j++) {
31
+ const heightAtTile = heights[i * columns + j]
32
+ if (y + height <= top + heightAtTile) {
33
+ return true
34
+ }
35
+ }
36
+ }
37
+
38
+ return false
39
+ }
@@ -0,0 +1,84 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { findCollisions } from "./hitmask"
4
+
5
+ test("it should prove that a rectangle touching a wall intersects with it", () => {
6
+ const hitmask = {
7
+ position: [0, 0, 0],
8
+ tileSize: [1, 1],
9
+ columns: 4,
10
+ heights: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
11
+ }
12
+ const rectangle = {
13
+ shape: "rectangle",
14
+ position: [2.5, 0, 1.5],
15
+ size: [1, 1, 1],
16
+ }
17
+
18
+ expect(findCollisions(hitmask, rectangle)).toBe(true)
19
+ })
20
+
21
+ test("it should prove that shifting operands does not change the outcome", () => {
22
+ const SHIFT = 10
23
+ const hitmask = {
24
+ position: [0 + SHIFT, 0, 0 + SHIFT],
25
+ tileSize: [1, 1],
26
+ columns: 4,
27
+ heights: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
28
+ }
29
+ const rectangle = {
30
+ shape: "rectangle",
31
+ position: [2.5 + SHIFT, 0, 1.5 + SHIFT],
32
+ size: [1, 1, 1],
33
+ }
34
+
35
+ expect(findCollisions(hitmask, rectangle)).toBe(true)
36
+ })
37
+
38
+ test("it should prove that a rectangle not touching a wall does not intersect with it", () => {
39
+ const hitmask = {
40
+ position: [0, 0, 0],
41
+ tileSize: [1, 1],
42
+ columns: 4,
43
+ heights: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
44
+ }
45
+ const rectangle = {
46
+ shape: "rectangle",
47
+ position: [0.5, 0, 0.5],
48
+ size: [1, 1, 1],
49
+ }
50
+
51
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
52
+ })
53
+
54
+ test("it should prove that a rectangle above the tile of a hitmask does not intersect with it", () => {
55
+ const hitmask = {
56
+ position: [0, 0, 0],
57
+ tileSize: [1, 1],
58
+ columns: 4,
59
+ heights: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
60
+ }
61
+ const rectangle = {
62
+ shape: "rectangle",
63
+ position: [2.5, 1, 1.5],
64
+ size: [1, 1, 1],
65
+ }
66
+
67
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
68
+ })
69
+
70
+ test("it should prove that a rectangle outside of a hitmask does not intersect with it", () => {
71
+ const hitmask = {
72
+ position: [0, 0, 0],
73
+ tileSize: [1, 1],
74
+ columns: 4,
75
+ heights: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
76
+ }
77
+ const rectangle = {
78
+ shape: "rectangle",
79
+ position: [5, 0, 1],
80
+ size: [1, 1, 1],
81
+ }
82
+
83
+ expect(findCollisions(hitmask, rectangle)).toBe(false)
84
+ })
@@ -0,0 +1,35 @@
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 "@inglorious/utils/math/numbers.js"
8
+
9
+ import { hypothenuse } from "./triangle.js"
10
+
11
+ /**
12
+ * Calculates the shortest distance from a point to a line in 2D space.
13
+ *
14
+ * @param {Line} line - The line represented by the equation ax + bz + c = 0, where `line` is [a, b, c].
15
+ * @param {Point} point - The point in 3D space represented as [x, y, z].
16
+ * @returns {number} The shortest distance from the point to the line.
17
+ */
18
+ export function distanceFromPoint(line, point) {
19
+ const [a, b, c] = line
20
+ const [x, , z] = point
21
+
22
+ return abs(a * x + b * z + c) / hypothenuse(a, b)
23
+ }
24
+
25
+ // @see https://math.stackexchange.com/questions/275529/check-if-line-intersects-with-circles-perimeter
26
+ /**
27
+ * Determines whether a line intersects with the perimeter of a circle.
28
+ *
29
+ * @param {Line} line - The line represented by the equation ax + bz + c = 0, where `line` is [a, b, c].
30
+ * @param {Circle} circle - The circle defined by its position (center) and radius.
31
+ * @returns {boolean} `true` if the line intersects the circle's perimeter, otherwise `false`.
32
+ */
33
+ export function intersectsCircle(line, circle) {
34
+ return distanceFromPoint(line, circle.position) <= circle.radius
35
+ }
@@ -0,0 +1,49 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { distanceFromPoint, intersectsCircle } from "./line.js"
4
+
5
+ test("it should compute the distance between a line and a point", () => {
6
+ const line = [-4, 3, 0]
7
+ const point = [5, 0, 0]
8
+ const expectedResult = 4
9
+
10
+ expect(distanceFromPoint(line, point)).toBe(expectedResult)
11
+ })
12
+
13
+ test("it should not compute the distance between a line and a point", () => {
14
+ const line = [-1, 1, 0]
15
+ const point = [2, 0, 2]
16
+ const expectedResult = 0
17
+
18
+ expect(distanceFromPoint(line, point)).toBe(expectedResult)
19
+ })
20
+
21
+ test("it should prove that a line that crosses a circle intersects with it", () => {
22
+ const line = [-2, 2, 0]
23
+ const circle = {
24
+ position: [1, 1, 0],
25
+ radius: 1,
26
+ }
27
+
28
+ expect(intersectsCircle(line, circle)).toBe(true)
29
+ })
30
+
31
+ test("it should prove that a line tangent to a circle intersects with it", () => {
32
+ const line = [-2, 0, 0]
33
+ const circle = {
34
+ position: [1, 0, 1],
35
+ radius: 1,
36
+ }
37
+
38
+ expect(intersectsCircle(line, circle)).toBe(true)
39
+ })
40
+
41
+ test("it should prove that a line that does not cross a circle does not intersect with it", () => {
42
+ const line = [1, 1, 0]
43
+ const circle = {
44
+ position: [1, 0, 1],
45
+ radius: 1,
46
+ }
47
+
48
+ expect(intersectsCircle(line, circle)).toBe(false)
49
+ })
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Checks if a platform intersects with a circle in 3D space.
3
+ * @param {Platform} platform - The platform to check.
4
+ * @param {Circle} circle - The circle to check.
5
+ * @returns {boolean} True if the platform intersects the circle, false otherwise.
6
+ */
7
+ export function intersectsCircle(platform, circle) {
8
+ const [left, top, front] = platform.position
9
+ const [extension, elevation, thickness] = platform.size
10
+
11
+ const [x, y, z] = circle.position
12
+
13
+ const lowestPoint = y - circle.radius
14
+ const isAbove = lowestPoint <= top && lowestPoint >= top - elevation
15
+
16
+ const isOverlappingX = x >= left && x <= left + extension
17
+ const isOverlappingZ = z >= front && z <= front + thickness
18
+
19
+ return isAbove && isOverlappingX && isOverlappingZ
20
+ }
21
+
22
+ /**
23
+ * Determines whether platform intersects with a rectangle in 3D space.
24
+ *
25
+ * @param {Platform} platform - The platform defined by its position (top-left-front corner) and size (extension, elevation, thickness).
26
+ * @param {Rectangle} rectangle - The rectangle defined by its position (top-left-front corner) and size (width, height, depth).
27
+ * @returns {boolean} True if the platform intersects the rectangle, false otherwise.
28
+ */
29
+ export function intersectsRectangle(platform, rectangle) {
30
+ const [left, top, front] = platform.position
31
+ const [extension, elevation, thickness] = platform.size
32
+
33
+ const [x, y, z] = rectangle.position
34
+ const [width, , depth] = rectangle.size
35
+
36
+ const isAbove = y >= top && y <= top + elevation
37
+
38
+ const isOverlappingX = x + width >= left && x <= left + extension
39
+ const isOverlappingZ = z + depth >= front && z <= front + thickness
40
+
41
+ return isAbove && isOverlappingX && isOverlappingZ
42
+ }
@@ -0,0 +1,133 @@
1
+ import { expect, test } from "vitest"
2
+
3
+ import { intersectsCircle, intersectsRectangle } from "./platform"
4
+
5
+ test("it should prove that a circle right above a platform intersects with it", () => {
6
+ const platform = {
7
+ position: [-1, 0, 0],
8
+ size: [2, 1, 0],
9
+ }
10
+ const circle = {
11
+ position: [1, 1, 0],
12
+ radius: 1,
13
+ }
14
+
15
+ expect(intersectsCircle(platform, circle)).toBe(true)
16
+ })
17
+
18
+ test("it should prove that a circle way above a platform intersects with it", () => {
19
+ const platform = {
20
+ position: [0, 0, -2],
21
+ size: [2, 1, 0],
22
+ }
23
+ const circle = {
24
+ position: [1, 1, 0],
25
+ radius: 1,
26
+ }
27
+
28
+ expect(intersectsCircle(platform, circle)).toBe(false)
29
+ })
30
+
31
+ test("it should prove that a circle crossing a platform does not intersect with it", () => {
32
+ const platform = {
33
+ position: [-1, -1, 0],
34
+ size: [2, 2, 0],
35
+ }
36
+ const circle = {
37
+ position: [1, 1, 0],
38
+ radius: 1,
39
+ }
40
+
41
+ expect(intersectsCircle(platform, circle)).toBe(false)
42
+ })
43
+
44
+ test("it should prove that a circle below a platform does not intersect with it", () => {
45
+ const platform = {
46
+ position: [0, 2, 0],
47
+ size: [2, 1, 0],
48
+ }
49
+ const circle = {
50
+ position: [1, 1, 0],
51
+ radius: 1,
52
+ }
53
+
54
+ expect(intersectsCircle(platform, circle)).toBe(false)
55
+ })
56
+
57
+ test("it should prove that a circle not crossing a platform horizontally does not intersect with it", () => {
58
+ const platform = {
59
+ position: [-3, -1, 0],
60
+ size: [2, 1, 0],
61
+ }
62
+ const circle = {
63
+ position: [1, 1, 0],
64
+ radius: 1,
65
+ }
66
+
67
+ expect(intersectsCircle(platform, circle)).toBe(false)
68
+ })
69
+
70
+ test("it should prove that a rectangle crossing a platform from above intersects with it", () => {
71
+ const platform = {
72
+ position: [0, -1, 0],
73
+ size: [2, 2, 0],
74
+ }
75
+ const rectangle = {
76
+ position: [0, 0, 0],
77
+ size: [2, 2, 0],
78
+ }
79
+
80
+ expect(intersectsRectangle(platform, rectangle)).toBe(true)
81
+ })
82
+
83
+ test("it should prove that a rectangle right on top of a platform intersects with it", () => {
84
+ const platform = {
85
+ position: [0, -2, 0],
86
+ size: [2, 2, 0],
87
+ }
88
+ const rectangle = {
89
+ position: [0, 0, 0],
90
+ size: [2, 2, 0],
91
+ }
92
+
93
+ expect(intersectsRectangle(platform, rectangle)).toBe(true)
94
+ })
95
+
96
+ test("it should prove that a rectangle crossing a platform from below does not intersect with it", () => {
97
+ const platform = {
98
+ position: [0, 1, 0],
99
+ size: [2, 2, 0],
100
+ }
101
+ const rectangle = {
102
+ position: [0, 0, 0],
103
+ size: [2, 2, 0],
104
+ }
105
+
106
+ expect(intersectsRectangle(platform, rectangle)).toBe(false)
107
+ })
108
+
109
+ test("it should prove that a rectangle below a platform does not intersect with it", () => {
110
+ const platform = {
111
+ position: [0, -3, 0],
112
+ size: [2, 2, 0],
113
+ }
114
+ const rectangle = {
115
+ position: [0, 0, 0],
116
+ size: [2, 2, 0],
117
+ }
118
+
119
+ expect(intersectsRectangle(platform, rectangle)).toBe(false)
120
+ })
121
+
122
+ test("it should prove that a rectangle not crossing a platform horizontally does not intersect with it", () => {
123
+ const platform = {
124
+ position: [-3, 0, 0],
125
+ size: [2, 2, 0],
126
+ }
127
+ const rectangle = {
128
+ position: [0, 0, 0],
129
+ size: [2, 2, 0],
130
+ }
131
+
132
+ expect(intersectsRectangle(platform, rectangle)).toBe(false)
133
+ })