-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboid.js
More file actions
188 lines (148 loc) · 4.51 KB
/
Copy pathboid.js
File metadata and controls
188 lines (148 loc) · 4.51 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
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
/* global width height lambda maxSpeed coefCohes coefAlign coefSep */
const EPS = 1e-4;
var uid = 0;
class Boids { // eslint-disable-line no-unused-vars
constructor (number, tailSize, influenceRadius, start, goal) {
start = start || [Math.random() * width, Math.random() * height];
goal = goal || [Math.random() * width, Math.random() * height];
this.number = number;
this.influenceRadius = influenceRadius;
this.lambda = lambda;
this.tailSize = tailSize;
this.goal = goal;
this.maxSpeed = maxSpeed;
this.uid = uid++;
this.coefCohes = coefCohes;
this.coefSep = coefSep;
this.coefAlign = coefAlign;
this.boids = d3.range(number).map(() => new Boid(start, this));
}
updateSpeed (groups) {
var centroid = this.getCentroid();
this.boids.forEach(boid => boid.updateSpeed(centroid, groups));
}
updatePosition () {
this.boids.forEach(boid => boid.updatePosition());
}
getCentroid() {
return [
d3.mean(this.boids, b => b.x),
d3.mean(this.boids, b => b.y)
];
}
}
class Boid {
constructor (start, group) {
this.group = group;
this.x = (2 * Math.random() - 1) * 125 + start[0];
this.y = (2 * Math.random() - 1) * 125 + start[1];
this.vx = 2 * Math.random() - 1,
this.vy = 2 * Math.random() - 1,
this.path = d3.range(this.group.tailSize).map(() => [this.x, this.y]);
this.count = 0;
}
vectorTo ([x, y]) {
return [x - this.x, y - this.y];
}
directionTo (vector) {
var [vx, vy] = this.vectorTo(vector);
var d = this.distanceTo(vector) + EPS;
return [vx / d, vy / d];
}
distanceTo ([x, y]) {
return norm([x - this.x, y - this.y]);
}
updateSpeed (centroid, groups) {
var group = this.group,
cohesionVector = this.getCohesionVector(centroid),
alignementVector = this.getAlignementVector(),
separationVector = this.getSeparationVector(groups);
var newVx = group.coefCohes * cohesionVector[0] + group.coefAlign * alignementVector[0] + group.coefSep * separationVector[0],
newVy = group.coefCohes * cohesionVector[1] + group.coefAlign * alignementVector[1] + group.coefSep * separationVector[1];
[newVx, newVy] = renorm([newVx, newVy], group.maxSpeed);
this.vx = group.lambda * this.vx + (1 - group.lambda) * newVx;
this.vy = group.lambda * this.vy + (1 - group.lambda) * newVy;
[this.vx, this.vy] = renorm([this.vx, this.vy], group.maxSpeed);
}
updatePosition () {
this.x += this.vx;
this.y += this.vy;
var path = this.path,
dx = this.vx,
dy = this.vy,
x = this.x,
y = this.y,
speed = this.group.maxSpeed,
count = speed * 10,
k1 = -5 - speed / 3;
path[0][0] = this.x;
path[0][1] = this.y;
for (var j = 1; j < this.group.tailSize; j++) {
var vx = x - path[j][0],
vy = y - path[j][1],
k2 = Math.sin(((this.count += count) + j * 3) / 300) / speed;
path[j][0] = (x += dx / speed * k1) - dy * k2;
path[j][1] = (y += dy / speed * k1) + dx * k2;
speed = Math.sqrt((dx = vx) * dx + (dy = vy) * dy);
}
}
getCohesionVector (vector) {
var [vx, vy] = this.vectorTo(vector);
[vx, vy] = [Math.log(Math.abs(vx) + 1) * Math.sign(vx), Math.log(Math.abs(vy) + 1) * Math.sign(vy)];
return renorm([vx, vy]);
}
getCohesionVector2 (vector) {
return this.directionTo(vector);
}
getAlignementVector () {
return this.directionTo(this.group.goal);
}
getSeparationVector(groups) {
var sum = [0, 0];
groups.forEach(group => {
group.boids.forEach(boid => {
var d = this.distanceTo([boid.x, boid.y]) + EPS;
if (EPS < d && d < this.group.influenceRadius) {
// console.log(~~(this.x - boid.x), ~~d);
// sum[0] += 1 / (this.x - boid.x) / d / d * 1000
// sum[1] += 1 / (this.y - boid.y) / d / d * 1000
sum[0] += (this.x - boid.x) / d / d * 10;
sum[1] += (this.y - boid.y) / d / d * 10;
}
});
});
return (sum);
}
getSeparationVector2(groups) {
var sum = [0, 0];
groups.forEach(group => {
group.boids.forEach(boid => {
if (boid == this)
return;
var [vx, vy] = this.vectorTo([boid.x, boid.y]);
sum[0] -= 1 / vx * 2;
sum[1] -= 1 / vy * 2;
});
});
return (sum);
}
getSeparationVector3() {
var sum = [0, 0];
this.group.boids.forEach((boid) => {
var d = this.distanceTo([boid.x, boid.y]) + EPS;
if (d < this.group.influenceRadius) {
sum[0] += (this.x - boid.x) / d;
sum[1] += (this.y - boid.y) / d;
}
});
return renorm(sum);
}
}
function norm([x, y]) {
return Math.sqrt(x*x + y*y);
}
function renorm([x, y], newNorm) {
newNorm = newNorm || 1;
var oldNorm = norm([x, y]) + EPS;
return [x *newNorm / oldNorm, y * newNorm / oldNorm];
}