-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteer.test.js
More file actions
285 lines (249 loc) · 9.68 KB
/
Steer.test.js
File metadata and controls
285 lines (249 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import {describe, it, expect, vi} from 'vitest';
vi.mock('@zakkster/lite-vec', async () => {
const actual = await import('./Vec.js');
return actual;
});
import {vec2} from './Vec.js';
import {
seek, arrive, flee, wander, followFlow,
separation, alignment, cohesion,
wrap, bounce, avoidEdges,
orbit, swirlToward, curl,
projectToSegment, followPath,
} from './Steer.js';
const force = vec2.create();
describe('🧭 lite-steer', () => {
describe('seek()', () => {
it('steers toward target', () => {
const pos = vec2.create(0, 0);
const vel = vec2.create(0, 0);
const target = vec2.create(100, 0);
seek(force, pos, vel, target, 10, 1);
expect(force[0]).toBeGreaterThan(0);
});
it('output is zero when at target', () => {
const pos = vec2.create(50, 50);
const vel = vec2.create(10, 0);
seek(force, pos, vel, pos, 10, 1);
// When pos === target, desired direction is zero-length
// normalize produces (0,0), so force = (0,0) - vel
expect(force[0]).toBeCloseTo(-10);
});
});
describe('arrive()', () => {
it('decelerates near target', () => {
const pos = vec2.create(95, 0);
const vel = vec2.create(0, 0);
const target = vec2.create(100, 0);
arrive(force, pos, vel, target, 100, 50);
// 5 units away with slowRadius 50 → speed = 100 * 5/50 = 10
expect(force[0]).toBeCloseTo(10);
});
it('returns zero when at target', () => {
const pos = vec2.create(100, 100);
arrive(force, pos, vec2.create(), pos, 100, 50);
expect(force[0]).toBe(0);
expect(force[1]).toBe(0);
});
});
describe('flee()', () => {
it('steers away from threat', () => {
const pos = vec2.create(10, 0);
const threat = vec2.create(0, 0);
flee(force, pos, vec2.create(), threat, 10, 50);
expect(force[0]).toBeGreaterThan(0); // moving away
});
it('ignores threats beyond panicDist', () => {
const pos = vec2.create(100, 0);
const threat = vec2.create(0, 0);
flee(force, pos, vec2.create(), threat, 10, 50);
expect(force[0]).toBe(0);
expect(force[1]).toBe(0);
});
});
describe('wander()', () => {
it('returns updated angle', () => {
const vel = vec2.create(1, 0);
const rng = {next: () => 0.5}; // neutral randomness
const newAngle = wander(force, vel, 10, 0.5, 0, rng);
expect(typeof newAngle).toBe('number');
});
it('produces non-zero force', () => {
const vel = vec2.create(5, 0);
const rng = {next: () => 0.7};
wander(force, vel, 20, 1.0, 0, rng);
expect(vec2.mag(force)).toBeGreaterThan(0);
});
it('is deterministic with same RNG', () => {
const vel = vec2.create(5, 0);
let i = 0;
const rng1 = {next: () => [0.3, 0.7, 0.1][i++ % 3]};
wander(force, vel, 20, 1.0, 0, rng1);
const x1 = force[0], y1 = force[1];
i = 0;
const rng2 = {next: () => [0.3, 0.7, 0.1][i++ % 3]};
wander(force, vel, 20, 1.0, 0, rng2);
expect(force[0]).toBeCloseTo(x1);
expect(force[1]).toBeCloseTo(y1);
});
});
describe('followFlow()', () => {
it('steers along flow direction', () => {
const pos = vec2.create(50, 50);
const vel = vec2.create(0, 0);
const fieldFn = (out, x, y) => {
out[0] = 1;
out[1] = 0;
}; // rightward flow
followFlow(force, pos, vel, fieldFn, 10, 1);
expect(force[0]).toBeGreaterThan(0);
});
});
describe('separation()', () => {
it('pushes away from close neighbors', () => {
const pos = vec2.create(50, 50);
const neighbors = [
{pos: vec2.create(55, 50)},
{pos: vec2.create(45, 50)},
];
separation(force, pos, neighbors, 30);
// Symmetric neighbors should roughly cancel on X
expect(Math.abs(force[0])).toBeLessThan(1);
});
it('returns zero with no neighbors in range', () => {
const pos = vec2.create(0, 0);
const neighbors = [{pos: vec2.create(1000, 1000)}];
separation(force, pos, neighbors, 10);
expect(force[0]).toBe(0);
expect(force[1]).toBe(0);
});
});
describe('alignment()', () => {
it('steers toward average heading', () => {
const vel = vec2.create(0, 0);
const neighbors = [
{vel: vec2.create(10, 0)},
{vel: vec2.create(10, 0)},
];
alignment(force, vel, neighbors);
expect(force[0]).toBeGreaterThan(0);
});
});
describe('cohesion()', () => {
it('steers toward center of mass', () => {
const pos = vec2.create(0, 0);
const neighbors = [
{pos: vec2.create(10, 0)},
{pos: vec2.create(20, 0)},
];
cohesion(force, pos, neighbors);
expect(force[0]).toBeGreaterThan(0); // center of mass is at (15, 0)
});
});
describe('wrap()', () => {
it('wraps left edge', () => {
const pos = vec2.create(-5, 50);
wrap(pos, 100, 100);
expect(pos[0]).toBe(95);
});
it('wraps right edge', () => {
const pos = vec2.create(105, 50);
wrap(pos, 100, 100);
expect(pos[0]).toBe(5);
});
it('wraps top edge', () => {
const pos = vec2.create(50, -10);
wrap(pos, 100, 100);
expect(pos[1]).toBe(90);
});
});
describe('bounce()', () => {
it('reverses velocity on left wall', () => {
const pos = vec2.create(-1, 50);
const vel = vec2.create(-10, 0);
bounce(pos, vel, 100, 100, 1);
expect(vel[0]).toBeGreaterThan(0);
expect(pos[0]).toBe(0);
});
it('applies restitution', () => {
const pos = vec2.create(-1, 50);
const vel = vec2.create(-10, 0);
bounce(pos, vel, 100, 100, 0.5);
expect(vel[0]).toBeCloseTo(5);
});
});
describe('avoidEdges()', () => {
it('pushes right when near left edge', () => {
avoidEdges(force, vec2.create(10, 50), 40, 800, 600, 1);
expect(force[0]).toBeGreaterThan(0);
});
it('returns zero when far from edges', () => {
avoidEdges(force, vec2.create(400, 300), 40, 800, 600, 1);
expect(force[0]).toBe(0);
expect(force[1]).toBe(0);
});
});
describe('orbit()', () => {
it('rotates position around center', () => {
const pos = vec2.create(10, 0);
const center = vec2.create(5, 0);
orbit(pos, pos, center, Math.PI / 2, 1); // 90° in 1 second
expect(pos[0]).toBeCloseTo(5);
expect(pos[1]).toBeCloseTo(5);
});
});
describe('swirlToward()', () => {
it('produces force with both radial and tangential components', () => {
const pos = vec2.create(0, 0);
const target = vec2.create(100, 0);
swirlToward(force, pos, target, 10, 50);
expect(force[0]).toBeGreaterThan(0); // radial: toward target
expect(force[1]).not.toBe(0); // tangential: perpendicular spin
});
});
describe('curl()', () => {
it('computes divergence-free field from noise', () => {
const noiseFn = (x, y) => Math.sin(x) * Math.cos(y);
curl(force, 1, 1, noiseFn);
expect(Number.isFinite(force[0])).toBe(true);
expect(Number.isFinite(force[1])).toBe(true);
});
});
describe('projectToSegment()', () => {
it('projects onto the middle of a segment', () => {
const out = vec2.create();
projectToSegment(out, vec2.create(5, 10), vec2.create(0, 0), vec2.create(10, 0));
expect(out[0]).toBeCloseTo(5);
expect(out[1]).toBeCloseTo(0);
});
it('clamps to segment start', () => {
const out = vec2.create();
projectToSegment(out, vec2.create(-5, 0), vec2.create(0, 0), vec2.create(10, 0));
expect(out[0]).toBeCloseTo(0);
});
it('clamps to segment end', () => {
const out = vec2.create();
projectToSegment(out, vec2.create(15, 0), vec2.create(0, 0), vec2.create(10, 0));
expect(out[0]).toBeCloseTo(10);
});
});
describe('followPath()', () => {
it('steers toward path', () => {
const pos = vec2.create(5, 20);
const vel = vec2.create(0, 0);
const path = [vec2.create(0, 0), vec2.create(100, 0)];
followPath(force, pos, vel, path, 10);
expect(force[1]).toBeLessThan(0); // should steer downward toward path
});
it('handles single-segment path', () => {
const pos = vec2.create(50, 50);
followPath(force, pos, vec2.create(), [vec2.create(0, 0), vec2.create(100, 0)], 10);
expect(Number.isFinite(force[0])).toBe(true);
});
it('returns zero for path with < 2 points', () => {
followPath(force, vec2.create(), vec2.create(), [vec2.create(0, 0)], 10);
expect(force[0]).toBe(0);
expect(force[1]).toBe(0);
});
});
});