-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboids.js
128 lines (107 loc) · 2.99 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
const canvas = document.getElementById("boidsCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const protectedRangeSquared = 50 * 50;
const visibleRangeSquared = 100 * 100;
var avoidFactor = 0.05;
var matchingFactor = 0.05;
var centeringFactor = 0.005;
var turnFactor = 0.2;
const leftMargin = 100;
const rightMargin = canvas.width - 100;
const topMargin = 100;
const bottomMargin = canvas.height - 100;
var maxSpeed = 5;
var minSpeed = 2;
class Boid {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = Math.random() * 2 - 1;
this.vy = Math.random() * 2 - 1;
}
update(flock) {
let xpos_avg = 0,
ypos_avg = 0,
xvel_avg = 0,
yvel_avg = 0,
neighboring_boids = 0;
let close_dx = 0,
close_dy = 0;
flock.forEach((otherBoid) => {
if (otherBoid !== this) {
const dx = this.x - otherBoid.x;
const dy = this.y - otherBoid.y;
const squared_distance = dx * dx + dy * dy;
if (
Math.abs(dx) < visibleRangeSquared &&
Math.abs(dy) < visibleRangeSquared
) {
if (squared_distance < protectedRangeSquared) {
close_dx += dx;
close_dy += dy;
} else if (squared_distance < visibleRangeSquared) {
xpos_avg += otherBoid.x;
ypos_avg += otherBoid.y;
xvel_avg += otherBoid.vx;
yvel_avg += otherBoid.vy;
neighboring_boids++;
}
}
}
});
if (neighboring_boids > 0) {
xpos_avg /= neighboring_boids;
ypos_avg /= neighboring_boids;
xvel_avg /= neighboring_boids;
yvel_avg /= neighboring_boids;
this.vx +=
(xpos_avg - this.x) * centeringFactor +
(xvel_avg - this.vx) * matchingFactor;
this.vy +=
(ypos_avg - this.y) * centeringFactor +
(yvel_avg - this.vy) * matchingFactor;
}
this.vx += close_dx * avoidFactor;
this.vy += close_dy * avoidFactor;
if (this.x < leftMargin) this.vx += turnFactor;
if (this.x > rightMargin) this.vx -= turnFactor;
if (this.y < topMargin) this.vy += turnFactor;
if (this.y > bottomMargin) this.vy -= turnFactor;
const speed = Math.sqrt(this.vx * this.vx + this.vy * this.vy);
if (speed < minSpeed) {
this.vx = (this.vx / speed) * minSpeed;
this.vy = (this.vy / speed) * minSpeed;
} else if (speed > maxSpeed) {
this.vx = (this.vx / speed) * maxSpeed;
this.vy = (this.vy / speed) * maxSpeed;
}
this.x += this.vx;
this.y += this.vy;
if (this.x > canvas.width) this.x = 0;
if (this.y > canvas.height) this.y = 0;
if (this.x < 0) this.x = canvas.width;
if (this.y < 0) this.y = canvas.height;
}
draw() {
ctx.fillStyle = "black";
ctx.beginPath();
ctx.arc(this.x, this.y, 4, 0, Math.PI * 2);
ctx.fill();
}
}
let flock = [];
for (let i = 0; i < 200; i++) {
flock.push(new Boid());
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const drawnCenters = new Set();
flock.forEach((boid) => {
boid.update(flock);
boid.draw();
});
requestAnimationFrame(animate);
}
animate();