@inglorious/utils 3.8.0 → 5.0.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.
- package/README.md +83 -3
- package/package.json +104 -12
- package/src/algorithms/decision-tree.js +3 -3
- package/src/algorithms/path-finding.js +6 -6
- package/src/data-structures/array.js +12 -0
- package/src/data-structures/board.js +97 -15
- package/src/data-structures/index.js +1 -2
- package/src/data-structures/object.js +164 -2
- package/src/data-structures/string.js +77 -0
- package/src/data-structures/tree.js +1 -1
- package/src/functions/function.js +190 -0
- package/src/functions/index.js +1 -4
- package/src/math/circle.js +5 -7
- package/src/math/hitmask.js +1 -1
- package/src/math/index.js +2 -6
- package/src/math/line.js +4 -4
- package/src/math/point.js +31 -9
- package/src/math/quaternion.js +19 -2
- package/src/math/rectangle.js +16 -4
- package/src/math/rng.js +5 -2
- package/src/math/segment.js +11 -5
- package/src/math/trigonometry.js +1 -1
- package/src/math/vector.js +288 -37
- package/src/physics/acceleration.js +2 -3
- package/src/physics/friction.js +1 -1
- package/src/physics/gravity.js +1 -1
- package/src/physics/jump.js +0 -4
- package/src/physics/velocity.js +2 -3
- package/src/v.js +1 -1
- package/types/algorithms/decision-tree.d.ts +27 -0
- package/types/algorithms/index.d.ts +2 -0
- package/types/algorithms/path-finding.d.ts +69 -0
- package/types/data-structures/array.d.ts +61 -0
- package/types/data-structures/board.d.ts +121 -0
- package/types/data-structures/boolean.d.ts +4 -0
- package/types/data-structures/heap.d.ts +69 -0
- package/types/data-structures/index.d.ts +8 -0
- package/types/data-structures/object.d.ts +237 -0
- package/types/data-structures/set.d.ts +4 -0
- package/types/data-structures/tree.d.ts +21 -0
- package/types/functions/function.d.ts +57 -0
- package/types/functions/index.d.ts +1 -0
- package/types/index.d.ts +6 -4
- package/types/math/circle.d.ts +22 -0
- package/types/math/hitmask.d.ts +20 -0
- package/types/math/index.d.ts +14 -0
- package/types/math/line.d.ts +11 -0
- package/types/math/linear-interpolation.d.ts +2 -0
- package/types/math/number.d.ts +26 -0
- package/types/math/point.d.ts +50 -0
- package/types/math/quaternion.d.ts +10 -0
- package/types/math/rectangle.d.ts +57 -0
- package/types/math/rng.d.ts +14 -0
- package/types/math/segment.d.ts +23 -0
- package/types/math/statistics.d.ts +11 -0
- package/types/math/triangle.d.ts +5 -0
- package/types/math/trigonometry.d.ts +26 -0
- package/types/math/vector.d.ts +219 -0
- package/types/physics/acceleration.d.ts +24 -0
- package/types/physics/friction.d.ts +11 -0
- package/types/physics/gravity.d.ts +20 -0
- package/types/physics/index.d.ts +5 -0
- package/types/physics/jump.d.ts +10 -0
- package/types/physics/velocity.d.ts +20 -0
- package/types/v.d.ts +7 -3
- package/src/data-structures/objects.js +0 -113
- package/src/functions/functions.js +0 -31
- package/src/math/quaternions.js +0 -23
- package/src/math/vectors.js +0 -212
- package/types/algorithms.d.ts +0 -26
- package/types/data-structures.d.ts +0 -53
- package/types/math.d.ts +0 -90
- /package/src/math/{numbers.js → number.js} +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Point } from "./point"
|
|
2
|
+
import type { Platform } from "./rectangle"
|
|
3
|
+
|
|
4
|
+
/** Represents the width and depth of a hitmask tile. */
|
|
5
|
+
export type TileSize = readonly [width: number, depth: number]
|
|
6
|
+
|
|
7
|
+
/** Represents a tiled collision mask. */
|
|
8
|
+
export interface Hitmask {
|
|
9
|
+
/** The center of the hitmask. */
|
|
10
|
+
position: Point
|
|
11
|
+
/** The dimensions of each tile. */
|
|
12
|
+
tileSize: TileSize
|
|
13
|
+
/** The number of tile columns. */
|
|
14
|
+
columns: number
|
|
15
|
+
/** The height of each tile. */
|
|
16
|
+
heights: number[]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Checks whether a platform collides with a hitmask. */
|
|
20
|
+
export function findCollisions(hitmask: Hitmask, target: Platform): boolean
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * as circle from "./circle"
|
|
2
|
+
export * as hitmask from "./hitmask"
|
|
3
|
+
export * as line from "./line"
|
|
4
|
+
export * as linearInterpolation from "./linear-interpolation"
|
|
5
|
+
export * as number from "./number"
|
|
6
|
+
export * as point from "./point"
|
|
7
|
+
export * as quaternion from "./quaternion"
|
|
8
|
+
export * as rectangle from "./rectangle"
|
|
9
|
+
export * as rng from "./rng"
|
|
10
|
+
export * as segment from "./segment"
|
|
11
|
+
export * as statistics from "./statistics"
|
|
12
|
+
export * as triangle from "./triangle"
|
|
13
|
+
export * as trigonometry from "./trigonometry"
|
|
14
|
+
export * as vector from "./vector"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Circle } from "./circle"
|
|
2
|
+
import type { Point } from "./point"
|
|
3
|
+
|
|
4
|
+
/** Represents a 2D line as `ax + bz + c = 0`. */
|
|
5
|
+
export type Line = readonly [a: number, b: number, c: number]
|
|
6
|
+
|
|
7
|
+
/** Calculates the shortest distance from a point to a line. */
|
|
8
|
+
export function distanceFromPoint(line: Line, point: Point): number
|
|
9
|
+
|
|
10
|
+
/** Checks whether a line intersects a circle. */
|
|
11
|
+
export function intersectsCircle(line: Line, circle: Circle): boolean
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Returns the absolute value of a number. */
|
|
2
|
+
export function abs(num: number): number
|
|
3
|
+
|
|
4
|
+
/** Clamps a number to an inclusive range. */
|
|
5
|
+
export function clamp(num: number, min: number, max: number): number
|
|
6
|
+
|
|
7
|
+
/** Checks whether a number is within an inclusive range. */
|
|
8
|
+
export function isBetween(num: number, min: number, max: number): boolean
|
|
9
|
+
|
|
10
|
+
/** Checks whether two numbers are within a tolerance. */
|
|
11
|
+
export function isClose(num1: number, num2: number, tolerance?: number): boolean
|
|
12
|
+
|
|
13
|
+
/** Computes a positive modulus, returning the dividend for a zero divisor. */
|
|
14
|
+
export function mod(dividend: number, divisor: number): number
|
|
15
|
+
|
|
16
|
+
/** Snaps a number to the nearest multiple of a precision. */
|
|
17
|
+
export function snap(num: number, precision?: number): number
|
|
18
|
+
|
|
19
|
+
/** Alias for `mod`. */
|
|
20
|
+
export const remainder: typeof mod
|
|
21
|
+
|
|
22
|
+
/** Returns the sign of a number. */
|
|
23
|
+
export function sign(num: number): number
|
|
24
|
+
|
|
25
|
+
/** Computes the square root of a number. */
|
|
26
|
+
export function sqrt(num: number): number
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Vector3 } from "./vector"
|
|
2
|
+
import type { Circle } from "./circle"
|
|
3
|
+
import type { Line } from "./line"
|
|
4
|
+
import type { Rectangle } from "./rectangle"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A point in the Inglorious coordinate system: `[x, y, z]`.
|
|
8
|
+
*
|
|
9
|
+
* The world is **y-up**: the origin sits on the floor, and a bigger `y` is
|
|
10
|
+
* always a higher place in the world. Gravity subtracts from it, a jump adds to
|
|
11
|
+
* it, and an object resting on the ground has `y = 0`.
|
|
12
|
+
*
|
|
13
|
+
* `z` is the depth axis, and it is **y-up as well** — it is not screen-down, so
|
|
14
|
+
* both vertical axes grow upwards. Renderers are what turn them into screen
|
|
15
|
+
* coordinates: `@inglorious/renderer-2d` projects a point to
|
|
16
|
+
* `canvasY = viewportHeight - y - z`, which puts `(0, 0, 0)` at the bottom-left
|
|
17
|
+
* corner of the canvas and turns both axes upside down.
|
|
18
|
+
*
|
|
19
|
+
* @example Reading a position in a 2D game
|
|
20
|
+
* ```js
|
|
21
|
+
* // Ground level, and a hundred pixels above it.
|
|
22
|
+
* v(0, 0, 0) // the floor
|
|
23
|
+
* v(0, 100, 0) // a hundred pixels up
|
|
24
|
+
*
|
|
25
|
+
* // Falling means losing height, in 2D as well as in 3D.
|
|
26
|
+
* entity.velocity[1] -= GRAVITY * dt
|
|
27
|
+
*
|
|
28
|
+
* // Keep a body on the ground.
|
|
29
|
+
* entity.position[1] = 0
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export type Point = readonly [x: number, y: number, z: number]
|
|
33
|
+
|
|
34
|
+
/** Accepts a 3D point, branded vector, or object with a point position. */
|
|
35
|
+
export type PointInput = Point | Vector3 | { position: Point | Vector3 }
|
|
36
|
+
|
|
37
|
+
/** Calculates the distance from a point to a line. */
|
|
38
|
+
export function getDistanceFromLine(point: Point, line: Line): number
|
|
39
|
+
|
|
40
|
+
/** Checks whether two points occupy the same position. */
|
|
41
|
+
export function intersectsPoint(point1: PointInput, point2: PointInput): boolean
|
|
42
|
+
|
|
43
|
+
/** Checks whether a point intersects a circle. */
|
|
44
|
+
export function intersectsCircle(point: PointInput, circle: Circle): boolean
|
|
45
|
+
|
|
46
|
+
/** Checks whether a point intersects a rectangle. */
|
|
47
|
+
export function intersectsRectangle(
|
|
48
|
+
point: PointInput,
|
|
49
|
+
rectangle: Rectangle,
|
|
50
|
+
): boolean
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Vector, Vector3 } from "./vector"
|
|
2
|
+
|
|
3
|
+
/** A quaternion represented as a four-dimensional vector. */
|
|
4
|
+
export type Quaternion = Vector<readonly [number, number, number, number]>
|
|
5
|
+
|
|
6
|
+
/** Combines two quaternions with the Hamilton product. */
|
|
7
|
+
export function combine(q1: Quaternion, q2: Quaternion): Quaternion
|
|
8
|
+
|
|
9
|
+
/** Creates a quaternion for a rotation around an axis. */
|
|
10
|
+
export function quaternion(angle?: number, axis?: Vector3): Quaternion
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Circle } from "./circle"
|
|
2
|
+
import type { Point, PointInput } from "./point"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The dimensions of a 3D object, as `[width, height, depth]`.
|
|
6
|
+
*
|
|
7
|
+
* `width` is the extent along `x`, `height` the extent along `y` and `depth` the
|
|
8
|
+
* extent along `z`. `height` and `depth` are **independent axes**, not halves of
|
|
9
|
+
* one screen dimension, so in a 2D game that plays on the `xy` plane a size reads
|
|
10
|
+
* as `v(w, h, 0)` — and an entity whose body is placed on the `xz` plane instead
|
|
11
|
+
* reads as `v(w, 0, h)`.
|
|
12
|
+
*
|
|
13
|
+
* When a renderer has to flatten both vertical axes onto the screen, it adds
|
|
14
|
+
* them: `@inglorious/renderer-2d` draws a rectangle `height + depth` tall. A
|
|
15
|
+
* collision shape never does — `intersectsRectangle` tests the two axes
|
|
16
|
+
* separately, which is why a body cannot collide with something that is merely
|
|
17
|
+
* "near it" on the other axis.
|
|
18
|
+
*/
|
|
19
|
+
export type Size = readonly [width: number, height: number, depth: number]
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Represents an axis-aligned rectangle.
|
|
23
|
+
*
|
|
24
|
+
* `position` is the point the box hangs off and `size` is its extent, so with
|
|
25
|
+
* the default centred anchor the position is the middle of the box. Set an
|
|
26
|
+
* `anchor` to describe the box by a different point, such as `[0, 0]` for its
|
|
27
|
+
* bottom-left corner, so that the box stands on its position.
|
|
28
|
+
*/
|
|
29
|
+
export interface Rectangle {
|
|
30
|
+
/** The center of the rectangle. */
|
|
31
|
+
position: Point
|
|
32
|
+
/** The dimensions of the rectangle. */
|
|
33
|
+
size: Size
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Represents a rectangular platform. */
|
|
37
|
+
export interface Platform {
|
|
38
|
+
/** The center of the platform. */
|
|
39
|
+
position: Point
|
|
40
|
+
/** The dimensions of the platform. */
|
|
41
|
+
size: Size
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Checks whether a rectangle intersects a point. */
|
|
45
|
+
export function intersectsPoint(
|
|
46
|
+
rectangle: Rectangle,
|
|
47
|
+
point: PointInput,
|
|
48
|
+
): boolean
|
|
49
|
+
|
|
50
|
+
/** Checks whether a rectangle intersects a circle. */
|
|
51
|
+
export function intersectsCircle(rectangle: Rectangle, circle: Circle): boolean
|
|
52
|
+
|
|
53
|
+
/** Checks whether two rectangles intersect. */
|
|
54
|
+
export function intersectsRectangle(
|
|
55
|
+
rectangle1: Rectangle,
|
|
56
|
+
rectangle2: Rectangle,
|
|
57
|
+
): boolean
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Returns `undefined` when no values are supplied. */
|
|
2
|
+
export function choose(): undefined
|
|
3
|
+
|
|
4
|
+
/** Chooses a random supplied value. */
|
|
5
|
+
export function choose<T>(value: T, ...values: T[]): T
|
|
6
|
+
|
|
7
|
+
/** Generates a random number for the supplied bounds and step. */
|
|
8
|
+
export function random(): number
|
|
9
|
+
export function random(to: number): number
|
|
10
|
+
export function random(from: number, to: number): number
|
|
11
|
+
export function random(from: number, to: number, step: number): number
|
|
12
|
+
|
|
13
|
+
/** Generates a random value between two independent random values. */
|
|
14
|
+
export function randomBinomial(): number
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Circle } from "./circle"
|
|
2
|
+
import type { Line } from "./line"
|
|
3
|
+
import type { Point } from "./point"
|
|
4
|
+
|
|
5
|
+
/** Represents a line segment between two points. */
|
|
6
|
+
export interface Segment {
|
|
7
|
+
/** The starting point of the segment. */
|
|
8
|
+
from: Point
|
|
9
|
+
/** The ending point of the segment. */
|
|
10
|
+
to: Point
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Calculates the coefficients of the line containing a segment. */
|
|
14
|
+
export function coefficients(segment: Segment): Line
|
|
15
|
+
|
|
16
|
+
/** Finds the closest point on a segment. */
|
|
17
|
+
export function closestPoint(segment: Segment, point: Point): Point
|
|
18
|
+
|
|
19
|
+
/** Calculates the shortest distance from a point to a segment. */
|
|
20
|
+
export function distanceFromPoint(segment: Segment, point: Point): number
|
|
21
|
+
|
|
22
|
+
/** Checks whether a segment intersects a circle. */
|
|
23
|
+
export function intersectsCircle(segment: Segment, circle: Circle): boolean
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Calculates the arithmetic mean of the supplied numbers. */
|
|
2
|
+
export function mean(...numbers: number[]): number
|
|
3
|
+
|
|
4
|
+
/** Alias for `mean`. */
|
|
5
|
+
export const average: typeof mean
|
|
6
|
+
|
|
7
|
+
/** Calculates the median of the supplied numbers. */
|
|
8
|
+
export function median(...numbers: number[]): number
|
|
9
|
+
|
|
10
|
+
/** Finds the most frequent supplied value. */
|
|
11
|
+
export function mode(...values: unknown[]): string | undefined
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Calculates the arctangent of a quotient in radians. */
|
|
2
|
+
export function atan2(y: number, x: number): number
|
|
3
|
+
|
|
4
|
+
/** Calculates the cosine of an angle in radians. */
|
|
5
|
+
export function cos(angle: number): number
|
|
6
|
+
|
|
7
|
+
/** Alias for `cos`. */
|
|
8
|
+
export const cosine: typeof cos
|
|
9
|
+
|
|
10
|
+
/** Returns pi. */
|
|
11
|
+
export function pi(): number
|
|
12
|
+
|
|
13
|
+
/** Calculates the sine of an angle in radians. */
|
|
14
|
+
export function sin(angle: number): number
|
|
15
|
+
|
|
16
|
+
/** Alias for `sin`. */
|
|
17
|
+
export const sine: typeof sin
|
|
18
|
+
|
|
19
|
+
/** Converts radians to degrees. */
|
|
20
|
+
export function toDegrees(radians: number): number
|
|
21
|
+
|
|
22
|
+
/** Converts degrees to radians. */
|
|
23
|
+
export function toRadians(degrees: number): number
|
|
24
|
+
|
|
25
|
+
/** Normalizes an angle to the range [-π, π]. */
|
|
26
|
+
export function toRange(angle: number): number
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/** Identifies a value created by the vector factory. */
|
|
2
|
+
type IsVector = {
|
|
3
|
+
readonly __isVector__: true
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** A dynamically sized vector. */
|
|
7
|
+
export type Vector<Coordinates extends readonly number[] = readonly number[]> =
|
|
8
|
+
IsVector & Coordinates
|
|
9
|
+
|
|
10
|
+
/** A two-dimensional vector. */
|
|
11
|
+
export type Vector2 = Vector<readonly [number, number]>
|
|
12
|
+
|
|
13
|
+
/** A three-dimensional vector. */
|
|
14
|
+
export type Vector3 = Vector<readonly [number, number, number]>
|
|
15
|
+
|
|
16
|
+
/** A seven-dimensional vector. */
|
|
17
|
+
export type Vector7 = Vector<
|
|
18
|
+
readonly [number, number, number, number, number, number, number]
|
|
19
|
+
>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The vector with every component at zero.
|
|
23
|
+
*
|
|
24
|
+
* Vectors are mutable, so copy it with `v(...ZERO_VECTOR)` before storing it
|
|
25
|
+
* where something writes to one.
|
|
26
|
+
*/
|
|
27
|
+
export const ZERO_VECTOR: Vector3
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The vector one unit long along the x axis.
|
|
31
|
+
*
|
|
32
|
+
* Vectors are mutable, so copy it with `v(...UNIT_VECTOR)` before storing it
|
|
33
|
+
* where something writes to one.
|
|
34
|
+
*/
|
|
35
|
+
export const UNIT_VECTOR: Vector3
|
|
36
|
+
|
|
37
|
+
/** Returns the absolute value of each vector component. */
|
|
38
|
+
export function abs<T extends Vector>(vector: T): T
|
|
39
|
+
|
|
40
|
+
/** Alias for `sum`. */
|
|
41
|
+
export const add: typeof sum
|
|
42
|
+
|
|
43
|
+
/** Calculates the angle of a vector. */
|
|
44
|
+
export function angle<T extends Vector>(vector: T): number
|
|
45
|
+
|
|
46
|
+
/** Clamps a vector by scalar magnitude bounds. */
|
|
47
|
+
export function clamp<T extends Vector>(vector: T, min: number, max: number): T
|
|
48
|
+
|
|
49
|
+
/** Clamps a vector by component-wise vector bounds. */
|
|
50
|
+
export function clamp<T extends Vector>(
|
|
51
|
+
vector: T,
|
|
52
|
+
min: readonly number[],
|
|
53
|
+
max: readonly number[],
|
|
54
|
+
): T
|
|
55
|
+
|
|
56
|
+
/** Returns the vector conjugate. */
|
|
57
|
+
export function conjugate<T extends Vector>(vector: T): T
|
|
58
|
+
|
|
59
|
+
/** Creates a 3D vector from a magnitude and angle. */
|
|
60
|
+
export function createVector(magnitude: number, angle: number): Vector3
|
|
61
|
+
|
|
62
|
+
/** Computes the cross product of same-width 3D or 7D vectors. */
|
|
63
|
+
export function cross<T extends Vector3 | Vector7>(...vectors: [T, ...T[]]): T
|
|
64
|
+
|
|
65
|
+
/** Computes the distance between same-width 2D or 3D vectors. */
|
|
66
|
+
export function distance<T extends Vector2 | Vector3>(
|
|
67
|
+
...vectors: [T, ...T[]]
|
|
68
|
+
): number
|
|
69
|
+
|
|
70
|
+
/** Divides same-width vectors component-wise. */
|
|
71
|
+
export function divide<T extends Vector2 | Vector3>(...vectors: [T, ...T[]]): T
|
|
72
|
+
|
|
73
|
+
/** Divides each vector component by a scalar. */
|
|
74
|
+
export function divide<T extends Vector2 | Vector3>(
|
|
75
|
+
vector: T,
|
|
76
|
+
scalar: number,
|
|
77
|
+
): T
|
|
78
|
+
|
|
79
|
+
/** Divides a scalar by each vector component. */
|
|
80
|
+
export function divideBy<T extends Vector>(scalar: number, vector: T): T
|
|
81
|
+
|
|
82
|
+
/** Computes the dot product of same-width 2D or 3D vectors. */
|
|
83
|
+
export function dot<T extends Vector2 | Vector3>(
|
|
84
|
+
...vectors: [T, ...T[]]
|
|
85
|
+
): number
|
|
86
|
+
|
|
87
|
+
/** Converts a 2D vector to a 3D vector. */
|
|
88
|
+
export function from2D(vector: Vector2): Vector3
|
|
89
|
+
|
|
90
|
+
/** Creates a 3D unit vector from an angle. */
|
|
91
|
+
export function fromAngle(angle: number): Vector3
|
|
92
|
+
|
|
93
|
+
/** Alias for `multiply`. */
|
|
94
|
+
export const hadamard: typeof multiply
|
|
95
|
+
|
|
96
|
+
/** Checks whether a value is a vector. */
|
|
97
|
+
export function isVector<T>(value: T): value is T & Vector
|
|
98
|
+
|
|
99
|
+
/** Calculates a vector's magnitude. */
|
|
100
|
+
export function magnitude<T extends Vector>(vector: T): number
|
|
101
|
+
|
|
102
|
+
/** Alias for `magnitude`. */
|
|
103
|
+
export const length: typeof magnitude
|
|
104
|
+
|
|
105
|
+
/** Applies a component-wise modulus to same-width vectors. */
|
|
106
|
+
export function mod<T extends Vector2 | Vector3>(...vectors: [T, ...T[]]): T
|
|
107
|
+
|
|
108
|
+
/** Applies a modulus to each vector component. */
|
|
109
|
+
export function mod<T extends Vector2 | Vector3>(vector: T, divisor: number): T
|
|
110
|
+
|
|
111
|
+
/** Applies a scalar modulus to each vector component. */
|
|
112
|
+
export function modOf<T extends Vector>(scalar: number, vector: T): T
|
|
113
|
+
|
|
114
|
+
/** Multiplies same-width vectors component-wise. */
|
|
115
|
+
export function multiply<T extends Vector2 | Vector3>(
|
|
116
|
+
...vectors: [T, ...T[]]
|
|
117
|
+
): T
|
|
118
|
+
|
|
119
|
+
/** Multiplies each vector component by a scalar. */
|
|
120
|
+
export function multiply<T extends Vector2 | Vector3>(
|
|
121
|
+
vector: T,
|
|
122
|
+
scalar: number,
|
|
123
|
+
): T
|
|
124
|
+
|
|
125
|
+
/** Normalizes a vector to unit magnitude. */
|
|
126
|
+
/**
|
|
127
|
+
* Flips the direction of every component.
|
|
128
|
+
*/
|
|
129
|
+
export function negate<T extends Vector>(vector: T): T
|
|
130
|
+
|
|
131
|
+
export function normalize<T extends Vector>(vector: T): T
|
|
132
|
+
|
|
133
|
+
/** Alias for `power`. */
|
|
134
|
+
export const pow: typeof power
|
|
135
|
+
|
|
136
|
+
/** Raises same-width vectors component-wise to powers. */
|
|
137
|
+
export function power<T extends Vector2 | Vector3>(...vectors: [T, ...T[]]): T
|
|
138
|
+
|
|
139
|
+
/** Raises each vector component to a power. */
|
|
140
|
+
export function power<T extends Vector2 | Vector3>(
|
|
141
|
+
vector: T,
|
|
142
|
+
exponent: number,
|
|
143
|
+
): T
|
|
144
|
+
|
|
145
|
+
/** Raises a scalar to each vector component. */
|
|
146
|
+
export function powerOf<T extends Vector>(scalar: number, vector: T): T
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Draws a random vector, taking each component from its own range.
|
|
150
|
+
*/
|
|
151
|
+
export function randomVector(
|
|
152
|
+
low?: readonly number[],
|
|
153
|
+
high?: readonly number[],
|
|
154
|
+
): Vector3
|
|
155
|
+
|
|
156
|
+
/** Alias for `mod`. */
|
|
157
|
+
export const remainder: typeof mod
|
|
158
|
+
|
|
159
|
+
/** Rotates a 2D vector. */
|
|
160
|
+
export function rotate<T extends Vector2>(vector: T, angle: number): T
|
|
161
|
+
|
|
162
|
+
/** Rotates a 3D vector around the Y-axis. */
|
|
163
|
+
export function rotate<T extends Vector3>(vector: T, angle: number): T
|
|
164
|
+
|
|
165
|
+
/** Alias for `multiply`. */
|
|
166
|
+
export const scale: typeof multiply
|
|
167
|
+
|
|
168
|
+
/** Sets a vector's angle while preserving its magnitude. */
|
|
169
|
+
export function setAngle(vector: Vector, angle: number): Vector3
|
|
170
|
+
|
|
171
|
+
/** Alias for `setMagnitude`. */
|
|
172
|
+
export const setLength: typeof setMagnitude
|
|
173
|
+
|
|
174
|
+
/** Sets a vector's magnitude while preserving its direction. */
|
|
175
|
+
export function setMagnitude<T extends Vector>(vector: T, length: number): T
|
|
176
|
+
|
|
177
|
+
/** Rotates vector components by an index. */
|
|
178
|
+
export function shift<T extends Vector>(vector: T, index: number): T
|
|
179
|
+
|
|
180
|
+
/** Snaps each vector component to a precision. */
|
|
181
|
+
export function snap<T extends Vector>(vector: T, precision?: number): T
|
|
182
|
+
|
|
183
|
+
/** Subtracts same-width vectors component-wise. */
|
|
184
|
+
export function subtract<T extends Vector2 | Vector3>(
|
|
185
|
+
...vectors: [T, ...T[]]
|
|
186
|
+
): T
|
|
187
|
+
|
|
188
|
+
/** Sums same-width vectors component-wise. */
|
|
189
|
+
export function sum<T extends Vector2 | Vector3>(...vectors: [T, ...T[]]): T
|
|
190
|
+
|
|
191
|
+
/** Alias for `multiply`. */
|
|
192
|
+
export const times: typeof multiply
|
|
193
|
+
|
|
194
|
+
/** Converts a 3D vector to a 2D vector. */
|
|
195
|
+
export function to2D(vector: Vector3): Vector2
|
|
196
|
+
|
|
197
|
+
/** Converts 2D polar coordinates to Cartesian coordinates. */
|
|
198
|
+
export function toCartesian<T extends Vector2>(vector: T): T
|
|
199
|
+
|
|
200
|
+
/** Converts a 3D Cartesian vector to cylindrical coordinates. */
|
|
201
|
+
export function toCylindrical<T extends Vector3>(vector: T): T
|
|
202
|
+
|
|
203
|
+
/** Converts a 2D Cartesian vector to polar coordinates. */
|
|
204
|
+
export function toPolar<T extends Vector2>(vector: T): T
|
|
205
|
+
|
|
206
|
+
/** Formats a vector as a string. */
|
|
207
|
+
export function toString<T extends Vector>(vector: T, decimals?: number): string
|
|
208
|
+
|
|
209
|
+
/** Converts a 3D Cartesian vector to spherical coordinates. */
|
|
210
|
+
export function toSpherical<T extends Vector3>(vector: T): T
|
|
211
|
+
|
|
212
|
+
/** Creates a 3D unit vector. */
|
|
213
|
+
export function unit(angle?: number): Vector3
|
|
214
|
+
|
|
215
|
+
/** Alias for `cross`. */
|
|
216
|
+
export const vectorProduct: typeof cross
|
|
217
|
+
|
|
218
|
+
/** Creates a 3D zero vector. */
|
|
219
|
+
export function zero(): Vector3
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Vector3 } from "../math/vector"
|
|
2
|
+
|
|
3
|
+
export interface AccelerationParams {
|
|
4
|
+
acceleration?: Vector3
|
|
5
|
+
friction?: number
|
|
6
|
+
maxAcceleration: number
|
|
7
|
+
maxSpeed: number
|
|
8
|
+
position?: Vector3
|
|
9
|
+
velocity?: Vector3
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface AccelerationResult {
|
|
13
|
+
acceleration: Vector3
|
|
14
|
+
position: Vector3
|
|
15
|
+
velocity: Vector3
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Applies acceleration to an object using Euler integration.
|
|
20
|
+
*/
|
|
21
|
+
export function applyAcceleration(
|
|
22
|
+
params: AccelerationParams,
|
|
23
|
+
dt?: number,
|
|
24
|
+
): AccelerationResult
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Vector3 } from "../math/vector"
|
|
2
|
+
|
|
3
|
+
export interface FrictionParams {
|
|
4
|
+
friction: number
|
|
5
|
+
velocity?: Vector3
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Applies friction to a velocity vector.
|
|
10
|
+
*/
|
|
11
|
+
export function applyFriction(params: FrictionParams, dt?: number): Vector3
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Vector3 } from "../math/vector"
|
|
2
|
+
|
|
3
|
+
export interface GravityParams {
|
|
4
|
+
maxJump?: number
|
|
5
|
+
maxLeap?: number
|
|
6
|
+
maxSpeed?: number
|
|
7
|
+
position?: Vector3
|
|
8
|
+
vy?: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GravityResult {
|
|
12
|
+
ay: number
|
|
13
|
+
position: Vector3
|
|
14
|
+
vy: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Applies gravity to an object based on its current velocity and position.
|
|
19
|
+
*/
|
|
20
|
+
export function applyGravity(params: GravityParams, dt?: number): GravityResult
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Vector3 } from "../math/vector"
|
|
2
|
+
|
|
3
|
+
export interface VelocityParams {
|
|
4
|
+
maxSpeed?: number
|
|
5
|
+
position?: Vector3
|
|
6
|
+
velocity?: Vector3
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface VelocityResult {
|
|
10
|
+
position: Vector3
|
|
11
|
+
velocity: Vector3
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Applies velocity to a position.
|
|
16
|
+
*/
|
|
17
|
+
export function applyVelocity(
|
|
18
|
+
params: VelocityParams,
|
|
19
|
+
dt?: number,
|
|
20
|
+
): VelocityResult
|
package/types/v.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Vector } from "./math"
|
|
1
|
+
import type { Vector } from "./math/vector"
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Ensures that a given value is a vector.
|
|
@@ -12,7 +12,9 @@ import { Vector } from "./math"
|
|
|
12
12
|
* @param {T} value The value to check and potentially convert.
|
|
13
13
|
* @returns {T extends number[] ? Vector : T} The value as a vector, or the original value if no conversion was needed.
|
|
14
14
|
*/
|
|
15
|
-
export function ensureV<T>(
|
|
15
|
+
export function ensureV<T>(
|
|
16
|
+
value: T,
|
|
17
|
+
): T extends readonly number[] ? Vector<T> : T
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
20
|
* A utility function to create a vector from a list of coordinates.
|
|
@@ -22,4 +24,6 @@ export function ensureV<T>(value: T): T extends number[] ? Vector : T
|
|
|
22
24
|
* @param {...number} coords - The coordinates of the vector.
|
|
23
25
|
* @returns {Vector} The created vector.
|
|
24
26
|
*/
|
|
25
|
-
export function v
|
|
27
|
+
export function v<Coordinates extends number[]>(
|
|
28
|
+
...coords: Coordinates
|
|
29
|
+
): Vector<Coordinates>
|