-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrey.cpp
More file actions
214 lines (173 loc) · 5.33 KB
/
Prey.cpp
File metadata and controls
214 lines (173 loc) · 5.33 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
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
//****************************************************************************
//
// Prey
//
//****************************************************************************
// ===========================================================================
// Libraries
// ===========================================================================
#include <math.h>
// ===========================================================================
// Project Files
// ===========================================================================
#include "Prey.h"
#include "DefVal.h"
//############################################################################
// #
// Class Prey #
// #
//############################################################################
// ===========================================================================
// Definition of static attributes
// ===========================================================================
// ===========================================================================
// Constructors
// ===========================================================================
Prey::Prey(double x, double y) :
Agent(x,y,DefVal::PREY_HITBOX,DefVal::PREY_RADIUS,DefVal::PREY_COLOR)
{
}
Prey::Prey(double x, double y, unsigned int c) : Agent(x,y,DefVal::PREY_HITBOX,DefVal::PREY_RADIUS,c)
{
}
// ===========================================================================
// Destructor
// ===========================================================================
Prey::~Prey(void)
{
}
// ===========================================================================
// Public Methods
// ===========================================================================
void Prey::move(Border* borders, unsigned int nb_b, Agent** tab, int index, unsigned int nb_a, Obstacle* obs, unsigned int nb_o)
{
// Wind
speed_from_borders(borders, nb_b);
// Index and number of agents in the radius of detection and in his hitbox
int i,k1 = 0, k2 = 0;
// Global change of speed
double dvx1 = 0, dvx2 = 0, dvy1 = 0, dvy2 = 0;
for(i = 0; i < nb_a; i++)
{
double* dv;
if(i != index)
{
dv = tab[i]->speed_for_preys(x,y,dx,dy,r);
// If a speed is induced
if( (dv[0]!=0) && (dv[1]!= 0) ){
switch((int)dv[2])
{
// If prey in radius
case 0 :
k1 ++;
dvx1 += dv[0];
dvy1 += dv[1];
break;
// If prey in radius but too close
case 1 :
k2 ++;
dvx2 += dv[0];
dvy2 += dv[1];
break;
// If hunter
case 2 :
ndx += dv[0];
ndy += dv[1];
break;
}
}
}
}
// Compute new speeds
if(k1 != 0)
{
ndx += (dvx1 / (double) k1);
ndy += (dvy1 / (double) k1);
}
if(k2 != 0)
{
ndx += (dvx2 / (double) k2);
ndy += (dvy2 / (double) k2);
}
// Obstacles
for(i = 0; i < nb_o; i++)
if( ( (obs[i].x - x)*(obs[i].x - x) + (obs[i].y - y)*(obs[i].y - y) ) < (hitbox+obs[i].r)*(hitbox+obs[i].r))
{
// V3
ndx += - DefVal::GAMMA3o * (obs[i].x - x);
ndy += - DefVal::GAMMA3o * (obs[i].y - y);
}
// Max speed
if (ndx*ndx > (DefVal::MAX_PREY_SPEED)*(DefVal::MAX_PREY_SPEED))
ndx /= (abs(ndx)/DefVal::MAX_PREY_SPEED);
if (ndy*ndy > (DefVal::MAX_PREY_SPEED)*(DefVal::MAX_PREY_SPEED))
ndy /= (abs(ndy)/DefVal::MAX_PREY_SPEED);
}
double* Prey::speed_for_preys(double x2, double y2, double dx2, double dy2, double r2)
{
// Returned induced speed
double* res = new double [3];
res[0] = 0;
res[1] = 0;
res[2] = 0;
// If the prey is alive
if(state != 1)
{
// Distance between the 2 points
double dis = (x-x2)*(x-x2) + (y - y2)*(y-y2);
if(dis < hitbox*hitbox)
{
// V3
res[0] = - DefVal::GAMMA3 * (dx - dx2);
res[1] = - DefVal::GAMMA3 * (dy - dy2);
// V2
res[0] += DefVal::GAMMA2 * (x - x2);
res[1] += DefVal::GAMMA2 * (y - y2);
res[2] = 1;
}
else
{
if(dis < r2*r2)
{
// V1
res[0] = DefVal::GAMMA1 * (dx - dx2);
res[1] = DefVal::GAMMA1 * (dy - dy2);
// V2
res[0] += DefVal::GAMMA2 * (x - x2);
res[1] += DefVal::GAMMA2 * (y - y2);
}
}
}
return res;
}
double* Prey::speed_for_hunters(double x2, double y2, double r2)
{
// Returned induced speed
double* res = new double [3];
res[0] = 0;
res[1] = 0;
res[2] = 0;
// If the prey is alive
if(state != 1)
{
double dis = sqrt((x-x2)*(x-x2) + (y - y2)*(y-y2));
if( dis < r2)
{
res[0] = ((x - x2) * DefVal::HUNTING_SPEED) / dis;
res[1] = ((y - y2) * DefVal::HUNTING_SPEED) / dis;
}
res[2] = dis;
if( dis < hitbox)
{
state = 1;
res[2] = -1;
}
}
return res;
}
// ===========================================================================
// Protected Methods
// ===========================================================================
// ===========================================================================
// Non inline accessors
// ===========================================================================