@inglorious/engine 0.2.0 → 0.4.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 (169) hide show
  1. package/README.md +76 -75
  2. package/package.json +14 -25
  3. package/src/{engine/ai → ai}/movement/dynamic/align.js +63 -63
  4. package/src/{engine/ai → ai}/movement/dynamic/arrive.js +42 -42
  5. package/src/{engine/ai → ai}/movement/dynamic/evade.js +38 -38
  6. package/src/{engine/ai → ai}/movement/dynamic/face.js +19 -19
  7. package/src/{engine/ai → ai}/movement/dynamic/flee.js +45 -45
  8. package/src/{engine/ai → ai}/movement/dynamic/look-where-youre-going.js +16 -16
  9. package/src/{engine/ai → ai}/movement/dynamic/match-velocity.js +51 -51
  10. package/src/{engine/ai → ai}/movement/dynamic/pursue.js +38 -38
  11. package/src/{engine/ai → ai}/movement/dynamic/seek.js +44 -44
  12. package/src/{engine/ai → ai}/movement/dynamic/wander.js +31 -31
  13. package/src/{engine/ai → ai}/movement/kinematic/align.js +37 -37
  14. package/src/{engine/ai → ai}/movement/kinematic/arrive.js +42 -42
  15. package/src/{engine/ai → ai}/movement/kinematic/face.js +19 -19
  16. package/src/{engine/ai → ai}/movement/kinematic/flee.js +26 -26
  17. package/src/{engine/ai → ai}/movement/kinematic/seek.js +26 -26
  18. package/src/{engine/ai → ai}/movement/kinematic/seek.test.js +42 -42
  19. package/src/{engine/ai → ai}/movement/kinematic/wander-as-seek.js +31 -31
  20. package/src/{engine/ai → ai}/movement/kinematic/wander.js +27 -27
  21. package/src/{engine/animation → animation}/sprite.js +101 -101
  22. package/src/{engine/animation → animation}/ticker.js +38 -38
  23. package/src/{engine/behaviors → behaviors}/camera.js +68 -68
  24. package/src/{engine/behaviors → behaviors}/controls/dynamic/modern.js +76 -76
  25. package/src/{engine/behaviors → behaviors}/controls/dynamic/shooter.js +84 -84
  26. package/src/{engine/behaviors → behaviors}/controls/dynamic/tank.js +69 -69
  27. package/src/{engine/behaviors → behaviors}/controls/event-handlers.js +17 -17
  28. package/src/{engine/behaviors → behaviors}/controls/kinematic/modern.js +76 -76
  29. package/src/{engine/behaviors → behaviors}/controls/kinematic/shooter.js +82 -82
  30. package/src/{engine/behaviors → behaviors}/controls/kinematic/tank.js +67 -67
  31. package/src/behaviors/debug/collision.js +29 -0
  32. package/src/{engine/behaviors → behaviors}/fps.js +29 -29
  33. package/src/{engine/behaviors → behaviors}/fsm.js +33 -33
  34. package/src/{engine/behaviors → behaviors}/fsm.test.js +49 -49
  35. package/src/{engine/behaviors → behaviors}/game.js +15 -15
  36. package/src/{engine/behaviors → behaviors}/input/controls.js +37 -37
  37. package/src/{engine/behaviors → behaviors}/input/gamepad.js +114 -114
  38. package/src/{engine/behaviors → behaviors}/input/input.js +48 -48
  39. package/src/{engine/behaviors → behaviors}/input/keyboard.js +64 -64
  40. package/src/{engine/behaviors → behaviors}/input/mouse.js +91 -91
  41. package/src/{engine/behaviors → behaviors}/physics/bouncy.js +25 -25
  42. package/src/{engine/behaviors → behaviors}/physics/clamped.js +36 -36
  43. package/src/{engine/behaviors → behaviors}/physics/collidable.js +20 -20
  44. package/src/{engine/behaviors → behaviors}/physics/jumpable.js +145 -145
  45. package/src/{engine/behaviors → behaviors}/ui/button.js +17 -17
  46. package/src/{engine/collision → collision}/detection.js +110 -110
  47. package/src/{engine/core → core}/dev-tools.js +135 -135
  48. package/src/{engine/core → core}/engine.js +119 -119
  49. package/src/{engine/core → core}/loop.js +15 -15
  50. package/src/{engine/core → core}/loops/animation-frame.js +25 -25
  51. package/src/{engine/core → core}/loops/elapsed.js +22 -22
  52. package/src/{engine/core → core}/loops/fixed.js +27 -27
  53. package/src/{engine/core → core}/loops/flash.js +13 -13
  54. package/src/{engine/core → core}/loops/lag.js +26 -26
  55. package/src/main.js +10 -10
  56. package/src/{engine/movement → movement}/dynamic/modern.js +21 -21
  57. package/src/{engine/movement → movement}/dynamic/tank.js +43 -43
  58. package/src/{engine/movement → movement}/kinematic/modern.js +16 -16
  59. package/src/{engine/movement → movement}/kinematic/modern.test.js +27 -27
  60. package/src/{engine/movement → movement}/kinematic/tank.js +27 -27
  61. package/src/{engine/physics → physics}/bounds.js +138 -138
  62. package/src/{engine/physics → physics}/position.js +43 -43
  63. package/src/{engine/physics → physics}/position.test.js +80 -80
  64. package/src/{engine/systems → systems}/sprite-animation.js +27 -27
  65. package/src/engine/behaviors/debug/collision.js +0 -35
  66. package/src/engine/core/api.js +0 -34
  67. package/src/engine/core/select.js +0 -26
  68. package/src/engine/core/store.js +0 -178
  69. package/src/engine/core/store.test.js +0 -110
  70. package/src/renderers/canvas/absolute-position.js +0 -18
  71. package/src/renderers/canvas/camera.js +0 -13
  72. package/src/renderers/canvas/canvas-renderer.js +0 -68
  73. package/src/renderers/canvas/character.js +0 -38
  74. package/src/renderers/canvas/form/button.js +0 -25
  75. package/src/renderers/canvas/fps.js +0 -18
  76. package/src/renderers/canvas/image/hitmask.js +0 -51
  77. package/src/renderers/canvas/image/image.js +0 -34
  78. package/src/renderers/canvas/image/sprite.js +0 -49
  79. package/src/renderers/canvas/image/tilemap.js +0 -66
  80. package/src/renderers/canvas/mouse.js +0 -37
  81. package/src/renderers/canvas/rendering-system.js +0 -79
  82. package/src/renderers/canvas/shapes/circle.js +0 -29
  83. package/src/renderers/canvas/shapes/rectangle.js +0 -27
  84. package/src/renderers/react/game/character/character.module.scss +0 -17
  85. package/src/renderers/react/game/character/index.jsx +0 -20
  86. package/src/renderers/react/game/cursor/cursor.module.scss +0 -47
  87. package/src/renderers/react/game/cursor/index.jsx +0 -20
  88. package/src/renderers/react/game/form/fields/field/field.module.scss +0 -5
  89. package/src/renderers/react/game/form/fields/field/index.jsx +0 -56
  90. package/src/renderers/react/game/form/fields/fields.module.scss +0 -48
  91. package/src/renderers/react/game/form/fields/index.jsx +0 -12
  92. package/src/renderers/react/game/form/form.module.scss +0 -18
  93. package/src/renderers/react/game/form/index.jsx +0 -22
  94. package/src/renderers/react/game/fps/index.jsx +0 -16
  95. package/src/renderers/react/game/game.jsx +0 -72
  96. package/src/renderers/react/game/index.jsx +0 -29
  97. package/src/renderers/react/game/platform/index.jsx +0 -30
  98. package/src/renderers/react/game/platform/platform.module.scss +0 -7
  99. package/src/renderers/react/game/scene/index.jsx +0 -27
  100. package/src/renderers/react/game/scene/scene.module.scss +0 -9
  101. package/src/renderers/react/game/sprite/index.jsx +0 -60
  102. package/src/renderers/react/game/sprite/sprite.module.css +0 -3
  103. package/src/renderers/react/game/stats/index.jsx +0 -22
  104. package/src/renderers/react/hocs/with-absolute-position/index.jsx +0 -20
  105. package/src/renderers/react/hocs/with-absolute-position/with-absolute-position.module.scss +0 -5
  106. package/src/renderers/react/index.jsx +0 -9
  107. package/src/utils/algorithms/decision-tree.js +0 -24
  108. package/src/utils/algorithms/decision-tree.test.js +0 -153
  109. package/src/utils/algorithms/path-finding.js +0 -155
  110. package/src/utils/algorithms/path-finding.test.js +0 -151
  111. package/src/utils/algorithms/types.d.ts +0 -28
  112. package/src/utils/data-structures/array.js +0 -83
  113. package/src/utils/data-structures/array.test.js +0 -173
  114. package/src/utils/data-structures/board.js +0 -159
  115. package/src/utils/data-structures/board.test.js +0 -242
  116. package/src/utils/data-structures/boolean.js +0 -9
  117. package/src/utils/data-structures/heap.js +0 -164
  118. package/src/utils/data-structures/heap.test.js +0 -103
  119. package/src/utils/data-structures/object.js +0 -138
  120. package/src/utils/data-structures/object.test.js +0 -218
  121. package/src/utils/data-structures/objects.js +0 -66
  122. package/src/utils/data-structures/objects.test.js +0 -99
  123. package/src/utils/data-structures/tree.js +0 -36
  124. package/src/utils/data-structures/tree.test.js +0 -33
  125. package/src/utils/data-structures/types.d.ts +0 -4
  126. package/src/utils/functions/functions.js +0 -19
  127. package/src/utils/functions/functions.test.js +0 -23
  128. package/src/utils/math/geometry/circle.js +0 -70
  129. package/src/utils/math/geometry/circle.test.js +0 -97
  130. package/src/utils/math/geometry/hitmask.js +0 -70
  131. package/src/utils/math/geometry/hitmask.test.js +0 -155
  132. package/src/utils/math/geometry/line.js +0 -35
  133. package/src/utils/math/geometry/line.test.js +0 -49
  134. package/src/utils/math/geometry/point.js +0 -78
  135. package/src/utils/math/geometry/point.test.js +0 -81
  136. package/src/utils/math/geometry/rectangle.js +0 -76
  137. package/src/utils/math/geometry/rectangle.test.js +0 -42
  138. package/src/utils/math/geometry/segment.js +0 -80
  139. package/src/utils/math/geometry/segment.test.js +0 -183
  140. package/src/utils/math/geometry/triangle.js +0 -15
  141. package/src/utils/math/geometry/triangle.test.js +0 -11
  142. package/src/utils/math/geometry/types.d.ts +0 -23
  143. package/src/utils/math/linear-algebra/2d.js +0 -28
  144. package/src/utils/math/linear-algebra/2d.test.js +0 -17
  145. package/src/utils/math/linear-algebra/quaternion.js +0 -22
  146. package/src/utils/math/linear-algebra/quaternion.test.js +0 -25
  147. package/src/utils/math/linear-algebra/quaternions.js +0 -20
  148. package/src/utils/math/linear-algebra/quaternions.test.js +0 -29
  149. package/src/utils/math/linear-algebra/types.d.ts +0 -4
  150. package/src/utils/math/linear-algebra/vector.js +0 -327
  151. package/src/utils/math/linear-algebra/vector.test.js +0 -265
  152. package/src/utils/math/linear-algebra/vectors.js +0 -122
  153. package/src/utils/math/linear-algebra/vectors.test.js +0 -65
  154. package/src/utils/math/linear-interpolation.js +0 -9
  155. package/src/utils/math/numbers.js +0 -90
  156. package/src/utils/math/numbers.test.js +0 -137
  157. package/src/utils/math/rng.js +0 -44
  158. package/src/utils/math/rng.test.js +0 -39
  159. package/src/utils/math/statistics.js +0 -43
  160. package/src/utils/math/statistics.test.js +0 -47
  161. package/src/utils/math/trigonometry.js +0 -89
  162. package/src/utils/math/trigonometry.test.js +0 -52
  163. package/src/utils/physics/acceleration.js +0 -61
  164. package/src/utils/physics/friction.js +0 -28
  165. package/src/utils/physics/friction.test.js +0 -42
  166. package/src/utils/physics/gravity.js +0 -69
  167. package/src/utils/physics/gravity.test.js +0 -77
  168. package/src/utils/physics/jump.js +0 -31
  169. package/src/utils/physics/velocity.js +0 -36
