forked from beneater/boids
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboids.js
858 lines (755 loc) · 21.2 KB
/
boids.js
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// Simulation globals
// Size of canvas. These get updated to fill the whole browser.
let width = 150;
let height = 150;
let running = true;
// ---------------------
// Simulation constants
// ---------------------
const NUM_BOIDS = 50;
const NUM_PREDATORS = 1;
const NUM_LEADERS = 1;
const NUM_OBSTACLES = 4;
const NUM_WIND_CIRCLES = 4;
const BOID_SPEED_LIMIT = 6;
const LEADER_SEES_PREDATOR_MULT = 3;
const PREDATOR_SEES_BOID_MULT = 1.5;
const PREDATOR_ALIGNMENT_CORRECTION = 0.5;
const BOID_SEES_LEADER_MULT = 3;
const MARGIN = 200;
const TURN_FACTOR = 2;
const TIME_INDIRECT_COMM = 500;
const TIME_UPDATE_PLOTS = 1000;
const MAX_PLOT_LEN = 10000; // Max length of simulations data arrays
// Colors constants
const YELLOW = "#f4df55";
const BLUE = "#558cf4";
const GREEN = "#63D471";
const RED = "#d8315b";
const ALPHA_BLUE = "#558cf466";
const ALPHA_GREEN = "#63D47166";
const ALPHA_RED = "#d8315b66";
const ALPHA_YELLOW = "#f4df5566";
// Colors config
const boidsColors = {
normalBoid: BLUE,
predatorBoid: RED,
leaderBoid: YELLOW,
};
const boidsTrails = {
normalBoid: ALPHA_BLUE,
predatorBoid: ALPHA_RED,
leaderBoid: ALPHA_YELLOW,
};
// ---------------
// Simulation data
// ---------------
let globalVector = { x: 0, y: 0, dx: 0, dy: 0 };
let extensionHistory = [];
let boidsAliveHistory = [];
// ---------------------
// Simulation parameters
// ---------------------
let baseVisualRange = 75;
let centeringFactor = 0.005; // Coherence
let avoidFactor = 0.05; // Separation
let matchingFactor = 0.05; // Alignment
// Predation
let predationFactor = 0.01; // How much the predator will pursue the flock
let avoidPredatorFactor = 0.05; // How much the flock try to avoid the predator
var eatRange = 40; // How far the predator can get its prey
// =======
// OPTIONS
// =======
// Visual
let hideTrail = false;
let seeGlobalVector = false;
// Mouse leader
let useMouseLeader = false;
let mouseLeaderWeight = 0.3; // How much the boids will go towards the leader
// Obstacles and Turbulence
let useObstaclesTurb = false;
// Leader
let useLeaders = false;
let followLeaderFactor = 0.5;
let leaderRadius = 100;
// Predator
let usePredators = false;
// Interaction
let mouse = {
x: 0,
y: 0,
dx: 0,
dy: 0,
};
// Entities
let boids = [];
let predatorBoids = [];
let leaderBoids = [];
let obstacles = [];
let arrows = [];
let windCircles = [];
function mouse_position(e) {
mouse.x = e.clientX;
mouse.y = e.clientY - 220;
}
function initBoids() {
boids = [];
for (var i = 0; i < NUM_BOIDS; i += 1) {
boids[boids.length] = {
x: Math.random() * width,
y: Math.random() * height,
dx: Math.random() * 10 - 5,
dy: Math.random() * 10 - 5,
history: [],
type: "normalBoid",
};
}
}
function initPredators() {
predatorBoids = [];
for (var i = 0; i < NUM_PREDATORS; i += 1) {
predatorBoids[predatorBoids.length] = {
x: Math.random() * width,
y: Math.random() * height,
dx: Math.random() * 10 - 5,
dy: Math.random() * 10 - 5,
history: [],
type: "predatorBoid",
};
}
}
function initLeaders() {
leaderBoids = [];
for (var i = 0; i < NUM_LEADERS; i += 1) {
dx0 = Math.random() * 10 - 5;
dy0 = Math.random() * 10 - 5;
x0 = Math.random() * width;
y0 = Math.random() * height;
leaderBoids[leaderBoids.length] = {
x: x0,
y: y0,
dx: dx0,
dy: dy0,
arrow_dx: dx0,
arrow_dy: dy0,
arrow_x: x0,
arrow_y: y0,
history: [],
type: "leaderBoid",
};
}
}
// Used by the leaders to leave an indication on the map, hinting boids where to go
function updateArrows() {
for (leader of leaderBoids) {
leader.arrow_dx = leader.dx;
leader.arrow_dy = leader.dy;
leader.arrow_x = leader.x;
leader.arrow_y = leader.y;
}
}
function reactToArrow(boid) {
if (!useLeaders) return;
for (leader of leaderBoids) {
// If boid can see an arrow
if (
positionDistance(boid.x, boid.y, leader.arrow_x, leader.arrow_y) <
baseVisualRange
) {
boid.dx += leader.arrow_dx;
boid.dy += leader.arrow_dy;
}
}
}
function distance(A, B) {
if (!A || !B) return;
return Math.sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
function positionDistance(x1, y1, x2, y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
function initObstacles() {
obstacles = [];
for (var i = 0; i < NUM_OBSTACLES; i += 1) {
obstacles[obstacles.length] = {
x: Math.random() * width,
y: Math.random() * height,
r: Math.random() * 40,
};
}
}
function initWindCircles() {
windCircles = [];
for (var i = 0; i < NUM_WIND_CIRCLES; i += 1) {
windCircles[windCircles.length] = {
x: Math.random() * width,
y: Math.random() * height,
r: Math.random() * 40,
dx: Math.random() * 20,
dy: Math.random() * 20,
};
}
}
// TODO: This is naive and inefficient.
function nClosestBoids(boid, n) {
// Make a copy
const sorted = boids.slice();
// Sort the copy by distance from `boid`
sorted.sort((a, b) => distance(boid, a) - distance(boid, b));
// Return the `n` closest
return sorted.slice(1, n + 1);
}
// Called initially and whenever the window resizes to update the canvas
// size and width/height variables.
function sizeCanvas() {
const canvas = document.getElementById("boids");
// const canvas = document.getElementById("metrics");
width = canvas.parentElement.clientWidth;
height = canvas.parentElement.clientHeight;
canvas.width = width;
canvas.height = height - 100;
}
// Constrain a boid to within the window. If it gets too close to an edge,
// nudge it back in and reverse its direction.
function keepWithinBounds(boid) {
if (boid.x < MARGIN) {
boid.dx += TURN_FACTOR;
}
if (boid.x > width - MARGIN) {
boid.dx -= TURN_FACTOR;
}
if (boid.y < MARGIN) {
boid.dy += TURN_FACTOR;
}
if (boid.y > height - MARGIN) {
boid.dy -= TURN_FACTOR;
}
}
// Find the center of mass of the other boids and adjust velocity slightly to
// point towards the center of mass.
// [Deprecated: If mouse leader is used, go towards it with a greater weight.]
function flyTowardsCenter(boid) {
let centerX = 0;
let centerY = 0;
let numNeighbors = 0;
for (otherBoid of boids) {
if (distance(boid, otherBoid) < baseVisualRange) {
centerX += otherBoid.x;
centerY += otherBoid.y;
numNeighbors += 1;
}
}
if (numNeighbors) {
centerX = centerX / numNeighbors;
centerY = centerY / numNeighbors;
boid.dx += (centerX - boid.x) * centeringFactor;
boid.dy += (centerY - boid.y) * centeringFactor;
}
}
function flyTowardsLeader(boid) {
if (!useLeaders) return;
let targetX = 0;
let targetY = 0;
let foundLeader = false;
for (leader of leaderBoids) {
if (distance(boid, leader) < baseVisualRange * BOID_SEES_LEADER_MULT) {
targetX = leader.x;
targetY = leader.y;
foundLeader = true;
break;
}
}
if (foundLeader) {
boid.dx += (targetX - boid.x) * followLeaderFactor;
boid.dy += (targetY - boid.y) * followLeaderFactor;
}
}
// The predator flies towards the center of the flock it is hunting
// The predator has a larger visual range
function flyTowardsCenterPredator(boid) {
if (!usePredators) return;
let centerX = 0;
let centerY = 0;
let numTargets = 0;
for (let boid of boids) {
if (distance(boid, boid) < baseVisualRange * PREDATOR_SEES_BOID_MULT) {
centerX += boid.x;
centerY += boid.y;
numTargets += 1;
}
}
if (numTargets > 0) {
centerX = centerX / numTargets;
centerY = centerY / numTargets;
boid.dx += (centerX - boid.x) * predationFactor;
boid.dy += (centerY - boid.y) * predationFactor;
}
}
// Move away from other boids that are too close to avoid colliding
function avoidOthers(boid) {
const minDistance = 20; // The distance to stay away from other boids
let moveX = 0;
let moveY = 0;
for (let otherBoid of boids) {
if (otherBoid !== boid) {
if (distance(boid, otherBoid) < minDistance) {
moveX += boid.x - otherBoid.x;
moveY += boid.y - otherBoid.y;
}
}
}
boid.dx += moveX * avoidFactor;
boid.dy += moveY * avoidFactor;
}
// Run away from predators
function avoidPredators(boid) {
if (!usePredators) return;
let moveX = 0;
let moveY = 0;
for (let predator of predatorBoids) {
if (
distance(boid, predator) <
(boid.type == "leaderBoid"
? baseVisualRange * LEADER_SEES_PREDATOR_MULT
: baseVisualRange)
) {
moveX += boid.x - predator.x;
moveY += boid.y - predator.y;
}
}
boid.dx += moveX * avoidPredatorFactor;
boid.dy += moveY * avoidPredatorFactor;
}
// Boids will try to avoid obstacles, the near it gets more it will avoid.
function avoidObstacle(boid) {
if (!useObstaclesTurb) return;
let moveX = 0;
let moveY = 0;
let changeDirectionFactor = 0;
for (let obstacle of obstacles) {
if (distance(boid, obstacle) - obstacle.r < baseVisualRange) {
moveX += boid.x - obstacle.x;
moveY += boid.y - obstacle.y;
changeDirectionFactor +=
20 / (distance(boid, obstacle) - obstacle.r + 1e-3);
}
}
boid.dx += moveX * avoidFactor * changeDirectionFactor;
boid.dy += moveY * avoidFactor * changeDirectionFactor;
}
// The windCircle adds a factor to each direction of the boid
function passThroughWindCircle(boid) {
if (!useObstaclesTurb) return;
let moveX = 0;
let moveY = 0;
let aerodinamicFactor = 1;
for (let windCircle of windCircles) {
if (distance(boid, windCircle) < windCircle.r) {
moveX += windCircle.dx;
moveY += windCircle.dy;
}
}
boid.dx += moveX * aerodinamicFactor;
boid.dy += moveY * aerodinamicFactor;
}
// Find the average velocity (speed and direction) of the other boids and
// adjust velocity slightly to match.
function matchVelocity(boid) {
let avgDX = 0;
let avgDY = 0;
let numNeighbors = 0;
for (let otherBoid of boids) {
if (distance(boid, otherBoid) < baseVisualRange) {
avgDX += otherBoid.dx;
avgDY += otherBoid.dy;
numNeighbors += 1;
}
}
if (numNeighbors) {
avgDX = avgDX / numNeighbors;
avgDY = avgDY / numNeighbors;
boid.dx += (avgDX - boid.dx) * matchingFactor;
boid.dy += (avgDY - boid.dy) * matchingFactor;
}
}
function eatBoids(predator) {
if (!usePredators) return;
let boidsToEat = [];
for (let i = 0; i < boids.length; i++) {
let otherBoid = boids[i];
if (distance(predator, otherBoid) < eatRange) {
boidsToEat.push(i);
}
}
for (let boidEaten of boidsToEat) {
boids[boidEaten] = boids.pop();
}
}
// Predator match the average velocity of the flock it is hunting
function matchVelocityPredator(boid) {
let avgDX = 0;
let avgDY = 0;
let numTargets = 0;
for (let otherBoid of boids) {
if (distance(boid, otherBoid) < baseVisualRange * PREDATOR_SEES_BOID_MULT) {
avgDX += otherBoid.dx;
avgDY += otherBoid.dy;
numTargets += 1;
}
}
if (numTargets) {
avgDX = avgDX / numTargets;
avgDY = avgDY / numTargets;
boid.dx +=
(avgDX - boid.dx) * matchingFactor * PREDATOR_ALIGNMENT_CORRECTION;
boid.dy +=
(avgDY - boid.dy) * matchingFactor * PREDATOR_ALIGNMENT_CORRECTION;
}
}
// Speed will naturally vary in flocking behavior, but real animals can't go
// arbitrarily fast.
function limitSpeed(boid) {
const speed = Math.sqrt(boid.dx * boid.dx + boid.dy * boid.dy);
if (speed > BOID_SPEED_LIMIT) {
boid.dx = (boid.dx / speed) * BOID_SPEED_LIMIT;
boid.dy = (boid.dy / speed) * BOID_SPEED_LIMIT;
}
}
function drawTriangle(ctx, x, y, dx, dy, fillStyle, mult = 1) {
const angle = Math.atan2(dy, dx);
ctx.translate(x, y);
ctx.rotate(angle);
ctx.translate(-x, -y);
ctx.fillStyle = fillStyle;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x - 15 * mult, y + 5 * mult);
ctx.lineTo(x - 15 * mult, y - 5 * mult);
ctx.lineTo(x, y);
ctx.fill();
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
function drawBoid(ctx, boid) {
if (!boid) return;
drawTriangle(ctx, boid.x, boid.y, boid.dx, boid.dy, boidsColors[boid.type]);
if (!hideTrail) {
ctx.strokeStyle = boidsTrails[boid.type];
ctx.beginPath();
ctx.moveTo(boid.history[0][0], boid.history[0][1]);
for (const point of boid.history) {
ctx.lineTo(point[0], point[1]);
}
ctx.stroke();
}
// If leader, and not in circle-movement mode,
// draw arrow indicating last taken direction
if (boid.type == "leaderBoid") {
drawTriangle(
ctx,
boid.arrow_x,
boid.arrow_y,
boid.arrow_dx,
boid.arrow_dy,
boidsTrails[boid.type]
);
}
}
function drawObstacles(ctx) {
if (!useObstaclesTurb) return;
obstacles.forEach(function (obstacle) {
ctx.beginPath();
ctx.arc(obstacle.x, obstacle.y, obstacle.r, 0, Math.PI * 2);
ctx.fillStyle = "black";
ctx.fill();
});
}
function drawWindCircle(ctx) {
if (!useObstaclesTurb) return;
windCircles.forEach(function (windCircle) {
ctx.beginPath();
ctx.arc(windCircle.x, windCircle.y, windCircle.r, 0, Math.PI * 2);
ctx.fillStyle = ALPHA_YELLOW;
ctx.fill();
// ctx.fill("black");
});
}
function drawMouseLeader(ctx, mouse) {
ctx.fillStyle = YELLOW;
// Draw square
ctx.beginPath();
ctx.moveTo(mouse.x - 5, mouse.y - 5);
ctx.lineTo(mouse.x - 5, mouse.y + 5);
ctx.lineTo(mouse.x + 5, mouse.y + 5);
ctx.lineTo(mouse.x + 5, mouse.y - 5);
ctx.fill();
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
function drawGlobalVector(ctx) {
drawTriangle(
ctx,
globalVector.x,
globalVector.y,
globalVector.dx,
globalVector.dy,
ALPHA_GREEN,
2
);
}
function updateMetrics() {
// Calculate vectorized center of the flock
globalVector = { x: 0, y: 0, dx: 0, dy: 0 };
let ext = 0;
boids_copy = boids.copyWithin();
boids_copy = boids_copy.filter((el) => el !== undefined);
for (boid of boids_copy) {
globalVector.dx += boid.dx;
globalVector.dy += boid.dy;
globalVector.x += boid.x;
globalVector.y += boid.y;
}
globalVector.dx /= boids.length;
globalVector.dy /= boids.length;
globalVector.x /= boids.length;
globalVector.y /= boids.length;
for (boid of boids_copy) {
ext += Math.sqrt(
(globalVector.x - boid.x) ** 2 + (globalVector.y - boid.y) ** 2
);
}
ext /= boids.length;
extensionHistory.push(ext);
boidsAliveHistory.push(boids.length);
if (extensionHistory.length > MAX_PLOT_LEN) {
extensionHistory.splice(0, 1);
}
if (boidsAliveHistory.length > MAX_PLOT_LEN) {
boidsAliveHistory.splice(0, 1);
}
}
// -------------------
// Main animation loop
// -------------------
function animationLoop() {
// Update each boid
if (!running) {
window.requestAnimationFrame(animationLoop);
return;
}
let valid_boids = boids.filter((el) => el !== undefined);
for (let boid of valid_boids) {
// Update the velocities according to each rule
keepWithinBounds(boid);
flyTowardsCenter(boid);
flyTowardsLeader(boids);
avoidOthers(boid);
avoidPredators(boid);
reactToArrow(boid);
matchVelocity(boid);
limitSpeed(boid);
avoidObstacle(boid);
passThroughWindCircle(boid);
// Update the position based on the current velocity
boid.x += boid.dx;
boid.y += boid.dy;
boid.history.push([boid.x, boid.y]);
boid.history = boid.history.slice(-50);
}
for (let predatorBoid of predatorBoids) {
keepWithinBounds(predatorBoid);
flyTowardsCenterPredator(predatorBoid);
matchVelocityPredator(predatorBoid);
limitSpeed(predatorBoid);
avoidObstacle(predatorBoid);
passThroughWindCircle(predatorBoid);
eatBoids(predatorBoid);
predatorBoid.x += predatorBoid.dx;
predatorBoid.y += predatorBoid.dy;
predatorBoid.history.push([predatorBoid.x, predatorBoid.y]);
predatorBoid.history = predatorBoid.history.slice(-50);
}
for (let leader of leaderBoids) {
keepWithinBounds(leader);
// Leader avoids predator with higher visual range
avoidPredators(leader);
flyTowardsCenter(leader);
limitSpeed(leader);
leader.x += leader.dx;
leader.y += leader.dy;
leader.history.push([leader.x, leader.y]);
leader.history = leader.history.slice(-50);
}
// Clear the canvas and redraw all the boids in their current positions
const ctx = document.getElementById("boids").getContext("2d");
ctx.clearRect(0, 0, width, height);
for (boid of boids) {
drawBoid(ctx, boid);
}
if (useMouseLeader) drawMouseLeader(ctx, mouse);
if (usePredators) {
for (predatorBoid of predatorBoids) {
drawBoid(ctx, predatorBoid);
}
}
if (useLeaders) {
for (leaderBoid of leaderBoids) {
drawBoid(ctx, leaderBoid);
}
}
if (useObstaclesTurb) {
drawObstacles(ctx);
drawWindCircle(ctx);
}
updateMetrics();
if (seeGlobalVector) {
drawGlobalVector(ctx);
}
// Schedule the next frame
window.requestAnimationFrame(animationLoop);
}
function initAll() {
// Randomly distribute the boids to start
initBoids();
initPredators();
initLeaders();
initObstacles();
initWindCircles();
extensionHistory = [];
boidsAliveHistory = [];
}
window.onload = () => {
// Make sure the canvas always fills the whole window
window.addEventListener("resize", sizeCanvas, false);
sizeCanvas();
// Randomly distribute the boids and obstacles to start
initAll();
// Init simulation plots
drawSimPlots();
// Schedule the main animation loop
window.requestAnimationFrame(animationLoop);
// Update the indicating arrows left by the leaders
window.setInterval(updateArrows, TIME_INDIRECT_COMM);
// Define sliders behaviors
document.getElementById("slider-coherence").value = centeringFactor * 1000;
document.getElementById("slider-coherence").oninput = (ev) => {
//TODO: Find maximum value to centeringFactor, after 0.05 the boid already display a high centering behavior
centeringFactor = ev.target.value / 1000;
};
document.getElementById("slider-separation").value = avoidFactor * 100;
document.getElementById("slider-separation").oninput = (ev) => {
avoidFactor = ev.target.value / 100;
};
document.getElementById("slider-alignment").value = matchingFactor * 100;
document.getElementById("slider-alignment").oninput = (ev) => {
matchingFactor = ev.target.value / 100;
};
document.getElementById("slider-visual-range").value = baseVisualRange;
document.getElementById("slider-visual-range").oninput = (ev) => {
baseVisualRange = ev.target.value;
};
// Predator
document.getElementById("slider-predator-coherence").value =
predationFactor * 1000;
document.getElementById("slider-predator-coherence").oninput = (ev) => {
predationFactor = ev.target.value / 1000;
};
document.getElementById("slider-avoid-predator").value =
avoidPredatorFactor * 100;
document.getElementById("slider-avoid-predator").oninput = (ev) => {
avoidPredatorFactor = ev.target.value / 100;
};
// Toggles
document.getElementById("toggle-obstacles").value = useObstaclesTurb;
document.getElementById("toggle-obstacles").oninput = (ev) => {
useObstaclesTurb = ev.target.checked;
};
document.getElementById("toggle-leaders").value = useLeaders;
document.getElementById("toggle-leaders").oninput = (ev) => {
useLeaders = ev.target.checked;
};
document.getElementById("toggle-predators").value = usePredators;
document.getElementById("toggle-predators").oninput = (ev) => {
usePredators = ev.target.checked;
};
document.getElementById("toggle-global-vector").value = seeGlobalVector;
document.getElementById("toggle-global-vector").oninput = (ev) => {
seeGlobalVector = ev.target.checked;
};
document.getElementById("toggle-trail").value = hideTrail;
document.getElementById("toggle-trail").oninput = (ev) => {
hideTrail = ev.target.checked;
};
document.getElementById("reset-button").onclick = (ev) => {
initAll();
};
document.getElementById("pause-button").onclick = (ev) => {
running = !running;
if (!running) ev.target.text = "Play";
else ev.target.text = "Pause";
};
};
// ---------------------
// Draw simulation plots
// ---------------------
function drawSimPlots() {
let extensionTrace = {
y: extensionHistory,
mode: "lines",
line: { color: GREEN },
name: "Extension",
};
let countBoids = {
y: boidsAliveHistory,
xaxis: "x2",
yaxis: "y2",
mode: "lines",
line: { color: RED },
name: "Boids alive",
};
let layout = {
paper_bgcolor: "rgba(0,0,0,0)",
plot_bgcolor: "rgba(0, 0, 0, 0)",
xaxis: {
tickcolor: "grey",
tickfont: {
color: "grey",
},
gridcolor: "grey",
linecolor: "grey",
},
yaxis: {
tickcolor: "grey",
tickfont: {
color: "grey",
},
gridcolor: "grey",
linecolor: "grey",
},
xaxis2: {
tickcolor: "grey",
tickfont: {
color: "grey",
},
gridcolor: "grey",
linecolor: "grey",
},
yaxis2: {
tickcolor: "grey",
tickfont: {
color: "grey",
},
gridcolor: "grey",
linecolor: "grey",
},
legend: {
font: {
color: "grey",
},
},
grid: { rows: 2, columns: 1, pattern: "independent" },
};
Plotly.newPlot("charts", [extensionTrace, countBoids], layout);
var plotInterval = setInterval(function () {
Plotly.update(charts, { y: [extensionHistory, boidsAliveHistory] });
}, TIME_UPDATE_PLOTS);
}