-
Notifications
You must be signed in to change notification settings - Fork 5
/
non-reciprocal_vicsek_correct_pd.html
408 lines (350 loc) · 13.3 KB
/
non-reciprocal_vicsek_correct_pd.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Non-Reciprocal Vicsek Model</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
.collapsed fieldset {
display: none;
}
.control-toggle {
position: fixed;
top: 11px;
left: 15px;
z-index: 100;
cursor: pointer;
font-weight: bold;
border-radius: 5px;
}
#simulationCanvas {
display: block;
position: top;
}
#controlCenter {
position: absolute;
left: 10px;
top: 10px;
padding: 10px;
background-color: rgba(255, 255, 255, 0.7);
border-radius: 5px;
}
#controlCenter {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
background-color: rgba(255, 255, 255, 0.8);
padding: 10px;
border-radius: 5px;
}
fieldset {
border: 1px solid #ccc;
border-radius: 5px;
padding: 15px;
margin-bottom: 10px;
}
legend {
font-weight: bold;
}
.checkboxContainer {
display: flex;
flex-direction: column;
align-items: center;
margin: 5px;
}
.sliderInput {
width: 100%;
}
.sliderContainer label {
margin-bottom: 5px;
}
.sliderContainer {
display: block;
width: 160px;
text-align: left;
}
.slider {
margin-left: 5px;
}
.slider-container {
display: block;
width: 120px;
text-align: center;
}
</style>
</head>
<body>
<canvas id="simulationCanvas"></canvas>
<div id="controlCenter">
<div class="control-toggle" id="controlToggle">●</div>
<fieldset>
<legend>Controls</legend>
<div class="sliderContainer">
<label>Particles: <span id="numParticlesValue">1500</span></label>
<input type="range" min="10" max="3000" value="1500" class="slider sliderInput" id="numParticles">
</div>
<div class="sliderContainer">
<label>Speed: <span id="speedValue">7</span></label>
<input type="range" min="0.1" max="15" step="0.1" value="7" class="slider sliderInput" id="speed">
</div>
<div class="sliderContainer">
<label>Fish A Ratio: <span id="fishARatioValue">0.5</span></label>
<input type="range" min="0" max="1" step="0.01" value="0.5" class="slider sliderInput" id="fishARatio">
</div>
<div class="sliderContainer">
<label>A-A / B-B: <span id="sameFishWeightValue">.5</span></label>
<input type="range" min="0" max="1" step="0.01" value=".5" class="slider sliderInput" id="sameFishWeight">
</div>
<div class="sliderContainer">
<label>B-A: <span id="baFishWeightValue">-0.2</span></label>
<input type="range" min="-1" max="1" step="0.01" value="-0.2" class="slider sliderInput" id="baFishWeight">
</div>
<div class="sliderContainer">
<label>A-B: <span id="abFishWeightValue">1</span></label>
<input type="range" min="-1" max="1" step="0.01" value="1" class="slider sliderInput" id="abFishWeight">
</div>
<div class="sliderContainer">
<label>Interaction Radius: <span id="radiusValue">20</span></label>
<input type="range" min="1" max="150" value="20" class="slider sliderInput" id="radius">
</div>
<div class="sliderContainer">
<label>Noise: <span id="noiseValue">0.1</span></label>
<input type="range" min="0" max="1" step="0.001" value="0.1" class="slider sliderInput" id="noise">
</div>
<div class="sliderContainer">
<label>Particle Size: <span id="particleSizeValue">5</span></label>
<input type="range" min="1" max="20" value="5" class="slider sliderInput" id="particleSize">
</div>
<div class="selectContainer">
<label>Particle Type:</label>
<select id="particleType">
<option value="arrow">Fish</option>
<option value="dot">Dot</option>
</select>
</div>
<div >
<label for="invertColors">Invert Color:</label>
<input type="checkbox" id="invertColors">
</div>
</fieldset>
</div>
<script>
const canvas = document.getElementById('simulationCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const numParticlesSlider = document.getElementById('numParticles');
const numParticlesValue = document.getElementById('numParticlesValue');
const speedSlider = document.getElementById('speed');
const speedValue = document.getElementById('speedValue');
const radiusSlider = document.getElementById('radius');
const radiusValue = document.getElementById('radiusValue');
const noiseSlider = document.getElementById('noise');
const noiseValue = document.getElementById('noiseValue');
const particleSizeSlider = document.getElementById('particleSize');
const particleSizeValue = document.getElementById('particleSizeValue');
const particleTypeSelect = document.getElementById('particleType');
const invertColorsCheckbox = document.getElementById('invertColors');
const fishARatioSlider = document.getElementById('fishARatio');
const fishARatioValue = document.getElementById('fishARatioValue');
const sameFishWeightSlider = document.getElementById('sameFishWeight');
const sameFishWeightValue = document.getElementById('sameFishWeightValue');
const baFishWeightSlider = document.getElementById('baFishWeight');
const baFishWeightValue = document.getElementById('baFishWeightValue');
const abFishWeightSlider = document.getElementById('abFishWeight');
const abFishWeightValue = document.getElementById('abFishWeightValue');
let NUM_PARTICLES = parseInt(numParticlesSlider.value);
let SPEED = parseFloat(speedSlider.value);
let INTERACTION_RADIUS = parseFloat(radiusSlider.value);
let NOISE = parseFloat(noiseSlider.value);
let PARTICLE_SIZE = parseFloat(particleSizeSlider.value);
let PARTICLE_TYPE = particleTypeSelect.value;
let INVERT_COLORS = invertColorsCheckbox.checked;
let FISH_A_RATIO = parseFloat(fishARatioSlider.value);
let SAME_FISH_WEIGHT = parseFloat(sameFishWeightSlider.value);
let OPPOSITE_FISH_WEIGHT = parseFloat(baFishWeightSlider.value);
let AB_FISH_WEIGHT = parseFloat(abFishWeightSlider.value);
document.addEventListener('DOMContentLoaded', () => {
const controlCenter = document.getElementById('controlCenter');
const controlToggle = document.getElementById('controlToggle');
controlToggle.addEventListener('click', () => {
controlCenter.classList.toggle('collapsed');
});
});
abFishWeightSlider.oninput = function() {
AB_FISH_WEIGHT = parseFloat(this.value);
abFishWeightValue.innerHTML = this.value;
}
fishARatioSlider.oninput = function() {
FISH_A_RATIO = parseFloat(this.value);
fishARatioValue.innerHTML = this.value;
resetParticles();
}
sameFishWeightSlider.oninput = function() {
SAME_FISH_WEIGHT = parseFloat(this.value);
sameFishWeightValue.innerHTML = this.value;
}
baFishWeightSlider.oninput = function() {
OPPOSITE_FISH_WEIGHT = parseFloat(this.value);
baFishWeightValue.innerHTML = this.value;
}
numParticlesSlider.oninput = function() {
NUM_PARTICLES = parseInt(this.value);
numParticlesValue.innerHTML = this.value;
resetParticles();
}
speedSlider.oninput = function() {
SPEED = parseFloat(this.value);
speedValue.innerHTML = this.value;
}
radiusSlider.oninput = function() {
INTERACTION_RADIUS = parseFloat(this.value);
radiusValue.innerHTML = this.value;
}
noiseSlider.oninput = function() {
NOISE = parseFloat(this.value);
noiseValue.innerHTML = this.value;
}
particleSizeSlider.oninput = function() {
PARTICLE_SIZE = parseFloat(this.value);
particleSizeValue.innerHTML = this.value;
}
particleTypeSelect.onchange = function() {
PARTICLE_TYPE = this.value;
}
invertColorsCheckbox.onchange = function() {
INVERT_COLORS = this.checked;
}
class Particle {
constructor(x, y, angle, fishType) {
this.x = x;
this.y = y;
this.angle = angle;
this.fishType = fishType;
}
update(particles) {
let sumX = 0;
let sumY = 0;
let count = 0;
const periodic = (dx, dy) => {
const dxPeriodic = (dx + canvas.width) % canvas.width;
const dyPeriodic = (dy + canvas.height) % canvas.height;
const dxMin = Math.abs(dxPeriodic) < canvas.width / 2 ? dxPeriodic : dxPeriodic - canvas.width;
const dyMin = Math.abs(dyPeriodic) < canvas.height / 2 ? dyPeriodic : dyPeriodic - canvas.height;
return { dx: dxMin, dy: dyMin };
}
particles.forEach(particle => {
const { dx, dy } = periodic(this.x - particle.x, this.y - particle.y);
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < INTERACTION_RADIUS * 2 && distance > 0) {
let weight;
if (this.fishType === 'A' && particle.fishType === 'B') {
weight = AB_FISH_WEIGHT;
} else if (this.fishType === 'B' && particle.fishType === 'A') {
weight = OPPOSITE_FISH_WEIGHT;
} else {
weight = this.fishType === particle.fishType ? SAME_FISH_WEIGHT : OPPOSITE_FISH_WEIGHT;
}
sumX += Math.cos(particle.angle) * weight;
sumY += Math.sin(particle.angle) * weight;
count += Math.abs(weight);
}
});
if (count > 0) {
const avgAngle = Math.atan2(sumY / count, sumX / count);
const noise = (Math.random() - 0.5) * NOISE;
this.angle = (avgAngle + noise + 2 * Math.PI) % (2 * Math.PI);
} else {
const noise = (Math.random() - 0.5) * NOISE;
this.angle = (this.angle + noise + 2 * Math.PI) % (2 * Math.PI);
}
this.angle += (Math.random() - 0.5) * NOISE ;
this.x += SPEED * Math.cos(this.angle);
this.y += SPEED * Math.sin(this.angle);
if (this.x < 0) this.x += canvas.width;
if (this.x > canvas.width) this.x -= canvas.width;
if (this.y < 0) this.y += canvas.height;
if (this.y > canvas.height) this.y -= canvas.height;
}
draw() {
// 画出 interaction radius
ctx.beginPath();
ctx.arc(this.x, this.y, INTERACTION_RADIUS, 0, 2 * Math.PI);
ctx.fillStyle = this.fishType === 'A' ? 'rgba(200, 50, 50, 0.01)' : 'rgba(50, 50, 200, 0.01)';
ctx.fill();
if (INVERT_COLORS) {
ctx.fillStyle = this.fishType === 'A' ? 'rgb(255, 100, 100)' : 'rgb(100, 100, 255)';
ctx.strokeStyle = this.fishType === 'A' ? 'rgb(255, 100, 100)' : 'rgb(100, 100, 255)';
} else {
ctx.fillStyle = this.fishType === 'A' ? 'rgb(200, 50, 50)' : 'rgb(50, 50, 200)';
ctx.strokeStyle = this.fishType === 'A' ? 'rgb(200, 50, 50)' : 'rgb(50, 50, 200)';
}
if (PARTICLE_TYPE === 'dot') {
ctx.beginPath();
ctx.arc(this.x, this.y, PARTICLE_SIZE, 0, 2 * Math.PI);
ctx.fill();
} else if (PARTICLE_TYPE === 'arrow') {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
// 鱼的身体
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.quadraticCurveTo(PARTICLE_SIZE * 1.4, -PARTICLE_SIZE, PARTICLE_SIZE * 1.7, 0);
ctx.quadraticCurveTo(PARTICLE_SIZE * 1.4, PARTICLE_SIZE, 0, 0);
ctx.fill();
// 鱼的尾巴
ctx.beginPath();
ctx.moveTo(-PARTICLE_SIZE * 0.2, 0);
ctx.lineTo(-PARTICLE_SIZE * 1.2, -PARTICLE_SIZE / 2);
ctx.lineTo(-PARTICLE_SIZE * 1.2, PARTICLE_SIZE / 2);
ctx.closePath();
ctx.fill();
// 鱼头
ctx.beginPath();
ctx.arc(PARTICLE_SIZE * 1.7, 0, PARTICLE_SIZE / 2, 0, 2 * Math.PI);
ctx.fill();
ctx.restore();
}
}
}
let particles = [];
function createParticle() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const angle = (Math.random())* Math.PI *2 ;
const fishType = Math.random() < FISH_A_RATIO ? 'A' : 'B';
const particle = new Particle(x, y, angle, fishType);
return particle;
}
function resetParticles() {
particles = [];
for (let i = 0; i < NUM_PARTICLES; i++) {
particles.push(createParticle());
}
}
resetParticles();
function animate() {
if (INVERT_COLORS) {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
} else {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
particles.forEach(particle => {
particle.update(particles);
particle.draw();
});
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>