@inglorious/engine 24.16.0 → 24.17.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/package.json +2 -2
- package/src/ai/movement/dynamic/align.js +16 -17
- package/src/ai/movement/dynamic/alignment.js +25 -0
- package/src/ai/movement/dynamic/arrive.js +13 -5
- package/src/ai/movement/dynamic/avoid-walls.js +93 -0
- package/src/ai/movement/dynamic/cohesion.js +23 -0
- package/src/ai/movement/dynamic/evade.js +13 -31
- package/src/ai/movement/dynamic/face.js +10 -7
- package/src/ai/movement/dynamic/flee.js +11 -35
- package/src/ai/movement/dynamic/look-where-youre-going.js +10 -4
- package/src/ai/movement/dynamic/match-velocity.js +17 -33
- package/src/ai/movement/dynamic/pursue.js +20 -9
- package/src/ai/movement/dynamic/seek.js +11 -34
- package/src/ai/movement/dynamic/separation.js +49 -0
- package/src/ai/movement/dynamic/steering.js +151 -0
- package/src/ai/movement/dynamic/wander.js +17 -5
- package/types/ai/movement/dynamic/align.d.ts +8 -1
- package/types/ai/movement/dynamic/alignment.d.ts +8 -0
- package/types/ai/movement/dynamic/arrive.d.ts +12 -0
- package/types/ai/movement/dynamic/avoid-walls.d.ts +11 -0
- package/types/ai/movement/dynamic/cohesion.d.ts +7 -0
- package/types/ai/movement/dynamic/evade.d.ts +8 -0
- package/types/ai/movement/dynamic/face.d.ts +13 -1
- package/types/ai/movement/dynamic/flee.d.ts +7 -0
- package/types/ai/movement/dynamic/look-where-youre-going.d.ts +7 -1
- package/types/ai/movement/dynamic/match-velocity.d.ts +8 -1
- package/types/ai/movement/dynamic/pursue.d.ts +15 -0
- package/types/ai/movement/dynamic/seek.d.ts +7 -0
- package/types/ai/movement/dynamic/separation.d.ts +11 -0
- package/types/ai/movement/dynamic/steering.d.ts +34 -0
- package/types/ai/movement/dynamic/wander.d.ts +20 -2
- package/types/index.d.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.17.0",
|
|
4
4
|
"description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@inglorious/utils": "^5.
|
|
44
|
+
"@inglorious/utils": "^5.12.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.6",
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { abs, clamp } from "@inglorious/utils/math/number.js"
|
|
2
2
|
import { toRange } from "@inglorious/utils/math/trigonometry.js"
|
|
3
3
|
|
|
4
|
+
import { integrate } from "./steering.js"
|
|
5
|
+
|
|
4
6
|
export const DEFAULT_TARGET_RADIUS = 0.1
|
|
5
7
|
export const DEFAULT_SLOW_RADIUS = 0.1
|
|
6
8
|
export const DEFAULT_TIME_TO_TARGET = 0.1
|
|
@@ -11,12 +13,13 @@ const DEFAULT_MAX_ANGULAR_SPEED = 0
|
|
|
11
13
|
const DEFAULT_ANGULAR_SPEED = 0
|
|
12
14
|
const DEFAULT_ORIENTATION = 0
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Turns towards the target's orientation, slowing within `slowRadius` and asking
|
|
18
|
+
* nothing within `targetRadius` (radians; *AI for Games*, "Align").
|
|
19
|
+
*/
|
|
20
|
+
export function alignSteering(
|
|
17
21
|
entity,
|
|
18
22
|
target,
|
|
19
|
-
dt,
|
|
20
23
|
{
|
|
21
24
|
targetRadius = DEFAULT_TARGET_RADIUS,
|
|
22
25
|
slowRadius = DEFAULT_SLOW_RADIUS,
|
|
@@ -27,14 +30,14 @@ export function align(
|
|
|
27
30
|
entity.maxAngularAcceleration ?? DEFAULT_MAX_ANGULAR_ACCELERATION
|
|
28
31
|
const maxAngularSpeed = entity.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
const angularSpeed = entity.angularSpeed ?? DEFAULT_ANGULAR_SPEED
|
|
34
|
+
const orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
32
35
|
|
|
33
36
|
const direction = toRange(target.orientation - orientation)
|
|
34
37
|
const distance = abs(direction)
|
|
35
38
|
|
|
36
39
|
if (distance < targetRadius) {
|
|
37
|
-
return
|
|
40
|
+
return null
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
let targetAngularSpeed
|
|
@@ -45,19 +48,15 @@ export function align(
|
|
|
45
48
|
}
|
|
46
49
|
targetAngularSpeed *= direction / distance
|
|
47
50
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
let angularAcceleration = angularSpeedDelta / timeToTarget
|
|
51
|
-
angularAcceleration = clamp(
|
|
52
|
-
angularAcceleration,
|
|
51
|
+
const angular = clamp(
|
|
52
|
+
(targetAngularSpeed - angularSpeed) / timeToTarget,
|
|
53
53
|
-maxAngularAcceleration,
|
|
54
54
|
maxAngularAcceleration,
|
|
55
55
|
)
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
angularSpeed * dt +
|
|
60
|
-
angularAcceleration * HALF_ANGULAR_ACCELERATION * dt * dt
|
|
57
|
+
return { angular }
|
|
58
|
+
}
|
|
61
59
|
|
|
62
|
-
|
|
60
|
+
export function align(entity, target, dt, options) {
|
|
61
|
+
return integrate(entity, alignSteering(entity, target, options), dt)
|
|
63
62
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { divide, sum, zero } from "@inglorious/utils/vector.js"
|
|
2
|
+
|
|
3
|
+
import { matchVelocitySteering } from "./match-velocity.js"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Matches the neighbors' average velocity, so a flock heads the same way at the
|
|
7
|
+
* same pace (*AI for Games*, "Flocking and Swarming", alignment as velocity
|
|
8
|
+
* matching; Reynolds' boids, https://www.red3d.com/cwr/boids/). Not to be
|
|
9
|
+
* confused with `align`, which turns to an orientation. `null` without
|
|
10
|
+
* neighbors. The game picks the neighbors, as for `separationSteering`.
|
|
11
|
+
*/
|
|
12
|
+
export function alignmentSteering(entity, neighbors, options) {
|
|
13
|
+
const others = neighbors.filter((neighbor) => neighbor !== entity)
|
|
14
|
+
|
|
15
|
+
if (!others.length) {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const velocity = divide(
|
|
20
|
+
sum(...others.map((neighbor) => neighbor.velocity ?? zero())),
|
|
21
|
+
others.length,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
return matchVelocitySteering(entity, { velocity }, options)
|
|
25
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { magnitude, setMagnitude, subtract } from "@inglorious/utils/vector.js"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { matchVelocitySteering } from "./match-velocity.js"
|
|
4
|
+
import { integrate } from "./steering.js"
|
|
4
5
|
|
|
5
6
|
export const DEFAULT_TARGET_RADIUS = 1
|
|
6
7
|
export const DEFAULT_SLOW_RADIUS = 100
|
|
@@ -8,10 +9,13 @@ export const DEFAULT_TIME_TO_TARGET = 0.1
|
|
|
8
9
|
|
|
9
10
|
const DEFAULT_MAX_SPEED = 0
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Seeks the target at full speed, slowing down within `slowRadius` and asking
|
|
14
|
+
* nothing within `targetRadius` (*AI for Games*, "Arrive").
|
|
15
|
+
*/
|
|
16
|
+
export function arriveSteering(
|
|
12
17
|
entity,
|
|
13
18
|
target,
|
|
14
|
-
dt,
|
|
15
19
|
{
|
|
16
20
|
targetRadius = DEFAULT_TARGET_RADIUS,
|
|
17
21
|
slowRadius = DEFAULT_SLOW_RADIUS,
|
|
@@ -24,7 +28,7 @@ export function arrive(
|
|
|
24
28
|
const distance = magnitude(direction)
|
|
25
29
|
|
|
26
30
|
if (distance < targetRadius) {
|
|
27
|
-
return
|
|
31
|
+
return null
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
let speed
|
|
@@ -35,5 +39,9 @@ export function arrive(
|
|
|
35
39
|
}
|
|
36
40
|
const velocity = setMagnitude(direction, speed)
|
|
37
41
|
|
|
38
|
-
return
|
|
42
|
+
return matchVelocitySteering(entity, { velocity }, { timeToTarget })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function arrive(entity, target, dt, options) {
|
|
46
|
+
return integrate(entity, arriveSteering(entity, target, options), dt)
|
|
39
47
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { v } from "@inglorious/utils/v.js"
|
|
2
|
+
import {
|
|
3
|
+
magnitude,
|
|
4
|
+
multiply,
|
|
5
|
+
setMagnitude,
|
|
6
|
+
sum,
|
|
7
|
+
zero,
|
|
8
|
+
} from "@inglorious/utils/vector.js"
|
|
9
|
+
|
|
10
|
+
import { seekSteering } from "./seek.js"
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_LOOKAHEAD = 100
|
|
13
|
+
export const DEFAULT_AVOID_DISTANCE = 50
|
|
14
|
+
|
|
15
|
+
const ORIGIN = 0
|
|
16
|
+
const X = 0
|
|
17
|
+
const Z = 2
|
|
18
|
+
const INWARD = 1
|
|
19
|
+
const NO_HIT = Infinity
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Keeps the entity inside a `[width, depth]` arena (the game's `size`), after
|
|
23
|
+
* *AI for Games*, "Obstacle and Wall Avoidance": a ray `lookahead` long is cast
|
|
24
|
+
* along the velocity and, if it leaves the arena, the entity seeks a point
|
|
25
|
+
* `avoidDistance` inside the wall from where the ray hits it. `null` when the
|
|
26
|
+
* way ahead is clear, or the entity is still.
|
|
27
|
+
*
|
|
28
|
+
* The book casts the ray through a collision detector, for walls of any shape;
|
|
29
|
+
* the engine has no ray casting yet, so this handles the arena's four walls,
|
|
30
|
+
* the case every game has. One ray can still graze a wall it runs along or
|
|
31
|
+
* clip a corner (the book's answer is whiskers: more, shorter rays); blended
|
|
32
|
+
* with other behaviors that is rarely seen.
|
|
33
|
+
*
|
|
34
|
+
* Rejected: SWARM's "soft walls", a push growing near each edge, which only
|
|
35
|
+
* reacts once the entity is close; looking ahead turns it before it gets there.
|
|
36
|
+
*/
|
|
37
|
+
export function avoidWallsSteering(
|
|
38
|
+
entity,
|
|
39
|
+
[width, depth],
|
|
40
|
+
{
|
|
41
|
+
lookahead = DEFAULT_LOOKAHEAD,
|
|
42
|
+
avoidDistance = DEFAULT_AVOID_DISTANCE,
|
|
43
|
+
} = {},
|
|
44
|
+
) {
|
|
45
|
+
const velocity = entity.velocity ?? zero()
|
|
46
|
+
|
|
47
|
+
if (!magnitude(velocity)) {
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const ray = setMagnitude(velocity, lookahead)
|
|
52
|
+
const hit = castInside(entity.position, ray, width, depth)
|
|
53
|
+
|
|
54
|
+
if (!hit) {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const position = sum(
|
|
59
|
+
entity.position,
|
|
60
|
+
multiply(ray, hit.time),
|
|
61
|
+
multiply(hit.normal, avoidDistance),
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
return seekSteering(entity, { position })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Where a ray from inside the arena first crosses one of its walls, as a
|
|
68
|
+
// fraction of the ray, and that wall's inward normal; `null` if it doesn't.
|
|
69
|
+
function castInside(position, ray, width, depth) {
|
|
70
|
+
const x = crossing(position[X], ray[X], width)
|
|
71
|
+
const z = crossing(position[Z], ray[Z], depth)
|
|
72
|
+
|
|
73
|
+
const time = Math.min(x.time, z.time)
|
|
74
|
+
if (time === NO_HIT) {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const normal =
|
|
79
|
+
x.time <= z.time ? v(x.normal, ORIGIN, ORIGIN) : v(ORIGIN, ORIGIN, z.normal)
|
|
80
|
+
return { time, normal }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function crossing(start, step, size) {
|
|
84
|
+
const end = start + step
|
|
85
|
+
|
|
86
|
+
if (end < ORIGIN) {
|
|
87
|
+
return { time: Math.max((ORIGIN - start) / step, ORIGIN), normal: INWARD }
|
|
88
|
+
}
|
|
89
|
+
if (end > size) {
|
|
90
|
+
return { time: Math.max((size - start) / step, ORIGIN), normal: -INWARD }
|
|
91
|
+
}
|
|
92
|
+
return { time: NO_HIT, normal: ORIGIN }
|
|
93
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { divide, sum } from "@inglorious/utils/vector.js"
|
|
2
|
+
|
|
3
|
+
import { seekSteering } from "./seek.js"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Seeks the neighbors' center of mass (*AI for Games*, "Flocking and Swarming";
|
|
7
|
+
* Reynolds' boids, https://www.red3d.com/cwr/boids/). `null` without
|
|
8
|
+
* neighbors. The game picks the neighbors, as for `separationSteering`.
|
|
9
|
+
*/
|
|
10
|
+
export function cohesionSteering(entity, neighbors) {
|
|
11
|
+
const others = neighbors.filter((neighbor) => neighbor !== entity)
|
|
12
|
+
|
|
13
|
+
if (!others.length) {
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const center = divide(
|
|
18
|
+
sum(...others.map(({ position }) => position)),
|
|
19
|
+
others.length,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
return seekSteering(entity, { position: center })
|
|
23
|
+
}
|
|
@@ -1,40 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
subtract,
|
|
5
|
-
sum,
|
|
6
|
-
zero,
|
|
7
|
-
} from "@inglorious/utils/vector.js"
|
|
8
|
-
|
|
9
|
-
import { flee } from "./flee.js"
|
|
1
|
+
import { fleeSteering } from "./flee.js"
|
|
2
|
+
import { predict } from "./pursue.js"
|
|
3
|
+
import { integrate } from "./steering.js"
|
|
10
4
|
|
|
11
5
|
export const DEFAULT_MAX_PREDICTION = 10
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Flees from where the target will be, predicting at most `maxPrediction`
|
|
9
|
+
* seconds ahead (*AI for Games*, "Pursue and Evade").
|
|
10
|
+
*/
|
|
11
|
+
export function evadeSteering(
|
|
14
12
|
entity,
|
|
15
13
|
target,
|
|
16
|
-
dt,
|
|
17
14
|
{ maxPrediction = DEFAULT_MAX_PREDICTION } = {},
|
|
18
15
|
) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const distance = magnitude(direction)
|
|
23
|
-
|
|
24
|
-
if (!distance) {
|
|
25
|
-
return entity
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const speed = magnitude(velocity)
|
|
29
|
-
|
|
30
|
-
let prediction
|
|
31
|
-
if (speed <= distance / maxPrediction) {
|
|
32
|
-
prediction = maxPrediction
|
|
33
|
-
} else {
|
|
34
|
-
prediction = distance / speed
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const position = sum(target.position, multiply(target.velocity, prediction))
|
|
16
|
+
const position = predict(entity, target, maxPrediction)
|
|
17
|
+
return position && fleeSteering(entity, { ...target, position })
|
|
18
|
+
}
|
|
38
19
|
|
|
39
|
-
|
|
20
|
+
export function evade(entity, target, dt, options) {
|
|
21
|
+
return integrate(entity, evadeSteering(entity, target, options), dt)
|
|
40
22
|
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { angle, magnitude, subtract } from "@inglorious/utils/vector.js"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { alignSteering } from "./align.js"
|
|
4
|
+
import { integrate } from "./steering.js"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/** Turns to look at the target's position (*AI for Games*, "Face"). */
|
|
7
|
+
export function faceSteering(entity, target, options) {
|
|
6
8
|
const direction = subtract(target.position, entity.position)
|
|
7
|
-
const distance = magnitude(direction)
|
|
8
9
|
|
|
9
|
-
if (!
|
|
10
|
-
return
|
|
10
|
+
if (!magnitude(direction)) {
|
|
11
|
+
return null
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
return alignSteering(entity, { orientation: angle(direction) }, options)
|
|
15
|
+
}
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
export function face(entity, target, dt, options) {
|
|
18
|
+
return integrate(entity, faceSteering(entity, target, options), dt)
|
|
16
19
|
}
|
|
@@ -1,46 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
angle,
|
|
3
|
-
clamp,
|
|
4
|
-
magnitude,
|
|
5
|
-
multiply,
|
|
6
|
-
setMagnitude,
|
|
7
|
-
subtract,
|
|
8
|
-
sum,
|
|
9
|
-
zero,
|
|
10
|
-
} from "@inglorious/utils/vector.js"
|
|
1
|
+
import { magnitude, setMagnitude, subtract } from "@inglorious/utils/vector.js"
|
|
11
2
|
|
|
12
|
-
|
|
13
|
-
const DEFAULT_MAX_SPEED = 0
|
|
14
|
-
|
|
15
|
-
const MIN_SPEED = 0
|
|
3
|
+
import { integrate } from "./steering.js"
|
|
16
4
|
|
|
17
|
-
const
|
|
5
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
18
6
|
|
|
19
|
-
|
|
7
|
+
/** Accelerates away from the target at `maxAcceleration` (*AI for Games*, "Flee"). */
|
|
8
|
+
export function fleeSteering(entity, target) {
|
|
20
9
|
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
21
|
-
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
22
|
-
|
|
23
|
-
let velocity = entity.velocity ?? zero()
|
|
24
10
|
|
|
25
11
|
const direction = subtract(entity.position, target.position)
|
|
26
|
-
const distance = magnitude(direction)
|
|
27
12
|
|
|
28
|
-
if (!
|
|
29
|
-
return
|
|
13
|
+
if (!magnitude(direction)) {
|
|
14
|
+
return null
|
|
30
15
|
}
|
|
31
16
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
velocity = sum(velocity, multiply(acceleration, dt))
|
|
35
|
-
velocity = clamp(velocity, MIN_SPEED, maxSpeed)
|
|
36
|
-
|
|
37
|
-
const position = sum(
|
|
38
|
-
entity.position,
|
|
39
|
-
multiply(velocity, dt),
|
|
40
|
-
multiply(acceleration, HALF_ACCELERATION * dt * dt),
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
const orientation = angle(velocity)
|
|
17
|
+
return { linear: setMagnitude(direction, maxAcceleration) }
|
|
18
|
+
}
|
|
44
19
|
|
|
45
|
-
|
|
20
|
+
export function flee(entity, target, dt) {
|
|
21
|
+
return integrate(entity, fleeSteering(entity, target), dt)
|
|
46
22
|
}
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { angle, magnitude, zero } from "@inglorious/utils/vector.js"
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { alignSteering } from "./align.js"
|
|
4
|
+
import { integrate } from "./steering.js"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/** Turns to face where the entity is moving (*AI for Games*, "Looking Where You're Going"). */
|
|
7
|
+
export function lookWhereYoureGoingSteering(entity, options) {
|
|
6
8
|
const velocity = entity.velocity ?? zero()
|
|
7
9
|
|
|
8
10
|
if (!magnitude(velocity)) {
|
|
9
|
-
return
|
|
11
|
+
return null
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
return
|
|
14
|
+
return alignSteering(entity, { orientation: angle(velocity) }, options)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function lookWhereYoureGoing(entity, dt, options) {
|
|
18
|
+
return integrate(entity, lookWhereYoureGoingSteering(entity, options), dt)
|
|
13
19
|
}
|
|
@@ -1,52 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
divide,
|
|
5
|
-
magnitude,
|
|
6
|
-
multiply,
|
|
7
|
-
subtract,
|
|
8
|
-
sum,
|
|
9
|
-
zero,
|
|
10
|
-
} from "@inglorious/utils/vector.js"
|
|
1
|
+
import { clamp, divide, subtract, zero } from "@inglorious/utils/vector.js"
|
|
2
|
+
|
|
3
|
+
import { integrate } from "./steering.js"
|
|
11
4
|
|
|
12
5
|
export const DEFAULT_TIME_TO_TARGET = 0.1
|
|
13
6
|
|
|
14
7
|
const DEFAULT_MAX_ACCELERATION = 0
|
|
15
|
-
const DEFAULT_MAX_SPEED = 0
|
|
16
|
-
const DEFAULT_ORIENTATION = 0
|
|
17
8
|
|
|
18
9
|
const MIN_ACCELERATION = 0
|
|
19
|
-
const MIN_SPEED = 0
|
|
20
|
-
|
|
21
|
-
const HALF_ACCELERATION = 0.5
|
|
22
10
|
|
|
23
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Accelerates to reach the target's velocity in `timeToTarget` seconds, at most
|
|
13
|
+
* `maxAcceleration` (*AI for Games*, "Velocity Matching").
|
|
14
|
+
*/
|
|
15
|
+
export function matchVelocitySteering(
|
|
24
16
|
entity,
|
|
25
17
|
target,
|
|
26
|
-
dt,
|
|
27
18
|
{ timeToTarget = DEFAULT_TIME_TO_TARGET } = {},
|
|
28
19
|
) {
|
|
29
20
|
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
30
|
-
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
31
|
-
|
|
32
|
-
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
33
21
|
|
|
34
|
-
|
|
22
|
+
const velocity = entity.velocity ?? zero()
|
|
35
23
|
const velocityDelta = subtract(target.velocity, velocity)
|
|
36
24
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
velocity = clamp(velocity, MIN_SPEED, maxSpeed)
|
|
42
|
-
|
|
43
|
-
const position = sum(
|
|
44
|
-
entity.position,
|
|
45
|
-
multiply(velocity, dt),
|
|
46
|
-
multiply(acceleration, HALF_ACCELERATION * dt * dt),
|
|
25
|
+
const linear = clamp(
|
|
26
|
+
divide(velocityDelta, timeToTarget),
|
|
27
|
+
MIN_ACCELERATION,
|
|
28
|
+
maxAcceleration,
|
|
47
29
|
)
|
|
48
30
|
|
|
49
|
-
|
|
31
|
+
return { linear }
|
|
32
|
+
}
|
|
50
33
|
|
|
51
|
-
|
|
34
|
+
export function matchVelocity(entity, target, dt, options) {
|
|
35
|
+
return integrate(entity, matchVelocitySteering(entity, target, options), dt)
|
|
52
36
|
}
|
|
@@ -6,23 +6,36 @@ import {
|
|
|
6
6
|
zero,
|
|
7
7
|
} from "@inglorious/utils/vector.js"
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { seekSteering } from "./seek.js"
|
|
10
|
+
import { integrate } from "./steering.js"
|
|
10
11
|
|
|
11
12
|
export const DEFAULT_MAX_PREDICTION = 10
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Seeks where the target will be, predicting at most `maxPrediction` seconds
|
|
16
|
+
* ahead (*AI for Games*, "Pursue and Evade").
|
|
17
|
+
*/
|
|
18
|
+
export function pursueSteering(
|
|
14
19
|
entity,
|
|
15
20
|
target,
|
|
16
|
-
dt,
|
|
17
21
|
{ maxPrediction = DEFAULT_MAX_PREDICTION } = {},
|
|
18
22
|
) {
|
|
23
|
+
const position = predict(entity, target, maxPrediction)
|
|
24
|
+
return position && seekSteering(entity, { ...target, position })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function pursue(entity, target, dt, options) {
|
|
28
|
+
return integrate(entity, pursueSteering(entity, target, options), dt)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The target's position in the time the entity would take to reach it. */
|
|
32
|
+
export function predict(entity, target, maxPrediction) {
|
|
19
33
|
const velocity = entity.velocity ?? zero()
|
|
20
34
|
|
|
21
|
-
const
|
|
22
|
-
const distance = magnitude(direction)
|
|
35
|
+
const distance = magnitude(subtract(target.position, entity.position))
|
|
23
36
|
|
|
24
37
|
if (!distance) {
|
|
25
|
-
return
|
|
38
|
+
return null
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
const speed = magnitude(velocity)
|
|
@@ -34,7 +47,5 @@ export function pursue(
|
|
|
34
47
|
prediction = distance / speed
|
|
35
48
|
}
|
|
36
49
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return seek(entity, { ...target, position }, dt)
|
|
50
|
+
return sum(target.position, multiply(target.velocity ?? zero(), prediction))
|
|
40
51
|
}
|
|
@@ -1,45 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
angle,
|
|
3
|
-
clamp,
|
|
4
|
-
magnitude,
|
|
5
|
-
multiply,
|
|
6
|
-
setMagnitude,
|
|
7
|
-
subtract,
|
|
8
|
-
sum,
|
|
9
|
-
zero,
|
|
10
|
-
} from "@inglorious/utils/vector.js"
|
|
1
|
+
import { magnitude, setMagnitude, subtract } from "@inglorious/utils/vector.js"
|
|
11
2
|
|
|
12
|
-
|
|
13
|
-
const DEFAULT_MAX_SPEED = 0
|
|
14
|
-
|
|
15
|
-
const MIN_SPEED = 0
|
|
3
|
+
import { integrate } from "./steering.js"
|
|
16
4
|
|
|
17
|
-
const
|
|
5
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
18
6
|
|
|
19
|
-
|
|
7
|
+
/** Accelerates towards the target at `maxAcceleration` (*AI for Games*, "Seek"). */
|
|
8
|
+
export function seekSteering(entity, target) {
|
|
20
9
|
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
21
|
-
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
22
10
|
|
|
23
11
|
const direction = subtract(target.position, entity.position)
|
|
24
|
-
const distance = magnitude(direction)
|
|
25
12
|
|
|
26
|
-
if (!
|
|
27
|
-
return
|
|
13
|
+
if (!magnitude(direction)) {
|
|
14
|
+
return null
|
|
28
15
|
}
|
|
29
16
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let velocity = entity.velocity ?? zero()
|
|
33
|
-
velocity = sum(velocity, multiply(acceleration, dt))
|
|
34
|
-
velocity = clamp(velocity, MIN_SPEED, maxSpeed)
|
|
35
|
-
|
|
36
|
-
const position = sum(
|
|
37
|
-
entity.position,
|
|
38
|
-
multiply(velocity, dt),
|
|
39
|
-
multiply(acceleration, HALF_ACCELERATION * dt * dt),
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
const orientation = angle(velocity)
|
|
17
|
+
return { linear: setMagnitude(direction, maxAcceleration) }
|
|
18
|
+
}
|
|
43
19
|
|
|
44
|
-
|
|
20
|
+
export function seek(entity, target, dt) {
|
|
21
|
+
return integrate(entity, seekSteering(entity, target), dt)
|
|
45
22
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { magnitude, multiply, subtract, sum } from "@inglorious/utils/vector.js"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_THRESHOLD = 50
|
|
4
|
+
export const DEFAULT_DECAY_COEFFICIENT = 1_000_000
|
|
5
|
+
|
|
6
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Pushes away from every neighbor closer than `threshold`, harder the closer
|
|
10
|
+
* it is: `decayCoefficient / distance²`, at most `maxAcceleration` each
|
|
11
|
+
* (*AI for Games*, "Separation"; one of Craig Reynolds' three boids rules,
|
|
12
|
+
* https://www.red3d.com/cwr/boids/). `null` when no neighbor is that close.
|
|
13
|
+
*
|
|
14
|
+
* The game picks the neighbors (all of a pool, a grid cell's, a spatial index
|
|
15
|
+
* query), so the cost of looking for them stays in its hands.
|
|
16
|
+
*
|
|
17
|
+
* Rejected: a push falling off linearly with distance, the book's other option,
|
|
18
|
+
* which lets crowded boids overlap; the inverse square keeps them apart.
|
|
19
|
+
*/
|
|
20
|
+
export function separationSteering(
|
|
21
|
+
entity,
|
|
22
|
+
neighbors,
|
|
23
|
+
{
|
|
24
|
+
threshold = DEFAULT_THRESHOLD,
|
|
25
|
+
decayCoefficient = DEFAULT_DECAY_COEFFICIENT,
|
|
26
|
+
} = {},
|
|
27
|
+
) {
|
|
28
|
+
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
29
|
+
|
|
30
|
+
let linear = null
|
|
31
|
+
|
|
32
|
+
for (const neighbor of neighbors) {
|
|
33
|
+
if (neighbor === entity) continue
|
|
34
|
+
|
|
35
|
+
const direction = subtract(entity.position, neighbor.position)
|
|
36
|
+
const distance = magnitude(direction)
|
|
37
|
+
|
|
38
|
+
if (!distance || distance >= threshold) continue
|
|
39
|
+
|
|
40
|
+
const strength = Math.min(
|
|
41
|
+
decayCoefficient / (distance * distance),
|
|
42
|
+
maxAcceleration,
|
|
43
|
+
)
|
|
44
|
+
const push = multiply(direction, strength / distance)
|
|
45
|
+
linear = linear ? sum(linear, push) : push
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return linear && { linear }
|
|
49
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Steering as data, after Ian Millington, *AI for Games* (3rd ed., CRC Press,
|
|
3
|
+
* 2019), chapter 3 "Movement": a steering behavior only says how it wants to
|
|
4
|
+
* accelerate, `{ linear, angular }`, and one update applies it. Kept apart, the
|
|
5
|
+
* accelerations of several behaviors can be blended or arbitrated before
|
|
6
|
+
* anything moves ("Combining Steering Behaviors"), which is what flocking needs.
|
|
7
|
+
* https://www.oreilly.com/library/view/ai-for-games/9781351053280/
|
|
8
|
+
*
|
|
9
|
+
* Every dynamic behavior has an `…Steering` function returning that object (or
|
|
10
|
+
* `null`, the book's "no steering", when it has nothing to ask), and the older
|
|
11
|
+
* name (`seek`, `arrive`…) is that steering passed through `integrate`.
|
|
12
|
+
*/
|
|
13
|
+
import { abs, clamp as clampNumber } from "@inglorious/utils/math/number.js"
|
|
14
|
+
import {
|
|
15
|
+
angle,
|
|
16
|
+
clamp,
|
|
17
|
+
magnitude,
|
|
18
|
+
multiply,
|
|
19
|
+
sum,
|
|
20
|
+
zero,
|
|
21
|
+
} from "@inglorious/utils/vector.js"
|
|
22
|
+
|
|
23
|
+
export const DEFAULT_EPSILON = 0.1
|
|
24
|
+
|
|
25
|
+
const DEFAULT_ANGULAR_SPEED = 0
|
|
26
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
27
|
+
const DEFAULT_MAX_ANGULAR_ACCELERATION = 0
|
|
28
|
+
const DEFAULT_MAX_SPEED = 0
|
|
29
|
+
const DEFAULT_ORIENTATION = 0
|
|
30
|
+
const MIN_SPEED = 0
|
|
31
|
+
|
|
32
|
+
const HALF = 0.5
|
|
33
|
+
const NONE = 0
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Applies a steering to the entity for `dt` seconds, the book's
|
|
37
|
+
* `Kinematic.update`. The linear part changes velocity (at most `maxSpeed`) and
|
|
38
|
+
* position, and turns the entity where it goes; the angular part changes
|
|
39
|
+
* `angularSpeed` and `orientation`, and wins over the linear turn when both are
|
|
40
|
+
* given. `null` leaves the entity as it is, as when a behavior has arrived.
|
|
41
|
+
*/
|
|
42
|
+
export function integrate(entity, steering, dt) {
|
|
43
|
+
if (!steering) {
|
|
44
|
+
return entity
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { linear, angular } = steering
|
|
48
|
+
const result = {}
|
|
49
|
+
|
|
50
|
+
if (linear) {
|
|
51
|
+
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
52
|
+
|
|
53
|
+
let velocity = entity.velocity ?? zero()
|
|
54
|
+
velocity = sum(velocity, multiply(linear, dt))
|
|
55
|
+
velocity = clamp(velocity, MIN_SPEED, maxSpeed)
|
|
56
|
+
|
|
57
|
+
result.acceleration = linear
|
|
58
|
+
result.velocity = velocity
|
|
59
|
+
result.position = sum(
|
|
60
|
+
entity.position,
|
|
61
|
+
multiply(velocity, dt),
|
|
62
|
+
multiply(linear, HALF * dt * dt),
|
|
63
|
+
)
|
|
64
|
+
result.orientation = magnitude(velocity)
|
|
65
|
+
? angle(velocity)
|
|
66
|
+
: (entity.orientation ?? DEFAULT_ORIENTATION)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (angular != null) {
|
|
70
|
+
const angularSpeed =
|
|
71
|
+
(entity.angularSpeed ?? DEFAULT_ANGULAR_SPEED) + angular * dt
|
|
72
|
+
const orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
73
|
+
|
|
74
|
+
result.angularSpeed = angularSpeed
|
|
75
|
+
result.orientation =
|
|
76
|
+
orientation + angularSpeed * dt + angular * HALF * dt * dt
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return result
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Weighted blending (*AI for Games*, "Combining Steering Behaviors"): adds up
|
|
84
|
+
* `[steering, weight]` pairs, then caps the sum at the entity's
|
|
85
|
+
* `maxAcceleration` and `maxAngularAcceleration`. Flocking is separation,
|
|
86
|
+
* cohesion and alignment blended this way. `null` steerings are skipped; all
|
|
87
|
+
* `null` gives `null`.
|
|
88
|
+
*
|
|
89
|
+
* Rejected: normalising the weights to sum to 1, which would let a behavior that
|
|
90
|
+
* asks nothing (a far wall) water down the others.
|
|
91
|
+
*/
|
|
92
|
+
export function blend(entity, weightedSteerings) {
|
|
93
|
+
let linear = null
|
|
94
|
+
let angular = null
|
|
95
|
+
|
|
96
|
+
for (const [steering, weight] of weightedSteerings) {
|
|
97
|
+
if (!steering) continue
|
|
98
|
+
|
|
99
|
+
if (steering.linear) {
|
|
100
|
+
const weighted = multiply(steering.linear, weight)
|
|
101
|
+
linear = linear ? sum(linear, weighted) : weighted
|
|
102
|
+
}
|
|
103
|
+
if (steering.angular != null) {
|
|
104
|
+
angular = (angular ?? NONE) + steering.angular * weight
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!linear && angular == null) {
|
|
109
|
+
return null
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const result = {}
|
|
113
|
+
if (linear) {
|
|
114
|
+
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
115
|
+
result.linear = clamp(linear, MIN_SPEED, maxAcceleration)
|
|
116
|
+
}
|
|
117
|
+
if (angular != null) {
|
|
118
|
+
const maxAngularAcceleration =
|
|
119
|
+
entity.maxAngularAcceleration ?? DEFAULT_MAX_ANGULAR_ACCELERATION
|
|
120
|
+
result.angular = clampNumber(
|
|
121
|
+
angular,
|
|
122
|
+
-maxAngularAcceleration,
|
|
123
|
+
maxAngularAcceleration,
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
return result
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Priorities (*AI for Games*, "Combining Steering Behaviors"): takes steerings
|
|
131
|
+
* from most to least urgent (usually each a `blend` of a group) and returns the
|
|
132
|
+
* first one asking for more than `epsilon`, so avoiding a wall is not averaged
|
|
133
|
+
* away by the wish to flock. When none does, the last one is returned, as in the
|
|
134
|
+
* book.
|
|
135
|
+
*/
|
|
136
|
+
export function prioritize(steerings, { epsilon = DEFAULT_EPSILON } = {}) {
|
|
137
|
+
let last = null
|
|
138
|
+
|
|
139
|
+
for (const steering of steerings) {
|
|
140
|
+
last = steering
|
|
141
|
+
if (!steering) continue
|
|
142
|
+
|
|
143
|
+
const linear = steering.linear ? magnitude(steering.linear) : NONE
|
|
144
|
+
const angular = abs(steering.angular ?? NONE)
|
|
145
|
+
if (linear > epsilon || angular > epsilon) {
|
|
146
|
+
return steering
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return last
|
|
151
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBinomial } from "@inglorious/utils/math/rng.js"
|
|
2
2
|
import { createVector, sum } from "@inglorious/utils/vector.js"
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { seekSteering } from "./seek.js"
|
|
5
|
+
import { integrate } from "./steering.js"
|
|
5
6
|
|
|
6
7
|
export const DEFAULT_WANDER_OFFSET = 100
|
|
7
8
|
export const DEFAULT_WANDER_RADIUS = 100
|
|
@@ -10,22 +11,33 @@ const DEFAULT_MAX_ANGULAR_SPEED = 0
|
|
|
10
11
|
|
|
11
12
|
const DEFAULT_ORIENTATION = 0
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Seeks a point on a circle `wanderRadius` wide, `wanderOffset` ahead, moved a
|
|
16
|
+
* little at random each frame (*AI for Games*, "Wander"). Pass a seeded `rng`
|
|
17
|
+
* (`createRng` from `@inglorious/utils/math/rng.js`) to keep a simulation
|
|
18
|
+
* deterministic.
|
|
19
|
+
*/
|
|
20
|
+
export function wanderSteering(
|
|
14
21
|
entity,
|
|
15
|
-
dt,
|
|
16
22
|
{
|
|
17
23
|
wanderOffset = DEFAULT_WANDER_OFFSET,
|
|
18
24
|
wanderRadius = DEFAULT_WANDER_RADIUS,
|
|
25
|
+
rng,
|
|
19
26
|
} = {},
|
|
20
27
|
) {
|
|
21
28
|
const maxAngularSpeed = entity.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
|
|
29
|
+
const binomial = rng ? rng.randomBinomial() : randomBinomial()
|
|
22
30
|
|
|
23
31
|
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
24
32
|
|
|
25
33
|
let position = sum(entity.position, createVector(wanderOffset, orientation))
|
|
26
34
|
|
|
27
|
-
orientation +=
|
|
35
|
+
orientation += binomial * maxAngularSpeed
|
|
28
36
|
position = sum(position, createVector(wanderRadius, orientation))
|
|
29
37
|
|
|
30
|
-
return
|
|
38
|
+
return seekSteering(entity, { position })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function wander(entity, dt, options) {
|
|
42
|
+
return integrate(entity, wanderSteering(entity, options), dt)
|
|
31
43
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Kinematic, Steering } from "../../../index"
|
|
1
|
+
import type { Kinematic, Steering, SteeringOutput } from "../../../index"
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_TARGET_RADIUS: number
|
|
4
4
|
export const DEFAULT_SLOW_RADIUS: number
|
|
@@ -17,3 +17,10 @@ export function align<TEntity extends Omit<Kinematic, "position">>(
|
|
|
17
17
|
dt: number,
|
|
18
18
|
options?: AlignOptions,
|
|
19
19
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
20
|
+
|
|
21
|
+
/** The angular acceleration `align` applies; `null` within `targetRadius`. */
|
|
22
|
+
export function alignSteering(
|
|
23
|
+
entity: Omit<Kinematic, "position">,
|
|
24
|
+
target: { orientation: number },
|
|
25
|
+
options?: AlignOptions,
|
|
26
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
/** Matches the neighbors' average velocity (flocking's alignment; `align` turns to an orientation instead). */
|
|
4
|
+
export function alignmentSteering(
|
|
5
|
+
entity: Kinematic,
|
|
6
|
+
neighbors: Pick<Kinematic, "velocity">[],
|
|
7
|
+
options?: { timeToTarget?: number },
|
|
8
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -20,3 +21,14 @@ export function arrive<TEntity extends Kinematic>(
|
|
|
20
21
|
timeToTarget?: number
|
|
21
22
|
},
|
|
22
23
|
): Steering<TEntity, DynamicMovement>
|
|
24
|
+
|
|
25
|
+
/** The acceleration `arrive` applies; `null` within `targetRadius`. */
|
|
26
|
+
export function arriveSteering(
|
|
27
|
+
entity: Kinematic,
|
|
28
|
+
target: Pick<SteeringTarget, "position">,
|
|
29
|
+
options?: {
|
|
30
|
+
targetRadius?: number
|
|
31
|
+
slowRadius?: number
|
|
32
|
+
timeToTarget?: number
|
|
33
|
+
},
|
|
34
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_LOOKAHEAD: number
|
|
4
|
+
export const DEFAULT_AVOID_DISTANCE: number
|
|
5
|
+
|
|
6
|
+
/** Seeks a point `avoidDistance` inside the arena's wall when a ray `lookahead` long along the velocity would cross it. */
|
|
7
|
+
export function avoidWallsSteering(
|
|
8
|
+
entity: Kinematic,
|
|
9
|
+
size: [width: number, depth: number],
|
|
10
|
+
options?: { lookahead?: number; avoidDistance?: number },
|
|
11
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -14,3 +15,10 @@ export function evade<TEntity extends Kinematic>(
|
|
|
14
15
|
dt: number,
|
|
15
16
|
options?: { maxPrediction?: number },
|
|
16
17
|
): Steering<TEntity, DynamicMovement>
|
|
18
|
+
|
|
19
|
+
/** The acceleration `evade` applies. */
|
|
20
|
+
export function evadeSteering(
|
|
21
|
+
entity: Kinematic,
|
|
22
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
23
|
+
options?: { maxPrediction?: number },
|
|
24
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Kinematic,
|
|
3
|
+
Steering,
|
|
4
|
+
SteeringOutput,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
2
7
|
import type { AlignOptions } from "./align"
|
|
3
8
|
|
|
4
9
|
/** Turns to face the target. */
|
|
@@ -8,3 +13,10 @@ export function face<TEntity extends Kinematic>(
|
|
|
8
13
|
dt: number,
|
|
9
14
|
options?: AlignOptions,
|
|
10
15
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
16
|
+
|
|
17
|
+
/** The angular acceleration `face` applies. */
|
|
18
|
+
export function faceSteering(
|
|
19
|
+
entity: Kinematic,
|
|
20
|
+
target: Pick<SteeringTarget, "position">,
|
|
21
|
+
options?: AlignOptions,
|
|
22
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -11,3 +12,9 @@ export function flee<TEntity extends Kinematic>(
|
|
|
11
12
|
target: Pick<SteeringTarget, "position">,
|
|
12
13
|
dt: number,
|
|
13
14
|
): Steering<TEntity, DynamicMovement>
|
|
15
|
+
|
|
16
|
+
/** The acceleration `flee` applies: `maxAcceleration` away from the target. */
|
|
17
|
+
export function fleeSteering(
|
|
18
|
+
entity: Kinematic,
|
|
19
|
+
target: Pick<SteeringTarget, "position">,
|
|
20
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Kinematic, Steering } from "../../../index"
|
|
1
|
+
import type { Kinematic, Steering, SteeringOutput } from "../../../index"
|
|
2
2
|
import type { AlignOptions } from "./align"
|
|
3
3
|
|
|
4
4
|
/** Turns to face where it is going. */
|
|
@@ -9,3 +9,9 @@ export function lookWhereYoureGoing<
|
|
|
9
9
|
dt: number,
|
|
10
10
|
options?: AlignOptions,
|
|
11
11
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
12
|
+
|
|
13
|
+
/** The angular acceleration `lookWhereYoureGoing` applies. */
|
|
14
|
+
export function lookWhereYoureGoingSteering(
|
|
15
|
+
entity: Omit<Kinematic, "position">,
|
|
16
|
+
options?: AlignOptions,
|
|
17
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DynamicMovement, Kinematic } from "../../../index"
|
|
1
|
+
import type { DynamicMovement, Kinematic, SteeringOutput } from "../../../index"
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_TIME_TO_TARGET: number
|
|
4
4
|
|
|
@@ -9,3 +9,10 @@ export function matchVelocity(
|
|
|
9
9
|
dt: number,
|
|
10
10
|
options?: { timeToTarget?: number },
|
|
11
11
|
): Required<DynamicMovement>
|
|
12
|
+
|
|
13
|
+
/** The acceleration `matchVelocity` applies. */
|
|
14
|
+
export function matchVelocitySteering(
|
|
15
|
+
entity: Kinematic,
|
|
16
|
+
target: { velocity: import("@inglorious/utils/vector.js").Vector3 },
|
|
17
|
+
options?: { timeToTarget?: number },
|
|
18
|
+
): SteeringOutput
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -14,3 +15,17 @@ export function pursue<TEntity extends Kinematic>(
|
|
|
14
15
|
dt: number,
|
|
15
16
|
options?: { maxPrediction?: number },
|
|
16
17
|
): Steering<TEntity, DynamicMovement>
|
|
18
|
+
|
|
19
|
+
/** The acceleration `pursue` applies. */
|
|
20
|
+
export function pursueSteering(
|
|
21
|
+
entity: Kinematic,
|
|
22
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
23
|
+
options?: { maxPrediction?: number },
|
|
24
|
+
): SteeringOutput | null
|
|
25
|
+
|
|
26
|
+
/** Where the target will be when the entity, at its speed, could reach it: at most `maxPrediction` seconds ahead. `null` when they are in the same place. */
|
|
27
|
+
export function predict(
|
|
28
|
+
entity: Kinematic,
|
|
29
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
30
|
+
maxPrediction: number,
|
|
31
|
+
): import("@inglorious/utils/vector.js").Vector3 | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -11,3 +12,9 @@ export function seek<TEntity extends Kinematic>(
|
|
|
11
12
|
target: Pick<SteeringTarget, "position">,
|
|
12
13
|
dt: number,
|
|
13
14
|
): Steering<TEntity, DynamicMovement>
|
|
15
|
+
|
|
16
|
+
/** The acceleration `seek` applies: `maxAcceleration` towards the target. */
|
|
17
|
+
export function seekSteering(
|
|
18
|
+
entity: Kinematic,
|
|
19
|
+
target: Pick<SteeringTarget, "position">,
|
|
20
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_THRESHOLD: number
|
|
4
|
+
export const DEFAULT_DECAY_COEFFICIENT: number
|
|
5
|
+
|
|
6
|
+
/** Pushes away from neighbors closer than `threshold`, by `decayCoefficient / distance²`, at most `maxAcceleration` each. */
|
|
7
|
+
export function separationSteering(
|
|
8
|
+
entity: Kinematic,
|
|
9
|
+
neighbors: Pick<Kinematic, "position">[],
|
|
10
|
+
options?: { threshold?: number; decayCoefficient?: number },
|
|
11
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_EPSILON: number
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Applies a steering for `dt` seconds: the linear part changes `velocity` (at
|
|
7
|
+
* most `maxSpeed`), `position` and `orientation` (where it goes); the angular
|
|
8
|
+
* part changes `angularSpeed` and `orientation`. `null` returns the entity.
|
|
9
|
+
*/
|
|
10
|
+
export function integrate<TEntity extends Kinematic>(
|
|
11
|
+
entity: TEntity,
|
|
12
|
+
steering: SteeringOutput | null,
|
|
13
|
+
dt: number,
|
|
14
|
+
):
|
|
15
|
+
| TEntity
|
|
16
|
+
| {
|
|
17
|
+
acceleration?: Kinematic["position"]
|
|
18
|
+
velocity?: Kinematic["position"]
|
|
19
|
+
position?: Kinematic["position"]
|
|
20
|
+
orientation?: number
|
|
21
|
+
angularSpeed?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Adds up `[steering, weight]` pairs, capped at `maxAcceleration` and `maxAngularAcceleration`. `null` if all are. */
|
|
25
|
+
export function blend(
|
|
26
|
+
entity: Pick<Kinematic, "maxAcceleration" | "maxAngularAcceleration">,
|
|
27
|
+
weightedSteerings: [SteeringOutput | null, number][],
|
|
28
|
+
): SteeringOutput | null
|
|
29
|
+
|
|
30
|
+
/** The first steering asking for more than `epsilon`, most urgent first; else the last. */
|
|
31
|
+
export function prioritize(
|
|
32
|
+
steerings: (SteeringOutput | null)[],
|
|
33
|
+
options?: { epsilon?: number },
|
|
34
|
+
): SteeringOutput | null
|
|
@@ -1,11 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
DynamicMovement,
|
|
3
|
+
Kinematic,
|
|
4
|
+
Steering,
|
|
5
|
+
SteeringOutput,
|
|
6
|
+
} from "../../../index"
|
|
2
7
|
|
|
3
8
|
export const DEFAULT_WANDER_OFFSET: number
|
|
4
9
|
export const DEFAULT_WANDER_RADIUS: number
|
|
5
10
|
|
|
11
|
+
export interface WanderOptions {
|
|
12
|
+
wanderOffset?: number
|
|
13
|
+
wanderRadius?: number
|
|
14
|
+
/** A seeded generator (`createRng`), to keep a simulation deterministic. */
|
|
15
|
+
rng?: { randomBinomial(): number }
|
|
16
|
+
}
|
|
17
|
+
|
|
6
18
|
/** Seeks a random point on a circle `wanderOffset` ahead. */
|
|
7
19
|
export function wander<TEntity extends Kinematic>(
|
|
8
20
|
entity: TEntity,
|
|
9
21
|
dt: number,
|
|
10
|
-
options?:
|
|
22
|
+
options?: WanderOptions,
|
|
11
23
|
): Steering<TEntity, DynamicMovement>
|
|
24
|
+
|
|
25
|
+
/** The acceleration `wander` applies. */
|
|
26
|
+
export function wanderSteering(
|
|
27
|
+
entity: Kinematic,
|
|
28
|
+
options?: WanderOptions,
|
|
29
|
+
): SteeringOutput | null
|
package/types/index.d.ts
CHANGED
|
@@ -188,3 +188,14 @@ export type KinematicMovement = {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
export type DynamicMovement = KinematicMovement & { acceleration?: Vector3 }
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* What a dynamic steering behavior asks for, before anything moves: a linear
|
|
194
|
+
* acceleration, an angular one (radians a second²), or both. `null` asks for
|
|
195
|
+
* nothing. Apply it with `integrate`, or combine several first with `blend` and
|
|
196
|
+
* `prioritize` (`@inglorious/engine/ai/movement/dynamic/steering.js`).
|
|
197
|
+
*/
|
|
198
|
+
export interface SteeringOutput {
|
|
199
|
+
linear?: Vector3
|
|
200
|
+
angular?: number
|
|
201
|
+
}
|