-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfishHandler.cpp
More file actions
239 lines (176 loc) · 9.54 KB
/
fishHandler.cpp
File metadata and controls
239 lines (176 loc) · 9.54 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "fishHandler.h"
FishHandler::FishHandler(){
this->allFish = nullptr;
int boxes_rows = FRUSTUM_HALF_WIDTH*Global::aspect_ratio/FISH_SIGHT_RANGE + 2;
int boxes_cols = FRUSTUM_HALF_WIDTH/FISH_SIGHT_RANGE + 2;
this->boxes = new box_struct*[boxes_rows];
for(int i = 0; i < boxes_rows; i++){
this->boxes[i] = new box_struct[boxes_cols];
}
for(int i = 0; i < boxes_rows; i++){
for(int j = 0; j < boxes_cols; j++){
this->boxes[i][j].fish_indexes = nullptr;
}
}
this->update_num_of_fish();
this->update_cohesion_intensity();
this->update_alignment_intensity();
this->update_separation_intensity();
this->update_edge_evasion_intensity();
}
FishHandler::~FishHandler(){
delete[] this->allFish;
int boxes_rows = FRUSTUM_HALF_WIDTH*Global::aspect_ratio/FISH_SIGHT_RANGE + 2;
int boxes_cols = FRUSTUM_HALF_WIDTH/FISH_SIGHT_RANGE + 2;
for(int i = 0; i < boxes_rows; i++){
for(int j = 0; j < boxes_cols; j++){
delete this->boxes[i][j].fish_indexes;
}
delete[] this->boxes[i];
}
delete[] this->boxes;
}
void FishHandler::update_num_of_fish(){
this->number_of_fish = Serializer::number_of_fish;
int boxes_rows = FRUSTUM_HALF_WIDTH*Global::aspect_ratio/FISH_SIGHT_RANGE + 2;
int boxes_cols = FRUSTUM_HALF_WIDTH/FISH_SIGHT_RANGE + 2;
for(int i = 0; i < boxes_rows; i++){
for(int j = 0; j < boxes_cols; j++){
delete this->boxes[i][j].fish_indexes;
}
}
for(int i = 0; i < boxes_rows; i++){
for(int j = 0; j < boxes_cols; j++){
this->boxes[i][j].fish_indexes = new int[this->number_of_fish];
this->boxes[i][j].num_of_boxed_fish = 0;
}
}
this->createFish();
}
void FishHandler::update_cohesion_intensity(){
this->cohesionIntensity = Serializer::cohesion_intensity;
}
void FishHandler::update_separation_intensity(){
this->separationIntensity = Serializer::separation_intensity;
}
void FishHandler::update_alignment_intensity(){
this->alignmentIntensity = Serializer::alignment_intensity;
}
void FishHandler::update_edge_evasion_intensity(){
this->edgeEvasionIntensity = Serializer::edge_evasion_intensity;
}
void FishHandler::createFish(){
delete[] this->allFish;
this->allFish = new Fish[this->number_of_fish];
float distances[NUM_OF_JOINTS] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f}; // desired distances from one circle center to another
float radii[NUM_OF_JOINTS] = {1.2f, 1.3f, 1.4f, 1.4f, 1.3f, 1.2f, 1.0f, 0.8f, 0.6f, 0.5f, 0.3f, 0.2f}; // radii of each visible circle
glm::vec2 centers[NUM_OF_JOINTS] = { // position of each circle center, the values get changed the moment the fish update joints func gets called so they can be 0.0
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
glm::vec2(0.0f, 0.0f),
};
for(int i = 0; i < this->number_of_fish; i++){
centers[0] = glm::vec2((Global::GetRandomFloat()*2.0f - 1.0f), Global::GetRandomFloat()*2.0f - 1.0f) * // place fish on a random location on screen
glm::vec2(-FRUSTUM_HALF_WIDTH * 0.8f, FRUSTUM_HALF_WIDTH*Global::aspect_ratio * 0.8f); // * 0.8f is practically 20% padding so fish dont go off-screen
this->allFish[i] = Fish(centers, distances, radii, i);
}
}
void FishHandler::boxTheFish(){
int boxes_rows = FRUSTUM_HALF_WIDTH*Global::aspect_ratio/FISH_SIGHT_RANGE + 2;
int boxes_cols = FRUSTUM_HALF_WIDTH/FISH_SIGHT_RANGE + 2;
for(int i = 0; i < this->number_of_fish; i++){
int box_row_index = this->allFish[i].joints[0].Center.x/FISH_SIGHT_RANGE + 1;
if(box_row_index < 1) box_row_index = 1;
else if(box_row_index > boxes_rows - 2) box_row_index = boxes_rows - 2;
int box_col_index = this->allFish[i].joints[0].Center.x/FISH_SIGHT_RANGE + 1;
if(box_col_index < 1) box_col_index = 1;
else if(box_col_index > boxes_rows - 2) box_col_index = boxes_rows - 2;
this->boxes[box_row_index][box_col_index].fish_indexes[this->boxes[box_row_index][box_col_index].num_of_boxed_fish++] = this->allFish[i].fishID;
this->allFish[i].box_coords.x = box_row_index;
this->allFish[i].box_coords.y = box_col_index;
}
}
void FishHandler::resetBoxSizes(){
int boxes_rows = FRUSTUM_HALF_WIDTH*Global::aspect_ratio/FISH_SIGHT_RANGE + 2;
int boxes_cols = FRUSTUM_HALF_WIDTH/FISH_SIGHT_RANGE + 2;
for(int i = 0; i < boxes_rows; i++){
for(int j = 0; j < boxes_rows; j++){
this->boxes[i][j].num_of_boxed_fish = 0;
}
}
}
void FishHandler::handleFishMovement(float delta_time){
for(int i = 0; i < this->number_of_fish; i++){
Fish& fish = this->allFish[i];
glm::vec2 resultDir = fish.joints[0].moveDirection; // the final direction that will be passed to the fish's Move function
glm::vec2 separationVec = glm::vec2(0.0f);
glm::vec2 alignmentVec = glm::vec2(0.0f);
glm::vec2 cohesionVec = glm::vec2(0.0f);
int counter = 0; // counter used for alignment and cohesion
int separationCounter = 0; // counter used only for the separation
const float separationSight = 0.3f * FISH_SIGHT_RANGE; // other fish inside this 'sight' field will be accounted for in the separation part of the boid simulation
const float otherSight = 1.0f * FISH_SIGHT_RANGE; // other fish inside this 'sight' field will be accounted for in the alignment and cohesion part of the boid simulation
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
for(int k = 0; k < this->boxes[fish.box_coords.x+i][fish.box_coords.y+j].num_of_boxed_fish; k++){
unsigned int someFishId = this->boxes[fish.box_coords.x+i][fish.box_coords.y+j].fish_indexes[k];
const Fish& f = this->allFish[someFishId];
if(fish.fishID != f.fishID){
if(glm::length(fish.joints[0].Center- f.joints[0].Center) < separationSight && glm::dot(resultDir, f.joints[0].Center- fish.joints[0].Center) > -0.7){
separationVec += fish.joints[0].Center- f.joints[0].Center;
alignmentVec += f.joints[0].moveDirection;
cohesionVec += f.joints[0].Center;
separationCounter++;
counter++;
}
else if(glm::length(fish.joints[0].Center- f.joints[0].Center) < otherSight && glm::dot(resultDir, f.joints[0].Center- fish.joints[0].Center) > -0.7){
alignmentVec += f.joints[0].moveDirection;
cohesionVec += f.joints[0].Center;
counter++;
}
}
}
}
}
resultDir = Global::rotateVector(resultDir, fish.hit_checks_result * edgeEvasionIntensity * delta_time); // try not to go off-screen
if(separationCounter){
float separationNum = glm::length(separationVec/(float)separationCounter); // used to map the length of separationVec from [0, separationSight] to [0, 1] but in reverse
separationNum = 1.0f - separationNum/separationSight; // so if the length is closer to 0 it will be mapped closer to 1 so that if other fish are closer
// the observed fish will want to get away from them more
separationNum = powf(separationNum, 0.01f); // map the onto a nice curve
float rotationDir;
if(glm::cross(glm::vec3(resultDir, 0.0f), glm::vec3(separationVec, 0.0f)).z > 0)
rotationDir = 1.0f;
else
rotationDir = -1.0f;
resultDir = Global::rotateVector(resultDir, rotationDir * separationNum * separationIntensity * delta_time);
}
if(counter){
alignmentVec = alignmentVec/(float)counter;
cohesionVec = cohesionVec/(float)counter;
float alignmentAngleDelta = Global::angleOfVector(alignmentVec) - Global::angleOfVector(resultDir); // difference of the observed fish's move dir and the other fishes' move dir
float alignmentFactor = (float)counter / (float)(this->number_of_fish - 1); // more fish around observed fish -> more influence on the observed fish's move direciton
resultDir = Global::rotateVector(resultDir, alignmentAngleDelta * alignmentFactor * alignmentIntensity * delta_time);
float rotationDir;
if(glm::cross(glm::vec3(resultDir, 0.0f), glm::vec3(cohesionVec, 0.0f)).z > 0)
rotationDir = 1.0f;
else
rotationDir = -1.0f;
resultDir = Global::rotateVector(resultDir, rotationDir * glm::length(cohesionVec) * cohesionIntensity * delta_time); // just go to the average position of close fish
}
resultDir = Global::rotateVector(resultDir, (float)std::sin((glfwGetTime() + fish.fishID * 7.0f) * 2.0f) * 50.0f * delta_time); // make the fish wiggle while swimming
fish.updateMoveDir(resultDir);
}
for(int i = 0; i < this->number_of_fish; i++){
this->allFish[i].Move(delta_time);
}
}