-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlock.js
More file actions
83 lines (76 loc) · 2.83 KB
/
Copy pathFlock.js
File metadata and controls
83 lines (76 loc) · 2.83 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
class Flock{
constructor(boids) {
this.boids = boids;
this.avoidFactor = 0.005;
this.protectedRange = 25;
this.visibleRange = 150;
this.matchingFactor = 0.05;
this.centeringFactor = 0.001;
this.turnFactor = 0.08;
}
algorithm(){
this.boids.forEach(boid => {
let closeDx = 0;
let closeDy = 0;
let xposAvg = 0;
let yposAvg = 0;
let xvelAvg = 0;
let yvelAvg = 0;
let neighbouringBoids = 0;
const otherBoids = this.boids.filter(otherBoid => otherBoid != boid)
otherBoids.forEach(otherBoid => {
let dx = boid.x - otherBoid.x;
let dy = boid.y - otherBoid.y;
if (Math.abs(dx) < this.protectedRange && Math.abs(dy) < this.protectedRange){
const distance = dx * dx + dy * dy;
if (distance < this.protectedRange * this.protectedRange){
closeDx += boid.x - otherBoid.x;
closeDy += boid.y - otherBoid.y;
}
else if (distance < this.visibleRange * this.visibleRange){
xposAvg += otherBoid.x;
yposAvg += otherBoid.y;
xvelAvg += otherBoid.vx;
yvelAvg += otherBoid.vy;
neighbouringBoids++;
}
}
})
if (neighbouringBoids > 0){
xposAvg = xposAvg / neighbouringBoids;
yposAvg = yposAvg / neighbouringBoids;
xvelAvg = xvelAvg / neighbouringBoids;
yvelAvg = yvelAvg / neighbouringBoids;
boid.vx = (boid.vx +
(xposAvg - boid.x)*this.centeringFactor +
(xvelAvg - boid.vx)*this.matchingFactor)
boid.vy = (boid.vy +
(yposAvg - boid.y)*this.centeringFactor +
(yvelAvg - boid.vy)*this.matchingFactor)
}
boid.vx += (closeDx * this.avoidFactor);
boid.vy += (closeDy * this.avoidFactor);
const marginY = 300
const marginX = 400
if (boid.y < marginY){
boid.vy += this.turnFactor;
}
if (boid.x > window.innerWidth - marginX){
boid.vx -= this.turnFactor;
}
if (boid.x < marginX){
boid.vx += this.turnFactor;
}
if (boid.y > window.innerHeight - marginY){
boid.vy -= this.turnFactor;
}
})
}
move(){
this.algorithm()
this.boids.forEach(boid => {
boid.move()
});
}
}
export default Flock