-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoid.pde
355 lines (318 loc) · 9.96 KB
/
Boid.pde
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
// From The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Boid class
// Methods for Separation, Cohesion, Alignment added
HashMap<Integer, ArrayList<Boid>> groups = new HashMap<Integer, ArrayList<Boid>>();
float lineSpeed = 10;
PImage boidImage;
float neighbordist = 50;
class Boid {
final int id;
int groupID;
PVector location;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
int type;
boolean filtering;
color clr;
boolean[] closeToBorder= new boolean[4];
PVector sumVel=new PVector();
PVector sumLoc=new PVector();
int nbCount;
// cohesion-center;
PVector cohesionPoint;
SoundForm minCircle, scnClosest;
float minDist;
float lineInvert = 0;
Boid(float x, float y, int id) {
this.id = id;
acceleration = new PVector(0, 0);
velocity = new PVector(random(-1, 1), random(-1, 1));
location = new PVector(x, y);
r = 5.0;
maxspeed = 3+random(- flock.maxspeed_Deriv, flock.maxspeed_Deriv);
maxforce = 0.05;
type = (int) random (0, 3);
}
void run(ArrayList<Boid> boids) {
flock(boids);
update();
// get the closest SoundForm
calcSss();
borders();
if (renderFlock)
render();
lineInvert+=lineSpeed;
}
void initGroup() {
ArrayList<Boid> myGroup = new ArrayList<Boid>();
myGroup.add(this);
groupID = id;
groups.put(groupID, myGroup);
}
void applyForce(PVector force) {
acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList<Boid> boids) {
PVector sep = separate(boids); // Separation
getNeighbours(boids);
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
// Method to update location
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
location.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
checkCloseToBorder();
}
void calcSss() {
minDist = 30000;
float min2Dist = 30000;
SoundForm minCircle = null;
for (int i=0; i < ss; i++) {
SoundForm c = forms.get(i);
float distance = location.dist(c.location);
if (distance < minDist) {
minDist = distance;
scnClosest = minCircle;
minCircle = c;
}
else if (distance < min2Dist) {
min2Dist = distance;
scnClosest = c;
}
}
if (this.minCircle != null && this.minCircle != minCircle)
this.minCircle.removeBoid(this);
this.minCircle = minCircle;
minCircle.addBoid(this);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target, location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
// float minDist =0;
color fillColor=closeFormColors();
//stroke(0);
noStroke();
pushMatrix();
translate(location.x, location.y);
rotate(theta);
if (random_BoidScaleUp && random(1) < nbCount/(float)n) {
scale(2+5*(nbCount/(float)n));
}
if (boidsMode == GRADIANTS) {
tint(hue(fillColor), strongColor, strongColor, colorRange*0.2f);
image(boidImage, 0, 0, 60, 60);
}
else if (boidsMode == FORMS) {
// println(type + " "+fillColor);
fill(fillColor);
switch (type) {
case 0:
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
break;
case 1:
ellipse (0, 0, r, r);
break;
case 2:
rect (-r, -r, 2*r, 2*r);
break;
}
}
else if ( boidsMode == SIMPLE) {
stroke(colorRange);
// if (closeToBorder != 0)
// stroke(0, colorRange, colorRange);
line(0, 0, 0, 10);
}
popMatrix();
noFill();
// line(location.x, location.y, minCircle.location.x, minCircle.location.y);
// cohesionPoint is calculated in cohesion.
if (showCurves_boidToForm) {
println(minDist+" "+lineInvert);
if (lineInvert>minDist) { // line blink effect
stroke((int)(colorRange*0.5+ hue(minCircle.clr))%colorRange, strongColor, strongColor);
lineInvert =0;
}
else
stroke(minCircle.clr);
bezier(location.x, location.y,
cohesionPoint.x, cohesionPoint.y,
cohesionPoint.x, cohesionPoint.y,
minCircle.location.x, minCircle.location.y);
}
}
color closeFormColors() {
float minDist = location.dist(minCircle.location);
float min_Scn_rel = minDist / (minDist+location.dist(scnClosest.location));
float div = hue(scnClosest.clr) - hue(minCircle.clr) ;
boolean start1 = div < colorRange/2 && div >0;
if (start1)
return color((hue(minCircle.clr)+((hue(scnClosest.clr)-hue(minCircle.clr))*min_Scn_rel))%colorRange, strongColor, strongColor);
else
return color((hue(minCircle.clr)-((hue(scnClosest.clr)-hue(minCircle.clr))*min_Scn_rel)+colorRange)%colorRange, strongColor, strongColor);
}
// Wraparound
void borders() {
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 25.0f;
PVector steer = new PVector(0, 0, 0);
int count = 0;
// For every boid in the system, check if it's too close
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
boolean close = (d > 0) && (d < desiredseparation);
boolean borderAcrossBorder = closeAcrossBorder(other, desiredseparation);
if (close || borderAcrossBorder) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(location, other.location);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div((float)count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}
boolean closeAcrossBorder(Boid other, float maxDist) {
boolean borderAcrossBorder = borderMatch(other);
if (borderAcrossBorder)
borderAcrossBorder= location.dist(shiftToMe(other)) < maxDist;
return borderAcrossBorder;
}
void getNeighbours(ArrayList<Boid> boids) {
sumVel.mult(0);
sumLoc.mult(0);
nbCount = 0;
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
boolean close = (d > 0) && (d < neighbordist);
boolean borderAcrossBorder = closeAcrossBorder(other, neighbordist);
if (close || borderAcrossBorder) {
sumVel.add(other.velocity);
sumLoc.add(other.location);
nbCount++;
if (other.groupID < groupID)
integrateGroupInto(groupID, other.groupID);
}
}
// print(sum + " "+nbCount);
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList<Boid> boids) {
PVector dir = new PVector(sumVel.x, sumVel.y);
if (nbCount > 0) {
dir.div((float)nbCount);
dir.setMag(maxspeed);
dir.sub(velocity);
dir.limit(maxforce);
return dir;
}
else
return new PVector(0, 0);
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList<Boid> boids) {
PVector center = new PVector(sumLoc.x, sumLoc.y); // Start with empty vector to accumulate all locations
if (nbCount > 0) {
center.div((float) nbCount);
cohesionPoint = center;
return seek(center); // Steer towards the location
}
else {
cohesionPoint = location;
return new PVector(0, 0);
}
}
void checkCloseToBorder() {
java.util.Arrays.fill(closeToBorder, false);
if (location.x < neighbordist)
closeToBorder[0] = true;
else if (location.x > width-neighbordist)
closeToBorder[2] = true;
if ( location.y < neighbordist)
closeToBorder[1] = true;
else if (location.y > height-neighbordist)
closeToBorder[3] = true;
}
boolean borderMatch(Boid other) {
for (int i=0; i < 4;i++)
if (closeToBorder[i] & other.closeToBorder[(i+2)%4])
return true;
return false;
}
PVector shiftToMe(Boid other) {
PVector loc = new PVector(other.location.x, other.location.y);
if (closeToBorder[0])
loc.sub(width, 0, 0);
else if (closeToBorder[1])
loc.sub(0, height, 0);
if (closeToBorder[2])
loc.add(width, 0, 0);
else if (closeToBorder[3])
loc.add(0, height, 0);
return loc;
}
}
void integrateGroupInto(int from, int into) {
for (Boid b : groups.get(from))
b.groupID= groups.get(into).get(0).groupID;
groups.get(into).addAll(groups.remove(from));
}