@@ -1,327 +0,0 @@
1
- /**
2
- * @typedef {import('./types').Vector2} Vector2
3
- * @typedef {import('./types').Vector3} Vector3
4
- */
5
-
6
- import { hypothenuse } from "@inglorious/utils/math/geometry/triangle.js"
7
- import {
8
- abs as nAbs,
9
- clamp as nClamp,
10
- mod as nMod,
11
- snap as nSnap,
12
- } from "@inglorious/utils/math/numbers.js"
13
- import { atan2, cos, sin } from "@inglorious/utils/math/trigonometry.js"
14
-
15
- import { from2D, to2D } from "./2d.js"
16
- import { quaternion } from "./quaternion.js"
17
- import { cross, sum } from "./vectors.js"
18
-
19
- const ZERO_VECTOR = [0, 0, 0] // eslint-disable-line no-magic-numbers
20
- const UNIT_VECTOR = [1, 0, 0] // eslint-disable-line no-magic-numbers
21
-
22
- const X = 0
23
- const Y = 1
24
- const Z = 2
25
- const LAST_COORDINATE = 1
26
- const TWO_COORDINATES = 2
27
- const NO_Y = 0
28
- const DEFAULT_PRECISION = 1
29
- const DEFAULT_DECIMALS = 0
30
-
31
- /**
32
- * Returns the absolute value of each component in the vector.
33
- * @param {Vector3} vector - The input vector.
34
- * @returns {Vector3} The vector with absolute values.
35
- */
36
- export function abs(vector) {
37
- return vector.map(nAbs)
38
- }
39
-
40
- /**
41
- * Calculates the angle of the vector in radians.
42
- * @param {Vector3} vector - The input vector.
43
- * @returns {number} The angle in radians.
44
- */
45
- export function angle(vector) {
46
- return atan2(vector[vector.length - LAST_COORDINATE], vector[X])
47
- }
48
-
49
- /**
50
- * Clamps the magnitude of the vector between the given min and max values.
51
- * @param {Vector3} vector - The input vector.
52
- * @param {number} min - The minimum magnitude.
53
- * @param {number} max - The maximum magnitude.
54
- * @returns {Vector3} The clamped vector.
55
- */
56
- export function clamp(vector, min, max) {
57
- const length = magnitude(vector)
58
-
59
- if (typeof min === "number" && length < min) {
60
- return setMagnitude(vector, min)
61
- }
62
-
63
- if (typeof max === "number" && length > max) {
64
- return setMagnitude(vector, max)
65
- }
66
-
67
- if (typeof min !== "number" && typeof max !== "number") {
68
- return vector.map((coordinate, index) =>
69
- nClamp(coordinate, min[index], max[index]),
70
- )
71
- }
72
-
73
- return vector
74
- }
75
-
76
- /**
77
- * Returns the conjugate of the vector.
78
- * @param {Vector3} vector - The input vector.
79
- * @returns {Vector3} The conjugated vector.
80
- */
81
- export function conjugate(vector) {
82
- return vector.map((coordinate, index) => (index ? -coordinate : coordinate))
83
- }
84
-
85
- /**
86
- * Creates a vector with the given magnitude and angle.
87
- * @param {number} magnitude - The magnitude of the vector.
88
- * @param {number} angle - The angle of the vector in radians.
89
- * @returns {Vector3} The created vector.
90
- */
91
- export function createVector(magnitude, angle) {
92
- return multiply(fromAngle(angle), magnitude)
93
- }
94
-
95
- /**
96
- * Divides each component of the vector by the given scalar.
97
- * @param {Vector3} vector - The input vector.
98
- * @param {number} scalar - The scalar value.
99
- * @returns {Vector3} The resulting vector.
100
- */
101
- export function divide(vector, scalar) {
102
- return vector.map((coordinate) => coordinate / scalar)
103
- }
104
-
105
- /**
106
- * Creates a unit vector from the given angle.
107
- * @param {number} angle - The angle in radians.
108
- * @returns {Vector3} The unit vector.
109
- */
110
- export function fromAngle(angle) {
111
- return rotate(UNIT_VECTOR, angle)
112
- }
113
-
114
- export const length = magnitude
115
-
116
- /**
117
- * Calculates the magnitude of the vector.
118
- * @param {Vector3} vector - The input vector.
119
- * @returns {number} The magnitude of the vector.
120
- */
121
- export function magnitude(vector) {
122
- return hypothenuse(...vector)
123
- }
124
-
125
- /**
126
- * Calculates the modulus of each component in the vector with the given divisor.
127
- * @param {Vector3} vector - The input vector.
128
- * @param {number} divisor - The divisor value.
129
- * @returns {Vector3} The resulting vector.
130
- */
131
- export function mod(vector, divisor) {
132
- return vector.map((coordinate) => nMod(coordinate, divisor))
133
- }
134
-
135
- /**
136
- * Multiplies each component of the vector by the given scalar.
137
- * @param {Vector3} vector - The input vector.
138
- * @param {number} scalar - The scalar value.
139
- * @returns {Vector3} The resulting vector.
140
- */
141
- export function multiply(vector, scalar) {
142
- return vector.map((coordinate) => coordinate * scalar)
143
- }
144
-
145
- /**
146
- * Normalizes the vector to have a magnitude of 1.
147
- * @param {Vector3} vector - The input vector.
148
- * @returns {Vector3} The normalized vector.
149
- */
150
- export function normalize(vector) {
151
- const length = magnitude(vector)
152
- return vector.map((coordinate) => coordinate / length)
153
- }
154
-
155
- export const remainder = mod
156
-
157
- /**
158
- * Rotates the vector by the given angle.
159
- * @param {Vector3} vector - The input vector.
160
- * @param {number} angle - The angle in radians.
161
- * @returns {Vector3} The rotated vector.
162
- */
163
- export function rotate(vector, angle) {
164
- const is2D = vector.length === TWO_COORDINATES
165
-
166
- let v = is2D ? from2D(vector) : vector
167
-
168
- let result = rotateWithQuaternion(v, angle)
169
-
170
- return is2D ? to2D(result) : result
171
- }
172
-
173
- /**
174
- * Sets the angle of the vector while maintaining its magnitude.
175
- * @param {Vector3} vector - The input vector.
176
- * @param {number} angle - The new angle in radians.
177
- * @returns {Vector3} The vector with the updated angle.
178
- */
179
- export function setAngle(vector, angle) {
180
- const length = magnitude(vector)
181
- const [x, z] = toCartesian([length, angle])
182
- return [x, NO_Y, z]
183
- }
184
-
185
- export const setLength = setMagnitude
186
-
187
- /**
188
- * Sets the magnitude of the vector while maintaining its direction.
189
- * @param {Vector3} vector - The input vector.
190
- * @param {number} length - The new magnitude.
191
- * @returns {Vector3} The vector with the updated magnitude.
192
- */
193
- export function setMagnitude(vector, length) {
194
- const normalized = normalize(vector)
195
- return multiply(normalized, length)
196
- }
197
-
198
- /**
199
- * Shifts the components of the vector by the given index.
200
- * @param {Vector3} vector - The input vector.
201
- * @param {number} index - The index to shift by.
202
- * @returns {Vector3} The shifted vector.
203
- */
204
- export function shift(vector, index) {
205
- return [...vector.slice(index), ...vector.slice(X, index)]
206
- }
207
-
208
- /**
209
- * Snaps each component of the vector to the nearest multiple of the given precision.
210
- * @param {Vector3} vector - The input vector.
211
- * @param {number} [precision=DEFAULT_PRECISION] - The precision value.
212
- * @returns {Vector3} The snapped vector.
213
- */
214
- export function snap(vector, precision = DEFAULT_PRECISION) {
215
- return vector.map((coordinate) => nSnap(coordinate, -precision, precision))
216
- }
217
-
218
- export const times = multiply
219
-
220
- /**
221
- * Converts polar coordinates to Cartesian coordinates.
222
- * @param {Vector2} vector - The polar coordinates [magnitude, angle].
223
- * @returns {Vector2} The Cartesian coordinates [x, y].
224
- */
225
- export function toCartesian([magnitude, angle]) {
226
- return [magnitude * cos(angle), magnitude * sin(angle)]
227
- }
228
-
229
- /**
230
- * Converts a vector to cylindrical coordinates.
231
- * @param {Vector2} vector - The input vector.
232
- * @returns {Vector2} The cylindrical coordinates [radius, theta, z].
233
- */
234
- export function toCylindrical(vector) {
235
- const radius = magnitude(vector)
236
- const theta = angle(vector)
237
- return [radius * cos(theta), radius * sin(theta), vector[Z]]
238
- }
239
-
240
- /**
241
- * Converts a vector to polar coordinates.
242
- * @param {Vector2} vector - The input vector.
243
- * @returns {Vector2} The polar coordinates [magnitude, angle].
244
- */
245
- export function toPolar(vector) {
246
- return [magnitude(vector), angle(vector)]
247
- }
248
-
249
- /**
250
- * Converts a vector to a string representation.
251
- * @param {Vector3} vector - The input vector.
252
- * @param {number} [decimals=DEFAULT_DECIMALS] - The number of decimal places.
253
- * @returns {string} The string representation of the vector.
254
- */
255
- export function toString(vector, decimals = DEFAULT_DECIMALS) {
256
- return `[${vector
257
- .map((coordinate) => coordinate.toFixed(decimals))
258
- .join(", ")}]`
259
- }
260
-
261
- // TODO: add toSpherical(vector),
262
-
263
- /**
264
- * Converts a 3D cartesian vector to spherical coordinates [radius, inclination, azimuth].
265
- * - radius (r): distance from the origin.
266
- * - inclination (θ): angle from the Y-axis (0 to PI).
267
- * - azimuth (φ): angle from the X-axis in the XZ-plane (-PI to PI).
268
- *
269
- * @param {Vector3} vector The cartesian vector [x, y, z].
270
- * @returns {Vector3} The spherical coordinates [r, θ, φ].
271
- */
272
- export function toSpherical(vector) {
273
- const r = magnitude(vector)
274
-
275
- if (!r) {
276
- return zero()
277
- }
278
-
279
- // In a Y-up system, inclination is the angle with the Y axis.
280
- const inclination = Math.acos(vector[Y] / r)
281
- // Azimuth is the angle in the XZ plane, which `angle()` calculates.
282
- const azimuth = angle(vector)
283
-
284
- return [r, inclination, azimuth]
285
- }
286
-
287
- /**
288
- * Creates a unit vector with the given angle.
289
- * @param {number} angle - The angle in radians.
290
- * @returns {Vector3} The unit vector.
291
- */
292
- export function unit(angle) {
293
- if (!angle) {
294
- return [...UNIT_VECTOR]
295
- }
296
-
297
- return setAngle(UNIT_VECTOR, angle)
298
- }
299
-
300
- /**
301
- * Creates a zero vector.
302
- * @returns {Vector3} The zero vector.
303
- */
304
- export function zero() {
305
- return [...ZERO_VECTOR]
306
- }
307
-
308
- /**
309
- * Rotates the vector using a quaternion.
310
- * @param {Vector3} vector - The input vector.
311
- * @param {number} angle - The angle in radians.
312
- * @returns {Vector3} The rotated vector.
313
- */
314
- function rotateWithQuaternion(vector, angle) {
315
- if (!angle) {
316
- return vector
317
- }
318
-
319
- // @see https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Performance_comparisons
320
- const [w, ...r] = quaternion(angle)
321
- const result = sum(
322
- vector,
323
- cross(multiply(r, 2), sum(cross(r, vector), multiply(vector, w))), // eslint-disable-line no-magic-numbers
324
- )
325
-
326
- return conjugate(result) // HACK: not really sure why I should invert the result, it just works this way
327
- }
@@ -1,265 +0,0 @@
1
- import { sqrt } from "@inglorious/utils/math/numbers.js"
2
- import { cos, pi, sin } from "@inglorious/utils/math/trigonometry.js"
3
- import { expect, test } from "vitest"
4
-
5
- import {
6
- abs,
7
- angle,
8
- clamp,
9
- conjugate,
10
- createVector,
11
- divide,
12
- fromAngle,
13
- magnitude,
14
- mod,
15
- multiply,
16
- normalize,
17
- rotate,
18
- setAngle,
19
- setMagnitude,
20
- shift,
21
- snap,
22
- toCartesian,
23
- toCylindrical,
24
- toPolar,
25
- toSpherical,
26
- toString,
27
- unit,
28
- } from "./vector.js"
29
-
30
- test("it should compute the absolute value of a vector's coordinates", () => {
31
- const vector = [-2, 0, 3]
32
- const expectedResult = [2, 0, 3]
33
-
34
- expect(abs(vector)).toStrictEqual(expectedResult)
35
- })
36
-
37
- test("it should compute the angle of a 2D vector", () => {
38
- const vector = [1, 1]
39
- const expectedResult = pi() / 4
40
-
41
- expect(angle(vector)).toBe(expectedResult)
42
- })
43
-
44
- test("it should compute the angle of a 3D vector", () => {
45
- const vector = [1, 0, 1]
46
- const expectedResult = pi() / 4
47
-
48
- expect(angle(vector)).toBe(expectedResult)
49
- })
50
-
51
- test("it should clamp the magnitude of a vector to a certain length if too long", () => {
52
- const vector = [6, 8]
53
- const min = 0
54
- const max = 5
55
- const expectedResult = [3, 4]
56
-
57
- expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
58
- })
59
-
60
- test("it should not clamp the magnitude of a vector to a certain length if not too long", () => {
61
- const vector = [3, 4]
62
- const min = 0
63
- const max = 6
64
- const expectedResult = [3, 4]
65
-
66
- expect(clamp(vector, min, max)).toStrictEqual(expectedResult)
67
- })
68
-
69
- test("it should clamp the magnitude of a vector to the values of other vector bounds", () => {
70
- const vector = [6, 8]
71
- const lowerBound = [0, 0]
72
- const upperBound = [3, 4]
73
- const expectedResult = [3, 4]
74
-
75
- expect(clamp(vector, lowerBound, upperBound)).toStrictEqual(expectedResult)
76
- })
77
-
78
- test("it should conjugate a vector", () => {
79
- const vector = [1, 2, 3]
80
- const expectedResult = [1, -2, -3]
81
-
82
- expect(conjugate(vector)).toStrictEqual(expectedResult)
83
- })
84
-
85
- test("it should create a 3D vector given its magnitude and angle", () => {
86
- const magnitude = sqrt(2)
87
- const angle = pi() / 4
88
- const expectedResult = [1, -0, 1.0000000000000002] // close to [1, 0, 1]
89
-
90
- expect(createVector(magnitude, angle)).toStrictEqual(expectedResult)
91
- })
92
-
93
- test("it should divide a vector by a scalar", () => {
94
- const vector = [4, 8, 12]
95
- const scalar = 4
96
- const expectedResult = [1, 2, 3]
97
-
98
- expect(divide(vector, scalar)).toStrictEqual(expectedResult)
99
- })
100
-
101
- test("it should create a 3D unit vector from an angle", () => {
102
- const angle = pi() / 4
103
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(angle), 0, sin(angle)]
104
-
105
- expect(fromAngle(angle)).toStrictEqual(expectedResult)
106
- })
107
-
108
- test("it should compute the magnitude of a vector (aka length)", () => {
109
- const vector = [3, 4]
110
- const expectedResult = 5
111
-
112
- expect(magnitude(vector)).toBe(expectedResult)
113
- })
114
-
115
- test("it should apply the mod operator (aka remainder) on a vector", () => {
116
- const vector = [10, 12, -18]
117
- const divisor = 12
118
- const expectedResult = [10, 0, 6]
119
-
120
- expect(mod(vector, divisor)).toStrictEqual(expectedResult)
121
- })
122
-
123
- test("it should multiply a vector with a scalar (aka times)", () => {
124
- const vector = [1, 2, 3]
125
- const scalar = 4
126
- const expectedResult = [4, 8, 12]
127
-
128
- expect(multiply(vector, scalar)).toStrictEqual(expectedResult)
129
- })
130
-
131
- test("it should normalize a vector so it has unit length", () => {
132
- const vector = [3, 4]
133
- const expectedResult = [0.6, 0.8]
134
-
135
- expect(normalize(vector)).toStrictEqual(expectedResult)
136
- })
137
-
138
- test("it should normalize a negative vector", () => {
139
- const vector = [-3, -4]
140
- const expectedResult = [-0.6, -0.8]
141
-
142
- expect(normalize(vector)).toStrictEqual(expectedResult)
143
- })
144
-
145
- test("it should rotate a 2D vector by a certain angle", () => {
146
- const vector = [1, 0]
147
- const angle = pi() / 4
148
- const expectedResult = [0.7071067811865475, 0.7071067811865476] // close to [cos(pi/4), sin(pi/4)]
149
-
150
- expect(rotate(vector, angle)).toStrictEqual(expectedResult)
151
- })
152
-
153
- test("it should not rotate a vector when the angle is zero", () => {
154
- const vector = [1, 0, 0]
155
- const angle = 0
156
- const expectedResult = [1, 0, 0]
157
-
158
- expect(rotate(vector, angle)).toStrictEqual(expectedResult)
159
- })
160
-
161
- test("it should rotate a vector by a certain angle", () => {
162
- const vector = [1, 0, 0]
163
- const angle = pi() / 4
164
- const expectedResult = [0.7071067811865475, -0, 0.7071067811865476] // close to [cos(pi/4), 0, sin(pi/4)]
165
-
166
- expect(rotate(vector, angle)).toStrictEqual(expectedResult)
167
- })
168
-
169
- test("it should rotate a vector that faces left by a certain angle", () => {
170
- const vector = [-1, 0, 0]
171
- const angle = pi() / 4
172
- const expectedResult = [-0.7071067811865475, -0, -0.7071067811865476] // close to [cos(-3/4pi), 0, sin(-3/4pi)]
173
-
174
- expect(rotate(vector, angle)).toStrictEqual(expectedResult)
175
- })
176
-
177
- test("it should change the angle of a vector", () => {
178
- const vector = [cos(pi() / 4), 0, sin(pi() / 4)]
179
- const expectedResult = [6.123233995736766e-17, 0, 1] // close to [0, 0, 1]
180
-
181
- expect(setAngle(vector, 1.5707963267948966)).toStrictEqual(expectedResult)
182
- })
183
-
184
- test("it should change magnitude of a vector (aka setLength)", () => {
185
- const vector = [3, 4]
186
- const magnitude = 10
187
- const expectedResult = [6, 8]
188
-
189
- expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
190
- })
191
-
192
- test("it should change magnitude of a negative vector", () => {
193
- const vector = [-3, -4]
194
- const magnitude = 10
195
- const expectedResult = [-6, -8]
196
-
197
- expect(setMagnitude(vector, magnitude)).toStrictEqual(expectedResult)
198
- })
199
-
200
- test("it should shift a vector at a certain index", () => {
201
- const vector = [1, 2, 3, 4, 5]
202
- const index = 2
203
- const expectedResult = [3, 4, 5, 1, 2]
204
-
205
- expect(shift(vector, index)).toStrictEqual(expectedResult)
206
- })
207
-
208
- test("it should snap a floating vector to a certain precision", () => {
209
- const vector = [1, 1.5, 1.7]
210
- const precision = 1
211
- const expectedResult = [1, 1, 2]
212
-
213
- expect(snap(vector, precision)).toStrictEqual(expectedResult)
214
- })
215
-
216
- test("it should convert a 2D polar vector to cartesian coordinates", () => {
217
- const vector = [sqrt(2), pi() / 4]
218
- const expectedResult = [1.0000000000000002, 1]
219
-
220
- expect(toCartesian(vector)).toStrictEqual(expectedResult)
221
- })
222
-
223
- test("it should convert a 3D cartesian vector to cylindrical coordinates", () => {
224
- const vector = [1, 1, 1]
225
- const expectedResult = [1.2247448713915892, 1.224744871391589, 1]
226
-
227
- expect(toCylindrical(vector)).toStrictEqual(expectedResult)
228
- })
229
-
230
- test("it should convert a 2D cartesian vector to polar coordinates", () => {
231
- const vector = [1, 1]
232
- const expectedResult = [sqrt(2), pi() / 4]
233
-
234
- expect(toPolar(vector)).toStrictEqual(expectedResult)
235
- })
236
-
237
- test("it should convert a 3D cartesian vector to spherical coordinates", () => {
238
- const vector = [1, 1, 1]
239
- const expectedResult = [sqrt(3), Math.acos(1 / sqrt(3)), pi() / 4]
240
-
241
- expect(toSpherical(vector)).toStrictEqual(expectedResult)
242
- })
243
-
244
- test("it should create a string representation of an integer vector", () => {
245
- const vector = [3, 4]
246
- const expectedResult = "[3, 4]"
247
-
248
- expect(toString(vector)).toBe(expectedResult)
249
- })
250
-
251
- test("it should create a string representation of a float vector", () => {
252
- const vector = [pi() / 4, pi() / 4]
253
- const decimals = 2
254
- const expectedResult = "[0.79, 0.79]"
255
-
256
- expect(toString(vector, decimals)).toBe(expectedResult)
257
- })
258
-
259
- test("it should create a unit vector oriented on the X-axis", () => {
260
- expect(unit()).toStrictEqual([1, 0, 0])
261
- })
262
-
263
- test("it should create a unit vector oriented on the Z-axis", () => {
264
- expect(unit(pi() / 2)).toStrictEqual([6.123233995736766e-17, 0, 1]) // close to [0, 0, 1]
265
- })
@@ -1,122 +0,0 @@
1
- /**
2
- * @typedef {import('./types').Vector2} Vector2
3
- * @typedef {import('./types').Vector3} Vector3
4
- * @typedef {import('./types').Vector7} Vector7
5
- */
6
-
7
- import { magnitude, shift } from "./vector.js"
8
-
9
- /**
10
- * Alias for the sum function.
11
- * @type {typeof sum}
12
- */
13
- export const add = sum
14
-
15
- /**
16
- * Computes the cross product of multiple vectors.
17
- * @param {...Vector3 | Vector7} vectors - The vectors to compute the cross product for.
18
- * @returns {Vector3 | Vector7} The resulting vector after the cross product.
19
- */
20
- export function cross(...vectors) {
21
- return vectors.reduce(crossMultiplyCoordinates)
22
- }
23
-
24
- /**
25
- * Computes the distance between multiple vectors.
26
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the distance between.
27
- * @returns {number} The distance between the vectors.
28
- */
29
- export function distance(...vectors) {
30
- return magnitude(subtract(...vectors))
31
- }
32
-
33
- /**
34
- * Computes the dot product of multiple vectors.
35
- * @param {...Vector2 | Vector3} vectors - The vectors to compute the dot product for.
36
- * @returns {number} The resulting scalar value of the dot product.
37
- */
38
- export function dot(...vectors) {
39
- return vectors
40
- .reduce(dotMultiplyCoordinates)
41
- .reduce((coord1, coord2) => coord1 + coord2)
42
- }
43
-
44
- /**
45
- * Alias for the dot product function.
46
- * @type {typeof dot}
47
- */
48
- export const scalarProduct = dot
49
-
50
- /**
51
- * Subtracts multiple vectors.
52
- * @param {...Vector2 | Vector3} vectors - The vectors to subtract.
53
- * @returns {Vector2 | Vector3} The resulting vector after subtraction.
54
- */
55
- export function subtract(...vectors) {
56
- return vectors.reduce(subtractCoordinates)
57
- }
58
-
59
- /**
60
- * Sums multiple vectors.
61
- * @param {...Vector2 | Vector3} vectors - The vectors to sum.
62
- * @returns {Vector2 | Vector3} The resulting vector after summation.
63
- */
64
- export function sum(...vectors) {
65
- return vectors.reduce(sumCoordinates)
66
- }
67
-
68
- /**
69
- * Alias for the cross product function.
70
- * @type {typeof cross}
71
- */
72
- export const vectorProduct = cross
73
-
74
- /**
75
- * Adds the coordinates of two vectors.
76
- * @param {Vector2 | Vector3} vector1 - The first vector.
77
- * @param {Vector2 | Vector3} vector2 - The second vector.
78
- * @returns {Vector2 | Vector3} The resulting vector after addition.
79
- */
80
- function sumCoordinates(vector1, vector2) {
81
- return vector1.map((coordinate, index) => coordinate + vector2[index])
82
- }
83
-
84
- /**
85
- * Subtracts the coordinates of two vectors.
86
- * @param {Vector2 | Vector3} vector1 - The first vector.
87
- * @param {Vector2 | Vector3} vector2 - The second vector.
88
- * @returns {Vector2 | Vector3} The resulting vector after subtraction.
89
- */
90
- function subtractCoordinates(vector1, vector2) {
91
- return vector1.map((coordinate, index) => coordinate - vector2[index])
92
- }
93
-
94
- /**
95
- * Computes the cross product of two vectors' coordinates.
96
- * @param {Vector3} vector1 - The first vector.
97
- * @param {Vector3} vector2 - The second vector.
98
- * @returns {Vector3} The resulting vector after the cross product.
99
- */
100
- function crossMultiplyCoordinates(vector1, vector2) {
101
- const indexes = Array(vector1.length)
102
- .fill(null)
103
- .map((_, index) => index)
104
-
105
- return indexes.map((_, index) => {
106
- const [index1, index2] = shift(
107
- indexes.filter((_, i) => i !== index),
108
- index,
109
- )
110
- return vector1[index1] * vector2[index2] - vector1[index2] * vector2[index1]
111
- })
112
- }
113
-
114
- /**
115
- * Multiplies the coordinates of two vectors.
116
- * @param {Vector2 | Vector3} vector1 - The first vector.
117
- * @param {Vector2 | Vector3} vector2 - The second vector.
118
- * @returns {Vector2 | Vector3} The resulting vector containing multiplied coordinates.
119
- */
120
- function dotMultiplyCoordinates(vector1, vector2) {
121
- return vector1.map((coordinate, index) => coordinate * vector2[index])
122
- }