This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflock.js
230 lines (190 loc) · 6.25 KB
/
flock.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
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
// Copyright © Richard Poole 2010: http://sycora.com/
// Algorithm by Craig Reynolds: http://www.red3d.com/cwr/boids/
// Licence for this file: http://creativecommons.org/licenses/MIT/
Math.dotProduct2 = function(ax, ay, bx, by) {
return ax * bx + ay * by;
}
function FlockController(canvas, options) {
// Settings for the flocking algorithm.
var defaults = {
alignmentPriority: 2,
cohesionPriority: 1,
frameRate: 50,
maxRotation: 30 * Math.PI / 180,
minSpeed: 3,
maxSpeed: 5,
numBoids: 30,
rotationSpeed: 0.2,
separationDistance: 20,
separationPriority: 5,
targetPriority: 3,
torus: true,
visualField: Math.PI * 270 / 180,
visualRange: 50,
};
var boids = new Array();
var cosVisualFieldDiv2;
var redrawInterval;
var settings = {};
var targetX = canvas.width / 2;
var targetY = canvas.height / 2;
var timer;
var visualRangeSqr;
if (!canvas.getContext)
throw new Error('Canvas is not supported by your browser.');
var ctx = canvas.getContext('2d');
var drawBoid = function(boid) {
ctx.fillRect(boid.x, boid.y, 2, 2);
if (settings && settings.directionVectorStyle) {
ctx.strokeStyle = settings.directionVectorStyle;
ctx.beginPath();
ctx.moveTo(boid.x, boid.y);
ctx.lineTo(boid.x + boid.vx * 10, boid.y + boid.vy * 10);
ctx.stroke();
}
}
var getVisualStats = function(boid) {
var stats = {x: 0, y: 0, vx: 0, vy: 0, closestDistance: 999999999, count: 0};
for (var i in boids) {
if (boids[i] == boid)
continue;
var dx = boids[i].x - boid.x;
var dy = boids[i].y - boid.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < stats.closestDistance) {
stats.closestDistance = distance;
stats.closestBoid = boids[i];
}
var ndx = dx / distance;
var ndy = dy / distance;
var cosθ = Math.dotProduct2(Math.cos(boid.θ), Math.sin(boid.θ), ndx, ndy);
if (dx * dx + dy * dy <= visualRangeSqr && cosθ >= cosVisualFieldDiv2) {
stats.x += boids[i].x;
stats.y += boids[i].y;
stats.vx += boids[i].vx;
stats.vy += boids[i].vy;
stats.count++;
}
}
stats.x /= stats.count;
stats.y /= stats.count;
stats.θ = Math.atan2(stats.vy, stats.vx);
return stats;
}
this.animateFrame = function() {
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#f9f9f9';
ctx.beginPath();
ctx.arc(targetX, targetY, 50, 0, Math.PI * 2, true);
ctx.fill();
// Draw the boids!
ctx.fillStyle = 'black';
for (var i in boids)
drawBoid(boids[i]);
// Move the boids in a flocky way.
for (i in boids) {
var boid = boids[i];
if (settings.torus) {
// Make canvas a torus
if (boid.x < 0) {
boid.x += canvas.width;
}
if (boid.x > canvas.width) {
boid.x -= canvas.width;
}
if (boid.y < 0) {
boid.y += canvas.height;
}
if (boid.y > canvas.height) {
boid.y -= canvas.height;
}
}
var avg = getVisualStats(boid);
var tx = boid.vx;
var ty = boid.vy;
// Steer to avoid crowding local flock mates.
if (avg.closestBoid) {
var dx = avg.closestBoid.x - boid.x;
var dy = avg.closestBoid.y - boid.y;
if (avg.closestDistance < settings.separationDistance) {
tx -= dx / avg.closestDistance * settings.separationPriority;
ty -= dy / avg.closestDistance * settings.separationPriority;
}
}
// Steer towards the average heading of local flock mates.
tx += Math.cos(avg.θ) * settings.alignmentPriority;
ty += Math.sin(avg.θ) * settings.alignmentPriority;
// Steer towards the average position of local flock mates.
if (avg.count > 0) {
var dx = avg.x - boid.x;
var dy = avg.y - boid.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var ndx = dx / distance;
var ndy = dy / distance;
tx += ndx * settings.cohesionPriority;
ty += ndy * settings.cohesionPriority;
}
// Steer towards the target.
var dx = targetX - boid.x;
var dy = targetY - boid.y;
var distance = Math.max(Math.sqrt(dx * dx + dy * dy), 50);
var ndx = dx / distance;
var ndy = dy / distance;
tx += ndx * settings.targetPriority;
ty += ndy * settings.targetPriority;
var tθ = Math.atan2(ty, tx);
var cw = (tθ - boid.θ + Math.PI * 4) % (Math.PI * 2);
var acw = (boid.θ - tθ + Math.PI * 4) % (Math.PI * 2);
var rotation = Math.abs(cw) < Math.abs(acw) ? cw : -acw;
rotation *= settings.rotationSpeed;
rotation = Math.min(Math.max(rotation, -settings.maxRotation), settings.maxRotation);
boid.θ += rotation;
boid.updateVelocity();
boid.updatePosition();
}
};
this.setTarget = function(x, y) {
targetX = x;
targetY = y;
}
this.startAnimation = function() {
timer = window.setInterval(this.animateFrame, redrawInterval);
};
this.stopAnimation = function() {
window.clearInterval(timer);
};
this.updateOptions = function(options) {
$.extend(settings, options);
var newRedrawInterval = 1000 / settings.frameRate;
if (redrawInterval != newRedrawInterval) {
if (timer)
this.stopAnimation();
redrawInterval = newRedrawInterval;
this.startAnimation();
}
cosVisualFieldDiv2 = Math.cos(settings.visualField / 2);
visualRangeSqr = settings.visualRange * settings.visualRange;
while (boids.length < settings.numBoids)
boids.push(new Boid(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * Math.PI * 2, settings.minSpeed + Math.random(settings.maxSpeed - settings.minSpeed)));
while (boids.length > settings.numBoids)
boids.pop();
};
this.updateOptions(defaults);
this.updateOptions(options);
}
function Boid(x, y, θ, speed) {
this.x = x;
this.y = y;
this.θ = θ;
this.speed = speed;
this.updatePosition = function() {
this.x += this.vx * this.speed;
this.y += this.vy * this.speed;
};
this.updateVelocity = function() {
this.vx = Math.cos(this.θ);
this.vy = Math.sin(this.θ);
};
this.updateVelocity();
}