-
Notifications
You must be signed in to change notification settings - Fork 0
/
lotka.js
498 lines (437 loc) · 13.7 KB
/
lotka.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
let boids = [];
let preds = [];
let l_count_boids = [];
let l_count_preds = [];
function setup() {
//const canvas = createCanvas(windowWidth/2, windowHeight/2);
const canvas = createCanvas(1000, 400);
canvas.parent('sketch-holder');
// Add an initial set of boids into the system
for (let i = 0; i < 150; i++) {
boids[i] = new Boid(random(width-500), random(height));
}
// Add an initial set of predators into the system
for (let i = 0; i <5; i++) {
preds[i] = new Pred(random(width-500), random(height));
}
// keep track of the number of boids/preds
l_count_boids[0] = boids.length;
l_count_preds[0] = preds.length;
}
function draw() {
fill(255, 255, 255, 90);
noStroke();
rect(0, 0, width, height);
// Run all the boids
for (let i = 0; i < boids.length; i++) {
boids[i].run(boids, preds);
}
for (let i = 0; i < preds.length; i++) {
preds[i].run(boids, preds);
}
// Make predators die if boids ressources get limited
if (boids.length/(5*preds.length)<random()) {
if (preds.length>1 || boids.length<2){
preds.splice(random(preds.length-1),1);
}
}
// Make preys die randomly if ressource get limited
if ( ((frameCount/1000)%1)/20 >random()) {
boids.splice(random(boids.length-1),1);
}
//text((frameCount/1000)%1, width/2+400, 88);
fill(255, 255, 255);
rect(width-500, 0, 15, height);
// keep track of the number of boids/preds
l_count_boids.push( boids.length );
l_count_preds.push( preds.length );
if (l_count_boids.length>width-550) {
l_count_boids.shift();
l_count_preds.shift();
}
// curves to track the evolution in time
noFill();
beginShape();
for (var x = 0; x < l_count_boids.length; x++) {
stroke(66, 134, 244, 100);
vertex(550+x, height-l_count_boids[x]);
vertex(550+x, height);
}
endShape();
beginShape();
for (var x = 0; x < l_count_preds.length; x++) {
stroke(255, 140, 130, 100);
vertex(550+x, height-l_count_preds[x]*5);
vertex(550+x, height);
}
endShape();
//text
//noStroke();
//fill(100);
//rect(550, 0, width-551, 100, 5);
noStroke();
fill(255, 140, 130);
rect(width/2+389, 10, 80, 30, 20);
fill(66, 134, 244);
rect(width/2+389, 50, 80, 30, 20);
fill(255)
textSize(15)
text('Predators', width/2+400, 28);
text(' Preys', width/2+400, 68);
//some cosmetic
stroke(211,211,211);
fill(255,255,255,0);
rect(0, 0, width-500, height-1, 5);
rect(550, 0, width-551, height-1, 5);
//noStroke()
//fill(255)
//text('Click to reset', 600, 68);
}
function reset(){
// Add an initial set of boids into the system
for (let i = 0; i < 150; i++) {
boids[i] = new Boid(random(width-500), random(height));
}
// Add an initial set of predators into the system
for (let i = 0; i <5; i++) {
preds[i] = new Pred(random(width-500), random(height));
}
}
function mousePressed(){
reset();
}
// Boid class
// Methods for Separation, Cohesion, Alignment added
class Boid {
constructor(x, y) {
this.acceleration = createVector(0, 0);
this.velocity = p5.Vector.random2D(); //unit vector
this.position = createVector(x, y);
this.r = 3.0; //to handle boundaries
this.maxspeed = 2; // Maximum speed
this.maxforce = 0.05; // Maximum steering force
}
run(boids, preds) {
this.flock(boids, preds);
this.reproduce(boids);
this.update();
this.borders();
this.render();
}
// Forces go into acceleration
applyForce(force) {
this.acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
flock(boids, preds) {
let sep = this.separate(boids); // Separation
let ali = this.align(boids); // Alignment
let coh = this.cohesion(boids); // Cohesion
let flee = this.flee(preds); // avoid preds
// Arbitrarily weight these forces
sep.mult(2.5);
ali.mult(1.0);
coh.mult(2.0);
flee.mult(4.0);
// Add the force vectors to acceleration
this.applyForce(sep);
this.applyForce(ali);
this.applyForce(coh);
this.applyForce(flee);
}
// Method to update location
update() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset acceleration to 0 each cycle
this.acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
seek(target) {
let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(this.maxspeed);
// Steering = Desired minus Velocity
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce); // Limit to maximum steering force
return steer;
}
// Draw boid as a line
render() {
fill(66, 134, 244);
stroke(66, 134, 244);
line(this.position.x, this.position.y, this.position.x + 3*this.velocity.x, this.position.y + 3*this.velocity.y);
}
// Wraparound
borders() {
if (this.position.x < -this.r) this.position.x = width-500 + this.r;
if (this.position.y < -this.r) this.position.y = height + this.r;
if (this.position.x > width-500 + this.r) this.position.x = -this.r;
if (this.position.y > height + this.r) this.position.y = -this.r;
}
// Separation
// Method checks for nearby boids and steers away
separate(boids) {
let desiredseparation = 25.0;
let steer = createVector(0, 0);
let count = 0;
// For every boid in the system, check if it's too close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
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(count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
align(boids) {
let neighbordist = 50;
let sum = createVector(0, 0);
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].velocity);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.normalize();
sum.mult(this.maxspeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxforce);
return steer;
} else {
return createVector(0, 0);
}
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
cohesion(boids) {
let neighbordist = 50;
let sum = createVector(0, 0); // Start with empty vector to accumulate all locations
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].position); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return this.seek(sum); // Steer towards the location
} else {
return createVector(0, 0);
}
}
// Avoid predators
// Method checks for nearby predators and steers away
flee(preds) {
let desiredseparation = 100.0;
let steer = createVector(0, 0);
let count = 0;
// For every pred in the system, check if it's too close
for (let i = 0; i < preds.length; i++) {
let d = p5.Vector.dist(this.position, preds[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, preds[i].position);
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(count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
// Reproduce
// Method checks for nearby boids and reproduce with a probability proportionnal to this number
reproduce(boids) {
let reproductionradius = 100.0;
let count = 0;
let prepr = 0;
// For every boid in the system, check if it's close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < reproductionradius)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
count++; // Keep track of how many
}
}
let thres_reproduce = 0.005;
prepr = count / 1000;
// Average -- divide by how many
if ((prepr > thres_reproduce)&&(boids.length>100)) {
prepr = thres_reproduce;
}
if (prepr >= random()) {
boids[boids.length] = new Boid(this.position.x, this.position.y);
}
}
}
// Pred class
class Pred extends Boid{
constructor(x, y) {
super(x, y);
this.velocity.mult(2); //unit vector
this.maxspeed = 4; // Maximum speed
this.maxforce = 0.1;
this.killed = 0;
}
run(boids, preds) {
this.flock(boids, preds);
this.kill(boids);
this.reproduce(preds);
this.update();
this.borders();
this.render();
}
// No real flocking for the predators
flock(boids, preds) {
let coh = this.cohesion(boids); // Cohesion
let sep = this.separate(preds); // Separation
// Arbitrarily weight these forces
coh.mult(1.0);
sep.mult(1.0);
// Add the force vectors to acceleration
this.applyForce(coh);
this.applyForce(sep);
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
cohesion(boids) {
let neighbordist = 80;
let sum = createVector(0, 0); // Start with empty vector to accumulate all locations
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].position); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return this.seek(sum); // Steer towards the location
} else {
return createVector(0, 0);
}
}
// Separation
// Method checks for nearby boids and steers away
separate(boids) {
let desiredseparation = 100;
let steer = createVector(0, 0);
let count = 0;
// For every boid in the system, check if it's too close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
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(count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
// Kill
// Method checks for nearby boids and kill them
kill(boids) {
let killradius = 20.0;
let count = 0;
let prepr = 0;
let removeValFromIndex = [];
// For every boid in the system, check if it's close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < killradius)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
removeValFromIndex[count] = i;
count++; // Keep track of how many
}
}
//remove the killed boids
for (var i = removeValFromIndex.length -1; i >= 0; i--) {
boids.splice(removeValFromIndex[i],1);
}
this.killed += count;
}
// Reproduce
// Method checks for number of killed boids and reproduce accordingly
reproduce(preds) {
if (this.killed/200 >= random()) {
preds[preds.length] = new Pred(this.position.x, this.position.y);
this.killed = 0;
}
}
// Draw predators as circle + triangle (aka pacman)
render() {
//fill(255, 140, 130);
//noStroke();
//ellipse(this.position.x, this.position.y, 20, 20);
var angle = atan2(this.velocity.y, this.velocity.x);
fill(255, 140, 130);
noStroke();
arc(this.position.x, this.position.y, 20, 20, angle-2.5+PI, angle+2.5+PI, PIE);
}
}