@inglorious/engine 0.1.1 → 0.3.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 (201) hide show
  1. package/README.md +75 -72
  2. package/package.json +15 -37
  3. package/src/{engine/ai → ai}/movement/dynamic/align.js +9 -9
  4. package/src/{engine/ai → ai}/movement/dynamic/arrive.js +9 -10
  5. package/src/{engine/ai → ai}/movement/dynamic/evade.js +9 -9
  6. package/src/{engine/ai → ai}/movement/dynamic/face.js +5 -6
  7. package/src/{engine/ai/movement/dynamic/seek.js → ai/movement/dynamic/flee.js} +8 -7
  8. package/src/ai/movement/dynamic/look-where-youre-going.js +16 -0
  9. package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +9 -8
  10. package/src/{engine/ai → ai}/movement/dynamic/pursue.js +9 -9
  11. package/src/{engine/ai/movement/dynamic/flee.js → ai/movement/dynamic/seek.js} +7 -8
  12. package/src/{engine/ai → ai}/movement/dynamic/wander.js +9 -10
  13. package/src/{engine/ai → ai}/movement/kinematic/align.js +7 -7
  14. package/src/{engine/ai → ai}/movement/kinematic/arrive.js +8 -8
  15. package/src/{engine/ai → ai}/movement/kinematic/face.js +5 -6
  16. package/src/{engine/ai → ai}/movement/kinematic/flee.js +5 -5
  17. package/src/{engine/ai → ai}/movement/kinematic/seek.js +5 -5
  18. package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +10 -10
  19. package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +9 -9
  20. package/src/{engine/ai → ai}/movement/kinematic/wander.js +5 -5
  21. package/src/animation/sprite.js +101 -0
  22. package/src/animation/ticker.js +38 -0
  23. package/src/behaviors/camera.js +68 -0
  24. package/src/behaviors/controls/dynamic/modern.js +76 -0
  25. package/src/behaviors/controls/dynamic/shooter.js +84 -0
  26. package/src/behaviors/controls/dynamic/tank.js +69 -0
  27. package/src/behaviors/controls/event-handlers.js +17 -0
  28. package/src/behaviors/controls/kinematic/modern.js +76 -0
  29. package/src/behaviors/controls/kinematic/shooter.js +82 -0
  30. package/src/behaviors/controls/kinematic/tank.js +67 -0
  31. package/src/behaviors/debug/collision.js +29 -0
  32. package/src/behaviors/fps.js +29 -0
  33. package/src/behaviors/fsm.js +33 -0
  34. package/src/{game/decorators → behaviors}/fsm.test.js +15 -22
  35. package/src/behaviors/game.js +15 -0
  36. package/src/behaviors/input/controls.js +37 -0
  37. package/src/behaviors/input/gamepad.js +114 -0
  38. package/src/behaviors/input/input.js +48 -0
  39. package/src/behaviors/input/keyboard.js +64 -0
  40. package/src/behaviors/input/mouse.js +91 -0
  41. package/src/behaviors/physics/bouncy.js +25 -0
  42. package/src/behaviors/physics/clamped.js +36 -0
  43. package/src/{game/decorators/collisions.js → behaviors/physics/collidable.js} +3 -7
  44. package/src/behaviors/physics/jumpable.js +145 -0
  45. package/src/behaviors/ui/button.js +17 -0
  46. package/src/collision/detection.js +110 -0
  47. package/src/core/api.js +34 -0
  48. package/src/core/dev-tools.js +135 -0
  49. package/src/core/engine.js +119 -0
  50. package/src/core/loop.js +15 -0
  51. package/src/{engine/loop → core/loops}/animation-frame.js +1 -2
  52. package/src/{engine/loop → core/loops}/elapsed.js +1 -2
  53. package/src/{engine/loop → core/loops}/fixed.js +1 -2
  54. package/src/{engine/loop → core/loops}/flash.js +1 -2
  55. package/src/{engine/loop → core/loops}/lag.js +1 -2
  56. package/src/core/select.js +26 -0
  57. package/src/core/store.js +178 -0
  58. package/src/core/store.test.js +110 -0
  59. package/src/main.js +7 -2
  60. package/src/{engine/movement → movement}/dynamic/modern.js +3 -6
  61. package/src/{engine/movement → movement}/dynamic/tank.js +9 -9
  62. package/src/{engine/movement → movement}/kinematic/modern.js +3 -3
  63. package/src/movement/kinematic/modern.test.js +27 -0
  64. package/src/{engine/movement → movement}/kinematic/tank.js +5 -5
  65. package/src/physics/bounds.js +138 -0
  66. package/src/physics/position.js +43 -0
  67. package/src/physics/position.test.js +80 -0
  68. package/src/systems/sprite-animation.js +27 -0
  69. package/src/engine/ai/movement/dynamic/look-where-youre-going.js +0 -17
  70. package/src/engine/collision/detection.js +0 -115
  71. package/src/engine/loop.js +0 -15
  72. package/src/engine/movement/kinematic/modern.test.js +0 -27
  73. package/src/engine/store.js +0 -174
  74. package/src/engine/store.test.js +0 -256
  75. package/src/engine.js +0 -74
  76. package/src/game/animation.js +0 -26
  77. package/src/game/bounds.js +0 -66
  78. package/src/game/decorators/character.js +0 -5
  79. package/src/game/decorators/clamp-to-bounds.js +0 -15
  80. package/src/game/decorators/controls/dynamic/modern.js +0 -48
  81. package/src/game/decorators/controls/dynamic/shooter.js +0 -47
  82. package/src/game/decorators/controls/dynamic/tank.js +0 -55
  83. package/src/game/decorators/controls/kinematic/modern.js +0 -49
  84. package/src/game/decorators/controls/kinematic/shooter.js +0 -45
  85. package/src/game/decorators/controls/kinematic/tank.js +0 -52
  86. package/src/game/decorators/debug/collisions.js +0 -32
  87. package/src/game/decorators/double-jump.js +0 -70
  88. package/src/game/decorators/fps.js +0 -30
  89. package/src/game/decorators/fsm.js +0 -27
  90. package/src/game/decorators/game.js +0 -11
  91. package/src/game/decorators/image/image.js +0 -5
  92. package/src/game/decorators/image/sprite.js +0 -5
  93. package/src/game/decorators/image/tilemap.js +0 -5
  94. package/src/game/decorators/input/controls.js +0 -27
  95. package/src/game/decorators/input/gamepad.js +0 -74
  96. package/src/game/decorators/input/input.js +0 -41
  97. package/src/game/decorators/input/keyboard.js +0 -49
  98. package/src/game/decorators/input/mouse.js +0 -65
  99. package/src/game/decorators/jump.js +0 -72
  100. package/src/game/decorators/platform.js +0 -5
  101. package/src/game/decorators/ui/button.js +0 -21
  102. package/src/game/sprite.js +0 -119
  103. package/src/ui/canvas/absolute-position.js +0 -17
  104. package/src/ui/canvas/character.js +0 -35
  105. package/src/ui/canvas/form/button.js +0 -25
  106. package/src/ui/canvas/fps.js +0 -18
  107. package/src/ui/canvas/image/hitmask.js +0 -37
  108. package/src/ui/canvas/image/image.js +0 -37
  109. package/src/ui/canvas/image/sprite.js +0 -49
  110. package/src/ui/canvas/image/tilemap.js +0 -64
  111. package/src/ui/canvas/mouse.js +0 -37
  112. package/src/ui/canvas/shapes/circle.js +0 -31
  113. package/src/ui/canvas/shapes/rectangle.js +0 -31
  114. package/src/ui/canvas.js +0 -81
  115. package/src/ui/react/game/character/character.module.scss +0 -17
  116. package/src/ui/react/game/character/index.jsx +0 -30
  117. package/src/ui/react/game/cursor/cursor.module.scss +0 -47
  118. package/src/ui/react/game/cursor/index.jsx +0 -20
  119. package/src/ui/react/game/form/fields/field/field.module.scss +0 -5
  120. package/src/ui/react/game/form/fields/field/index.jsx +0 -56
  121. package/src/ui/react/game/form/fields/fields.module.scss +0 -48
  122. package/src/ui/react/game/form/fields/index.jsx +0 -12
  123. package/src/ui/react/game/form/form.module.scss +0 -18
  124. package/src/ui/react/game/form/index.jsx +0 -22
  125. package/src/ui/react/game/fps/index.jsx +0 -16
  126. package/src/ui/react/game/game.jsx +0 -71
  127. package/src/ui/react/game/index.jsx +0 -29
  128. package/src/ui/react/game/platform/index.jsx +0 -30
  129. package/src/ui/react/game/platform/platform.module.scss +0 -7
  130. package/src/ui/react/game/scene/index.jsx +0 -25
  131. package/src/ui/react/game/scene/scene.module.scss +0 -9
  132. package/src/ui/react/game/sprite/index.jsx +0 -58
  133. package/src/ui/react/game/sprite/sprite.module.css +0 -3
  134. package/src/ui/react/game/stats/index.jsx +0 -22
  135. package/src/ui/react/hocs/with-absolute-position/index.jsx +0 -20
  136. package/src/ui/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
  137. package/src/ui/react/index.jsx +0 -9
  138. package/src/utils/algorithms/decision-tree.js +0 -24
  139. package/src/utils/algorithms/decision-tree.test.js +0 -102
  140. package/src/utils/algorithms/path-finding.js +0 -155
  141. package/src/utils/algorithms/path-finding.test.js +0 -151
  142. package/src/utils/algorithms/types.d.ts +0 -28
  143. package/src/utils/data-structures/array.js +0 -83
  144. package/src/utils/data-structures/array.test.js +0 -173
  145. package/src/utils/data-structures/board.js +0 -159
  146. package/src/utils/data-structures/board.test.js +0 -242
  147. package/src/utils/data-structures/boolean.js +0 -9
  148. package/src/utils/data-structures/heap.js +0 -164
  149. package/src/utils/data-structures/heap.test.js +0 -103
  150. package/src/utils/data-structures/object.js +0 -102
  151. package/src/utils/data-structures/object.test.js +0 -121
  152. package/src/utils/data-structures/objects.js +0 -48
  153. package/src/utils/data-structures/objects.test.js +0 -99
  154. package/src/utils/data-structures/tree.js +0 -36
  155. package/src/utils/data-structures/tree.test.js +0 -33
  156. package/src/utils/data-structures/types.d.ts +0 -4
  157. package/src/utils/functions/functions.js +0 -19
  158. package/src/utils/functions/functions.test.js +0 -23
  159. package/src/utils/math/geometry/circle.js +0 -117
  160. package/src/utils/math/geometry/circle.test.js +0 -97
  161. package/src/utils/math/geometry/hitmask.js +0 -39
  162. package/src/utils/math/geometry/hitmask.test.js +0 -84
  163. package/src/utils/math/geometry/line.js +0 -35
  164. package/src/utils/math/geometry/line.test.js +0 -49
  165. package/src/utils/math/geometry/platform.js +0 -42
  166. package/src/utils/math/geometry/platform.test.js +0 -133
  167. package/src/utils/math/geometry/point.js +0 -71
  168. package/src/utils/math/geometry/point.test.js +0 -81
  169. package/src/utils/math/geometry/rectangle.js +0 -45
  170. package/src/utils/math/geometry/rectangle.test.js +0 -42
  171. package/src/utils/math/geometry/segment.js +0 -80
  172. package/src/utils/math/geometry/segment.test.js +0 -183
  173. package/src/utils/math/geometry/triangle.js +0 -15
  174. package/src/utils/math/geometry/triangle.test.js +0 -11
  175. package/src/utils/math/geometry/types.d.ts +0 -23
  176. package/src/utils/math/linear-algebra/2d.js +0 -28
  177. package/src/utils/math/linear-algebra/2d.test.js +0 -17
  178. package/src/utils/math/linear-algebra/quaternion.js +0 -22
  179. package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
  180. package/src/utils/math/linear-algebra/quaternions.js +0 -20
  181. package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
  182. package/src/utils/math/linear-algebra/types.d.ts +0 -4
  183. package/src/utils/math/linear-algebra/vector.js +0 -302
  184. package/src/utils/math/linear-algebra/vector.test.js +0 -257
  185. package/src/utils/math/linear-algebra/vectors.js +0 -122
  186. package/src/utils/math/linear-algebra/vectors.test.js +0 -65
  187. package/src/utils/math/numbers.js +0 -90
  188. package/src/utils/math/numbers.test.js +0 -137
  189. package/src/utils/math/rng.js +0 -44
  190. package/src/utils/math/rng.test.js +0 -39
  191. package/src/utils/math/statistics.js +0 -43
  192. package/src/utils/math/statistics.test.js +0 -47
  193. package/src/utils/math/trigonometry.js +0 -89
  194. package/src/utils/math/trigonometry.test.js +0 -52
  195. package/src/utils/physics/acceleration.js +0 -63
  196. package/src/utils/physics/friction.js +0 -30
  197. package/src/utils/physics/friction.test.js +0 -44
  198. package/src/utils/physics/gravity.js +0 -71
  199. package/src/utils/physics/gravity.test.js +0 -80
  200. package/src/utils/physics/jump.js +0 -41
  201. package/src/utils/physics/velocity.js +0 -38
