@inglorious/engine 24.15.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/src/behaviors/film.js +269 -0
- 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/behaviors/film.d.ts +124 -0
- 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
|
+
}
|