-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bolinha.cpp
330 lines (201 loc) · 7.15 KB
/
Bolinha.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
/**
* USP Sao Carlos, ICMC
* Agario Evolutivo
*
* Classe dos personagens, que sao capazes de se mover e absorver outros personagens e comidas
*
* @author Andre Santana Fernandes <11208537>
* @author Diogo Castanho Emídio <11297274>
* @author Leonardo Antonetti da Motta <11275338>
* @author Marcus Vinicius Santos Rodrigues <11218862>
* @author Olavo Morais Borges Pereira <11297792>
*
*/
#include "Bolinha.h"
#define CIRCLE_SEGMENTS 8
#define PI 3.1415
using namespace std;
// Desenha um poligono de n pontos
void DrawCircle(double cx, double cy, double r, int num_segments) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < num_segments; i++) {
double theta = 2.0f * PI * double(i) / double(num_segments); //get the current angle
double x = r * cosf(theta); //calculate the x component
double y = r * sinf(theta); //calculate the y component
glVertex2f(x + cx, y + cy); //output vertex
}
glEnd();
}
// Construtor
Bolinha::Bolinha(double _axonsIn[][N_NEURONS], double _axonsOut[][N_OUTPUTS], double _mass, double _x, double _y, double _r, double _g, double _b, double _horizontal, double _vertical, vector<Bolinha>& players) {
mass = _mass;
x = _x;
y = _y;
r = _r; g = _g; b = _b;
horizontal = _horizontal;
vertical = _vertical;
active = true;
closestFood = NULL;
closestEnemy = NULL;
// Cria a rede neural
double _inputs[] = {0, 0, 0, 0, 0, 0};
double _biasNeuron = 0;
double _biasOutput = -0;
redeNeural = new RedeNeural(_inputs, _biasNeuron, _biasOutput);
redeNeural->setAxonsIn(_axonsIn);
redeNeural->setAxonsOut(_axonsOut);
players.push_back(*this);
}
double Bolinha::Mass() {
return isnan(mass) ? 0 : mass;
}
// Calcula o raio com base na massa
double Bolinha::Radius() {
double radius = sqrt( mass / PI );
return isnan(radius) ? 0 : radius;
}
// Calcula a velocidade com base na massa
double Bolinha::Speed() {
double speed = pow(Radius(), -0.439) * 2.2;
return isnan(speed) ? 0 : speed;
}
// Desenha o poligono
void Bolinha::Draw() {
glColor3f(r, g, b);
DrawCircle(x, y, Radius(), CIRCLE_SEGMENTS);
glEnd();
}
// Joga os inputs na rede neural e move o personagem segundo o output
void Bolinha::Move() {
double _inputs[] = { DistanceToClosestEnemy(), AngleToClosestEnemy() / 180, ClosestEnemyMass(), Mass(), DistanceToClosestFood(), AngleToClosestFood() / 180 };
redeNeural->setInput(_inputs);
redeNeural->feedForward();
double *_outputs = redeNeural->getOutput();
horizontal = ( _outputs[0] * 2 ) - 1;
vertical = ( _outputs[1] * 2 ) - 1;
RedeNeural::structAxons playerAxons = redeNeural->getAxons();
// Move o personagem
x += horizontal * Speed() / 500;
y += vertical * Speed() / 500;
// Impede que o personagem saia da tela
x = x > 1 || x < -1 ? x*-1 : x;
y = y > 1 || y < -1 ? y*-1 : y;
// x = x + Radius() > 1 ? 1 - Radius() : x;
// x = x - Radius() < -1 ? -1 + Radius() : x;
// y = y + Radius() > 1 ? 1 - Radius() : y;
// y = y - Radius() < -1 ? -1 + Radius() : y;
}
// Calcula a colisao do personagem com outros personagens
// Define o inimigo mais proximo
void Bolinha::Collide(vector<Bolinha>& players) {
int playersLength = players.size();
double currentDistance = -1;
if( playersLength > 0 ) {
for(int i=0; i<playersLength; i++) {
if( &(players[i]) != this && players[i].isActive() ) {
double distance = sqrt(pow(players[i].x - x, 2) + pow(players[i].y - y, 2) * 1.0);
if( currentDistance == -1 ) {
closestEnemy = &players[i];
currentDistance = distance;
} else if( distance < currentDistance ) {
closestEnemy = &players[i];
currentDistance = distance;
}
// Se estao colidindo
if( (distance*2) < Radius() + players[i].Radius() ) {
if( mass > players[i].mass ) {
mass += players[i].mass;
players[i].setActive(false);
// players.erase(players.begin() + i);
}
}
}
}
} else {
closestEnemy = NULL;
}
}
// Calcula a colisao do personagem com comidas
// Define a comida mais proxima
void Bolinha::Collide(vector<Comida>& comidas) {
int comidasLength = comidas.size();
double currentDistance = -1;
if( comidasLength > 0 ) {
for(int i=0; i<comidasLength; i++) {
if( comidas[i].isActive() ) {
double distance = sqrt(pow(comidas[i].x - x, 2) + pow(comidas[i].y - y, 2) * 1.0);
if( currentDistance == -1 ) {
closestFood = &comidas[i];
currentDistance = distance;
} else if( distance < currentDistance ) {
closestFood = &comidas[i];
currentDistance = distance;
}
// Se estao colidindo
if( distance < Radius() + comidas[i].Radius() ) {
mass += comidas[i].mass;
comidas[i].setActive(false);
// comidas.erase(comidas.begin() + i);
}
}
}
} else {
closestFood = NULL;
}
}
// Calcula a distancia ate a comida mais proxima
double Bolinha::DistanceToClosestFood() {
if( closestFood != NULL ) {
double currentDistance = sqrt(pow((*closestFood).x - x, 2) + pow((*closestFood).y - y, 2) * 1.0);
return isnan(currentDistance) ? 0 : currentDistance;
}
return -1;
}
// Calcula o angulo ate a comida mais proxima
double Bolinha::AngleToClosestFood() {
if( closestFood != NULL ) {
double dx = (*closestFood).x - x;
double dy = (*closestFood).y - y;
double inRads = atan2(dy, dx);
double angle = inRads * 180.0 / PI;
if(angle < 0) angle += 360;
return isnan(angle) ? 0 : angle;
} else {
return -1;
}
}
// Calcula a distancia ate o inimigo mais proximo
double Bolinha::DistanceToClosestEnemy() {
if( closestEnemy != NULL ) {
double currentDistance = sqrt(pow((*closestEnemy).x - x, 2) + pow((*closestEnemy).y - y, 2) * 1.0);
return isnan(currentDistance) ? 0 : currentDistance;
}
return -1;
}
// Calcula o angulo ate o inimigo mais proximo
double Bolinha::AngleToClosestEnemy() {
if( closestEnemy != NULL ) {
double dx = (*closestEnemy).x - x;
double dy = (*closestEnemy).y - y;
double inRads = atan2(dy, dx);
double angle = inRads * 180.0 / PI;
if(angle < 0) angle += 360;
return isnan(angle) ? 0 : angle;
} else {
return -1;
}
}
// Retorna a massa do inimigo mais proximo
double Bolinha::ClosestEnemyMass() {
if( closestEnemy != NULL ) {
double mass = (*closestEnemy).mass;
return isnan(mass) ? 0 : mass;
}
return -1;
}
bool Bolinha::isActive() {
return this->active;
}
void Bolinha::setActive(bool active) {
this->active = active;
}