@@ -1,97 +0,0 @@
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
- })
@@ -1,39 +0,0 @@
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
- }
@@ -1,84 +0,0 @@
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
- })
@@ -1,35 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
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
- })
@@ -1,42 +0,0 @@
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
- }
@@ -1,133 +0,0 @@
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
- })
@@ -1,71 +0,0 @@
1
- /**
2
- * @typedef {import("./types").Point} Point
3
- * @typedef {import("./types").Line} Line
4
- * @typedef {import("./types").Circle} Circle
5
- * @typedef {import("./types").Rectangle} Rectangle
6
- */
7
-
8
- const SQUARED = 2
9
- const HALF = 2
10
-
11
- import { distanceFromPoint } from "./line.js"
12
-
13
- /**
14
- * Calculates the distance from a point to a line.
15
- * @param {Point} point - The point as a 3D coordinate [x, y, z].
16
- * @param {Line} line - The line to calculate the distance from.
17
- * @returns {number} The distance from the point to the line.
18
- */
19
- export function getDistanceFromLine(point, line) {
20
- return distanceFromPoint(line, point)
21
- }
22
-
23
- /**
24
- * Checks if two points intersect.
25
- * @param {Point} point1 - The first point as a 3D coordinate [x, y, z].
26
- * @param {Point} point2 - The second point as a 3D coordinate [x, y, z].
27
- * @returns {boolean} True if the points intersect, false otherwise.
28
- */
29
- export function intersectsPoint(point1, point2) {
30
- const [x1, y1, z1] = point1
31
- const [x2, y2, z2] = point2
32
- return x1 === x2 && y1 === y2 && z1 === z2
33
- }
34
-
35
- /**
36
- * Checks if a point intersects with a circle.
37
- * @param {Point} point - The point as a 3D coordinate [x, y, z].
38
- * @param {Circle} circle - The circle with a position and radius.
39
- * @returns {boolean} True if the point intersects the circle, false otherwise.
40
- */
41
- export function intersectsCircle(point, circle) {
42
- const [x, y, z] = point
43
- const [left, top, front] = circle.position
44
- const radius = circle.radius
45
-
46
- return (
47
- (x - left) ** SQUARED + (y - top) ** SQUARED + (z - front) ** SQUARED <=
48
- radius ** SQUARED
49
- )
50
- }
51
-
52
- /**
53
- * Checks if a point intersects with a rectangle.
54
- * @param {Point} point - The point as a 3D coordinate [x, y, z].
55
- * @param {Rectangle} rectangle - The rectangle with a position and size.
56
- * @returns {boolean} True if the point intersects the rectangle, false otherwise.
57
- */
58
- export function intersectsRectangle(point, rectangle) {
59
- const [x, y, z] = point
60
- const [left, top, front] = rectangle.position
61
- const [width, height, depth] = rectangle.size
62
-
63
- return (
64
- x >= left - width / HALF &&
65
- x <= left + width / HALF &&
66
- y >= top - height / HALF &&
67
- y <= top + height / HALF &&
68
- z >= front - depth / HALF &&
69
- z <= front + depth / HALF
70
- )
71
- }
@@ -1,81 +0,0 @@
1
- import { expect, test } from "vitest"
2
-
3
- import {
4
- intersectsCircle,
5
- intersectsPoint,
6
- intersectsRectangle,
7
- } from "./point.js"
8
-
9
- test("it should prove that two equal points intersect", () => {
10
- const point1 = [1.5, 1.5, 0]
11
- const point2 = [1.5, 1.5, 0]
12
-
13
- expect(intersectsPoint(point1, point2)).toBe(true)
14
- })
15
-
16
- test("it should prove that two different points do not intersect", () => {
17
- const point1 = [1.5, 1.5, 0]
18
- const point2 = [2, 1, 0]
19
-
20
- expect(intersectsPoint(point1, point2)).toBe(false)
21
- })
22
-
23
- test("it should prove that a point inside of a circle intersects with it", () => {
24
- const point = [1.5, 1.5, 0]
25
- const circle = {
26
- position: [1, 1, 0],
27
- radius: 1,
28
- }
29
-
30
- expect(intersectsCircle(point, circle)).toBe(true)
31
- })
32
-
33
- test("it should prove that a point on the border of a circle intersects with it", () => {
34
- const point = [2, 1, 0]
35
- const circle = {
36
- position: [1, 1, 0],
37
- radius: 1,
38
- }
39
-
40
- expect(intersectsCircle(point, circle)).toBe(true)
41
- })
42
-
43
- test("it should prove that a point outside of a circle does not intersect with it", () => {
44
- const point = [2, 2, 0]
45
- const circle = {
46
- position: [1, 1, 0],
47
- radius: 1,
48
- }
49
-
50
- expect(intersectsCircle(point, circle)).toBe(false)
51
- })
52
-
53
- test("it should prove that a point inside of a rectangle intersects with it", () => {
54
- const point = [1.5, 1.5, 0]
55
- const rectangle = {
56
- position: [1, 1, 0],
57
- size: [2, 2, 0],
58
- }
59
-
60
- expect(intersectsRectangle(point, rectangle)).toBe(true)
61
- })
62
-
63
- test("it should prove that a point on the border of a rectangle intersects with it", () => {
64
- const point = [2, 1, 0]
65
- const rectangle = {
66
- position: [1, 1, 0],
67
- size: [2, 2, 0],
68
- }
69
-
70
- expect(intersectsRectangle(point, rectangle)).toBe(true)
71
- })
72
-
73
- test("it should prove that a point outside of a rectangle does not intersect with it", () => {
74
- const point = [2.5, 2.5, 0]
75
- const rectangle = {
76
- position: [1, 1, 0],
77
- size: [2, 2, 0],
78
- }
79
-
80
- expect(intersectsRectangle(point, rectangle)).toBe(false)
81
- })
@@ -1,45 +0,0 @@
1
- /**
2
- * @typedef {import("./types").Rectangle} Rectangle
3
- * @typedef {import("./types").Platform} Platform
4
- */
5
-
6
- import { intersectsRectangle as circleIntersectsRectangle } from "./circle.js"
7
- import { intersectsRectangle as platformIntersectsRectangle } from "./platform.js"
8
-
9
- export function intersectsCircle(rectangle, circle) {
10
- return circleIntersectsRectangle(circle, rectangle)
11
- }
12
-
13
- /**
14
- * Determines whether a rectangle intersects another in 3D space.
15
- *
16
- * @param {Rectangle} rectangle1 - The rectangle defined by its position (x, y, z) and size (width, height, depth).
17
- * @param {Rectangle} rectangle2 - The other rectangle defined by its position (z, y, z) and size (width, height, depth).
18
- * @returns {boolean} True if the two rectangles intersect, false otherwise.
19
- */
20
- export function intersectsRectangle(rectangle1, rectangle2) {
21
- const [x1, y1, z1] = rectangle1.position
22
- const [w1, h1, d1] = rectangle1.size
23
-
24
- const [x2, y2, z2] = rectangle2.position
25
- const [w2, h2, d2] = rectangle2.size
26
-
27
- return (
28
- x1 <= x2 + w2 &&
29
- x1 + w1 >= x2 &&
30
- y1 <= y2 + h2 &&
31
- y1 + h1 >= y2 &&
32
- z1 <= z2 + d2 &&
33
- z1 + d1 >= z2
34
- )
35
- }
36
-
37
- /**
38
- * Checks if a rectangle intersects with a platform.
39
- * @param {Rectangle} rectangle - The rectangle to check.
40
- * @param {Platform} platform - The platform to check.
41
- * @returns {boolean} True if the rectangle intersects the platform, false otherwise.
42
- */
43
- export function intersectsPlatform(rectangle, platform) {
44
- return platformIntersectsRectangle(platform, rectangle)
45
- }