-
Notifications
You must be signed in to change notification settings - Fork 0
/
boids.cpp
556 lines (362 loc) · 13.3 KB
/
boids.cpp
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// ===========================================================================
// Libraries
// ===========================================================================
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
// ===========================================================================
// Project Files
// ===========================================================================
#include "boids.h"
//############################################################################
// #
// Class boids
// #
//############################################################################
// ===========================================================================
// Constructors
// ===========================================================================
boids::boids(void)
{
dt = 0;
population = 0;
tab=NULL;
predator=NULL;
tab_object=NULL;
Getter=NULL;
disti = 0;
distc = 0;
distp = 0;
distk = 0;
g1 = 0;
g2 = 0;
g3 = 0;
g4 = 0;
speed = new double[10];
nb_object = 0;
w=0;
h=0;
nb_predator = 0;
speed_predator = 0;
width = 0;
height = 0;
speed_init = 0;
found_no_prey = new bool[nb_predator];
for (int i = 0; i < nb_predator; ++i)
{
found_no_prey[i]=true;
}
wait = 0;
predator_feed = new int[nb_predator];
for (int i = 0; i < nb_predator; ++i)
{
predator_feed[i]=0;
}
x_alea = 0;
y_alea = 0;
wind_force=0;
speed_limit=0;
vx_pred=0;
vy_pred=0;
}
// ===========================================================================
// Destructor
// ===========================================================================
boids::~boids(void)
{
delete [] tab;
delete [] speed;
delete [] Getter;
delete [] predator;
delete [] found_no_prey;
delete [] predator_feed;
}
// ===========================================================================
// Public Methods
// ===========================================================================
void boids::initialization(void)
{
//for the birds
tab = new individue[population];
for (int i = 0; i < population; ++i)
{
tab[i].Set_x( ((width-100)-100)*((double)rand()/(double)RAND_MAX)+100 );
tab[i].Set_y( ((height-100)-100)*((double)rand()/(double)RAND_MAX)+100 );
tab[i].Set_vx(2*speed_init*((double)rand()/(double)RAND_MAX)-speed_init);
tab[i].Set_vy(2*speed_init*((double)rand()/(double)RAND_MAX)-speed_init);
}
//for the predator
predator = new individue[nb_predator];
for (int i = 0; i < nb_predator; ++i)
{
predator[i].Set_x( ((width-100)-100)*((double)rand()/(double)RAND_MAX)+100 );
predator[i].Set_y( ((height-100)-100)*((double)rand()/(double)RAND_MAX)+100 );
}
Getter = new double[4*population];
//set the object
tab_object = new double[2*nb_object];
for (int i = 0; i < 2*nb_object; i+=2)
{
tab_object[i] = ((width-100)-100)*((double)rand()/(double)RAND_MAX)+100; //for x coordonate
tab_object[i+1] = ((height-100)-100)*((double)rand()/(double)RAND_MAX)+100; //for y coordonate
}
}
double boids::abs(double x)
{
if (x>0) {return x;}
if (x<0) {return -x;}
}
int boids::signe(double x)
{
if (x>0) {return 1;}
if (x<0) {return -1;}
}
void boids::deplacement(void)
{
//=============================================================================
//==================== Prey ===================
//==================== ===================
//=============================================================================
for (int ind=0; ind<population; ind++)
{
//initialisation
//=====================================================================
//for the coordonate of the prey ind
double x = tab[ind].Get_x();
double y = tab[ind].Get_y();
double vx = tab[ind].Get_vx();
double vy = tab[ind].Get_vy();
//for change the x, y
Getter[4*ind] = x + dt * vx;
Getter[4*ind+1] = y + dt * vy;
int ci = 0; //compteur individue around
int cc = 0; //compteur individue too close
int co = 0; //compteur object too close
//for initialize every rule
speed[0] = 0; //for vx from rule 1
speed[1] = 0; //for vy from rule 1
speed[2] = 0; //for vx from rule 2
speed[3] = 0; //for vy from rule 2
speed[4] = 0; //for vx from rule 3 inter individue
speed[5] = 0; //for vy from rule 3 inter individue
speed[6] = 0; //for vx from rule 3 object
speed[7] = 0; //for vy from rule 3 object
speed[8] = 0; //for vx from rule 4
speed[9] = 0; //for vy from rule 4
// for the rules
//======================================================================
for (int i = 0; i < population; ++i)
{
double x_other = tab[i].Get_x();
double y_other = tab[i].Get_y();
//distance prey prey
double a = sqrt((x - x_other)*(x - x_other) + (y - y_other)*(y - y_other)); //distance between 2 individue
if (a<=disti)
{
//rule 1
speed[0] += (tab[i].Get_vx() - vx);
speed[1] += (tab[i].Get_vy() - vy);
//rule 2
speed[2] += (x_other - x);
speed[3] += (y_other - y);
ci++;
}
//rule 3
if (a<=distc)
{
speed[4] += (x_other - x);
speed[5] += (y_other - y);
cc++;
}
if (i<nb_object)
{
double xo = tab_object[2*i];
double yo = tab_object[2*i+1];
//distance object prey
double a = sqrt((x - xo)*(x - xo) + (y - yo)*(y - yo));
if (a<=distc)
{
speed[6] += (xo - x);
speed[7] += (yo - y);
co++;
}
}
//rule 4
if (i<nb_predator)
{
double xp = predator[i].Get_x();
double yp = predator[i].Get_y();
//distance predator prey
double a = sqrt((x - xp)*(x - xp)+(y - yp)*(y - yp));
speed[8] = - (xp - x)/a;
speed[9] = - (yp - y)/a;
}
}
if (ci!=0)
{
speed[0] = speed[0]/ci;
speed[1] = speed[1]/ci;
speed[2] = speed[2]/ci;
speed[3] = speed[3]/ci;
}
if (cc!=0)
{
speed[4] = - speed[4]/cc;
speed[5] = - speed[5]/cc;
}
if (co!=0)
{
speed[6] = - speed[6]/co;
speed[7] = - speed[7]/co;
}
//for the speed
//==================================================================
vx = vx + dt*(g1*speed[0]+g2*speed[2]+g3*(speed[4]+speed[6])+g4*speed[8]); //for change the vx
vy = vy + dt*(g1*speed[1]+g2*speed[3]+g3*(speed[5]+speed[7])+g4*speed[9]); //for change the vy
//for the speed limit
//=================================================================
if (abs(vx)>speed_limit)
{
vx = signe(vx)*speed_limit;
}
if (abs(vy)>speed_limit)
{
vy = signe(vy)*speed_limit;
}
// for the wind
//==================================================================
if (x<50 && x>-99) {vx = vx +wind_force;}
if (x > width - 50) {vx = vx - wind_force;}
if (y<50 && y>-99) {vy = vy +wind_force;}
if (y > height - 50) {vy = vy -wind_force;}
//for the Getter because if no getter the value of vx, vy
//change at the end of the loop
//===================================================================
Getter[4*ind+2] = vx;
Getter[4*ind+3] = vy;
}
//=============================================================================
//==================== Predator ===================
//==================== ===================
//=============================================================================
//for mouvement of predators
for (int i = 0; i < nb_predator; ++i)
{
//printf("%lg %lg\n",predator[i].Get_vx(), predator[i].Get_vy());
//if the predator doesn't eat any prey
//========================================================================
if (predator_feed[i]==0 || predator_feed[i] > wait)
{
//initialisation
//===========================================================
//for the coordonate of the predator
double x_pred = predator[i].Get_x();
double y_pred = predator[i].Get_y();
//for reinitialize the parameter predator_feed for when the predator
//digeste the prey
predator_feed[i]=0;
// +1 for the case a = distp, min for find the distance min
// in the radius of the sight of this predator
double min = distp+1;
// for numero of the prey
int prey = population+1;
// find the next prey
//===========================================================
for (int pop = 0; pop < population; ++pop)
{
//distance predator prey
double a = sqrt((x_pred - tab[pop].Get_x())*(x_pred - tab[pop].Get_x())+(y_pred - tab[pop].Get_y())*(y_pred - tab[pop].Get_y()));
if (a<=distp && a<min)
{
prey = pop;
min = a;
}
}
//if it see a prey
//==========================================================
if (min <= distp && min > distk)
{
vx_pred=(tab[prey].Get_x() - x_pred)*speed_predator/min;
vy_pred=(tab[prey].Get_y() - y_pred)*speed_predator/min;
found_no_prey[i] = false;
}
//if it is out of reach for the prey for the first time
//==========================================================
if (min > distp && found_no_prey[i]==false)
{
x_alea = ((width-100)-100)*((double)rand()/(double)RAND_MAX)+100;
y_alea = ((height-100)-100)*((double)rand()/(double)RAND_MAX)+100;
double d = sqrt((x_pred - x_alea)*(x_pred - x_alea)+(y_alea - y_pred)*(y_alea - y_pred));
vx_pred=(x_alea - x_pred)*speed_predator/d;
vy_pred=(y_alea - y_pred)*speed_predator/d;
found_no_prey[i] = true;
}
//if the predator kill a boid
//===========================================================
if (min<=distk)
{
// printf("%lg\n", min);
// printf("%d\n", prey);
// //temporal tab of individue
// double tab_temp[4*(population-1)];
// for (int pop = 0; pop < population; ++pop)
// {
// //take information in the temporal tab
// if (pop != prey)
// {
// tab_temp[4*pop] = tab[pop].Get_x();
// tab_temp[4*pop+1] = tab[pop].Get_y();
// tab_temp[4*pop+2] = tab[pop].Get_vx();
// tab_temp[4*pop+3] = tab[pop].Get_vy();
// }
// }
// delete[] tab;
// tab=NULL;
// population = population - 1;
// //individue* tab = new individue[population];
// tab = new individue[population];
// for (int pop = 0; pop < population; ++pop)
// {
// tab[pop].Set_x(tab_temp[4*pop]);
// tab[pop].Set_y(tab_temp[4*pop+1]);
// tab[pop].Set_vx(tab_temp[4*pop+2]);
// tab[pop].Set_vy(tab_temp[4*pop+3]);
// }
// another idea
Getter[4*prey]=-1000;
Getter[4*prey+1]=-1000;
Getter[4*prey+2]=0;
Getter[4*prey+3]=0;
predator_feed[i]=1;
}
//for the wind
//=============================================================
if (x_pred<50) {vx_pred = - vx_pred;}
if (y_pred<50) {vy_pred = - vy_pred;}
if (x_pred>width-50) {vx_pred = - vx_pred;}
if (y_pred>height-50) {vy_pred = - vy_pred;}
//for set the coordonate
predator[i].Set_x(x_pred + dt*vx_pred);
predator[i].Set_y(y_pred + dt*vy_pred);
predator[i].Set_vx(vx_pred);
predator[i].Set_vy(vy_pred);
printf("%lg %lg %lg %lg\n", predator[i].Get_x(),predator[i].Get_y(),predator[i].Get_vx(),predator[i].Get_vy());
}
// if the predator had eaten
//===================================================================
if (predator_feed[i]>0 && predator_feed[i]<=wait)
{
predator_feed[i]++;
}
}
//for set the speed and coordonate
//===========================================================================
for (int i = 0; i < population; ++i)
{
tab[i].Set_x(Getter[4*i]); // for x
tab[i].Set_y(Getter[4*i+1]); //for y
tab[i].Set_vx(Getter[4*i+2]); //for vx
tab[i].Set_vy(Getter[4*i+3]); //for vy
}
}