-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboids.js
202 lines (175 loc) · 5.84 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
const canvas = document.getElementById("boidsCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const NUM_BOIDS = 1000;
const BOID_RADIUS = 5;
const PERCEPTION_RADIUS = 50;
const MAX_SPEED = 2;
let ALIGNMENT_FACTOR = 1.5;
let COHESION_FACTOR = 1;
let SEPARATION_FACTOR = 2.5;
let TIME_STEP = 0.1; // Time step for simulation
// Add event listeners to update simulation parameters based on UI sliders
const timeStepControl = document.getElementById("timeStepControl");
const cohesionControl = document.getElementById("cohesionControl");
const separationControl = document.getElementById("separationControl");
timeStepControl.addEventListener("input", (event) => {
TIME_STEP = parseFloat(event.target.value);
});
cohesionControl.addEventListener("input", (event) => {
COHESION_FACTOR = parseFloat(event.target.value);
});
separationControl.addEventListener("input", (event) => {
SEPARATION_FACTOR = parseFloat(event.target.value);
});
class Boid {
constructor(x, y) {
this.position = { x, y };
this.velocity = {
x: Math.random() * 2 - 1,
y: Math.random() * 2 - 1,
};
this.acceleration = { x: 0, y: 0 };
}
update() {
this.velocity.x += this.acceleration.x * TIME_STEP;
this.velocity.y += this.acceleration.y * TIME_STEP;
const speed = Math.sqrt(
this.velocity.x ** 2 + this.velocity.y ** 2
);
if (speed > MAX_SPEED) {
this.velocity.x = (this.velocity.x / speed) * MAX_SPEED;
this.velocity.y = (this.velocity.y / speed) * MAX_SPEED;
}
this.position.x += this.velocity.x * TIME_STEP;
this.position.y += this.velocity.y * TIME_STEP;
this.acceleration.x = 0;
this.acceleration.y = 0;
this.boundaryCheck();
}
boundaryCheck() {
if (this.position.x > canvas.width) this.position.x = 0;
if (this.position.x < 0) this.position.x = canvas.width;
if (this.position.y > canvas.height) this.position.y = 0;
if (this.position.y < 0) this.position.y = canvas.height;
}
applyForce(force) {
this.acceleration.x += force.x;
this.acceleration.y += force.y;
}
draw() {
ctx.beginPath();
ctx.arc(this.position.x, this.position.y, BOID_RADIUS, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
ctx.strokeStyle = "green"; // Green outline for boids
ctx.stroke();
ctx.closePath();
}
}
const boids = Array.from({ length: NUM_BOIDS }, () => {
return new Boid(
Math.random() * canvas.width,
Math.random() * canvas.height
);
});
function calculateAlignment(boid) {
let steering = { x: 0, y: 0 };
let total = 0;
for (const other of boids) {
const distance = Math.hypot(
boid.position.x - other.position.x,
boid.position.y - other.position.y
);
if (other !== boid && distance < PERCEPTION_RADIUS) {
steering.x += other.velocity.x;
steering.y += other.velocity.y;
total++;
}
}
if (total > 0) {
steering.x /= total;
steering.y /= total;
const magnitude = Math.sqrt(steering.x ** 2 + steering.y ** 2);
if (magnitude > 0) {
steering.x = (steering.x / magnitude) * MAX_SPEED - boid.velocity.x;
steering.y = (steering.y / magnitude) * MAX_SPEED - boid.velocity.y;
}
}
return steering;
}
function calculateCohesion(boid) {
let steering = { x: 0, y: 0 };
let total = 0;
for (const other of boids) {
const distance = Math.hypot(
boid.position.x - other.position.x,
boid.position.y - other.position.y
);
if (other !== boid && distance < PERCEPTION_RADIUS) {
steering.x += other.position.x;
steering.y += other.position.y;
total++;
}
}
if (total > 0) {
steering.x /= total;
steering.y /= total;
steering.x -= boid.position.x;
steering.y -= boid.position.y;
const magnitude = Math.sqrt(steering.x ** 2 + steering.y ** 2);
if (magnitude > 0) {
steering.x = (steering.x / magnitude) * MAX_SPEED - boid.velocity.x;
steering.y = (steering.y / magnitude) * MAX_SPEED - boid.velocity.y;
}
}
return steering;
}
function calculateSeparation(boid) {
let steering = { x: 0, y: 0 };
let total = 0;
for (const other of boids) {
const distance = Math.hypot(
boid.position.x - other.position.x,
boid.position.y - other.position.y
);
if (other !== boid && distance < PERCEPTION_RADIUS / 2) {
const diff = {
x: boid.position.x - other.position.x,
y: boid.position.y - other.position.y,
};
diff.x /= distance;
diff.y /= distance;
steering.x += diff.x;
steering.y += diff.y;
total++;
}
}
if (total > 0) {
steering.x /= total;
steering.y /= total;
}
return steering;
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (const boid of boids) {
const alignment = calculateAlignment(boid);
const cohesion = calculateCohesion(boid);
const separation = calculateSeparation(boid);
alignment.x *= ALIGNMENT_FACTOR;
alignment.y *= ALIGNMENT_FACTOR;
cohesion.x *= COHESION_FACTOR;
cohesion.y *= COHESION_FACTOR;
separation.x *= SEPARATION_FACTOR;
separation.y *= SEPARATION_FACTOR;
boid.applyForce(alignment);
boid.applyForce(cohesion);
boid.applyForce(separation);
boid.update();
boid.draw();
}
requestAnimationFrame(animate);
}
animate();