-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
237 lines (204 loc) · 8.06 KB
/
main.cpp
File metadata and controls
237 lines (204 loc) · 8.06 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
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <time.h>
#include <vector>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include "vector3.hpp"
#include "triangle.hpp"
#include "mesh.hpp"
#include "camera.hpp"
#include "object.hpp"
#include "objects2d.hpp"
#include "evolutionSim.hpp"
#include "quaternion.hpp"
#include "color.hpp"
#include "whale.hpp"
#include "fish.hpp"
#include "SliderSFML.hpp"
#include "scene.hpp"
#include "loadDataFunctions.hpp"
#include "garbage.hpp"
#include "bnfGroup.hpp"
//#include "ResourcePath.hpp"
using namespace sf;
using namespace std;
int agent::nextID = 0;
int main(int, char const**) {
srand(time(NULL));
// MARK: Program vars
const int width = 800, height = 800, framerate = 60;
bool active = true, qDown = false, wDown = false, eDown = false,
aDown = false, sDown = false, dDown = false, lcontrolDown = false,
jDown = false, kDown = false, lDown = false, iDown = false,
leftDown = false, hasStarted = false, updated = false;
RenderWindow window(VideoMode(width, height), "SFML window");
window.setFramerateLimit(framerate);
camera cam(vector3(0, 150, -80),
vector3(0.253319, -0.590397, 0.76476).normalized(),
vector3(0.185644, 0.805622, 0.560449).normalized(),
vector3(-0.948137, 0, 0.314062).normalized(), 3.14159 / 3, 1);
Font font;
// if(!font.loadFromFile("/Users/michael/Downloads/oswald.ttf")){
if(!font.loadFromFile("Resources/oswald.ttf")){
cout<< "font error \n";
}
int numWhales = 10;
Text cohTxt, sepTxt, aliTxt, avoTxt, whaleInpTxt;
cohTxt.setString("Fish Cohesion");
sepTxt.setString("Fish Separation");
aliTxt.setString("Fish Alignment");
avoTxt.setString("Fish Avoidance");
cohTxt.setFont(font);
sepTxt.setFont(font);
aliTxt.setFont(font);
avoTxt.setFont(font);
cohTxt.setPosition(5,10);
sepTxt.setPosition(5,70);
aliTxt.setPosition(5,130);
avoTxt.setPosition(500,10);
Text titleTxt("Whale Bubble Net Feeding Simulation" ,font,45);
Text subTxt("by Bronte McKinnis, Elaine Demetrion, Jack Weinberg and Michael Schuff ",font,22);
Text startTxt("Start",font, 35);
subTxt.setPosition(50, 250);
titleTxt.setPosition(50, 150);
RectangleShape button;
button.setSize(Vector2f(140,60));
button.setFillColor(Color::White);
button.setPosition(50,500);
startTxt.setPosition(50,500);
startTxt.setFillColor(Color::Black);
SliderSFML coh(5, 30);
SliderSFML sep(5, 90);
SliderSFML ali(5, 150);
SliderSFML avo(500, 30);
coh.create(0, 1);
sep.create(0, 1);
ali.create(0, 1);
avo.create(0, 1);
coh.setSliderValue(0.1);
sep.setSliderValue(1);
ali.setSliderValue(0.25);
avo.setSliderValue(0);
whaleInpTxt.setString("Adjust the amount of whales 10 to 20");
whaleInpTxt.setFont(font);
whaleInpTxt.setPosition(50,390);
SliderSFML whaleInp(50, 370);
whaleInp.create(10,20);
whaleInp.setSliderValue(10);
double sensitivity = 0.05, speed = 20;
vector<color> fish_colors;
evolutionSim simulation(numWhales, 300, vector3(100, 100, 100), framerate);
mesh fish_mesh = getFishMesh();
mesh whale_mesh = getWhaleMesh();
scene world(getPoints(), getLines(), vector<triangle>(0), getBodies(simulation.fishList, simulation.whaleList, fish_mesh, whale_mesh));
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
switch (event.type) {
case Event::Closed: {
window.close();
break;
} case Event::KeyPressed: {
HandleKeyPressedEvent;// garbage.hpp
break;
} case Event::KeyReleased: {
HandleKeyReleasedEvent;// garbage.hpp
break;
} case Event::MouseWheelScrolled: {
for (int i = 0; i < event.mouseWheelScroll.delta; i++) {
speed *= 1.01;
}
for (int i = 0; i > event.mouseWheelScroll.delta; i--) {
speed /= 1.01;
}
break;
} case Event::MouseButtonPressed: {
if (event.mouseButton.button == Mouse::Left) {
leftDown = true;
}
break;
} case Event::MouseButtonReleased: {
if (event.mouseButton.button == Mouse::Left) {
leftDown = false;
}
break;
}
}
}
window.clear();
if (!hasStarted) {
window.draw(titleTxt);
window.draw(subTxt);
window.draw(button);
window.draw(startTxt);
whaleInp.draw(window);
window.draw(whaleInpTxt);
numWhales = (int)whaleInp.getSliderValue();
//evolutionSim.
Vector2i mouse_position = Mouse::getPosition(window);
if (leftDown and button.getGlobalBounds().contains(mouse_position.x, mouse_position.y)) {
hasStarted = true;
}
} else {
if(!updated){
simulation = evolutionSim(numWhales, 300, vector3(100, 100, 100), framerate);
world = scene(getPoints(), getLines(), vector<triangle>(0), getBodies(simulation.fishList, simulation.whaleList, fish_mesh, whale_mesh));
updated = true;
}
MoveCamera;// garbage.hpp
simulation.updateSim(coh.getSliderValue(), sep.getSliderValue(), ali.getSliderValue(), avo.getSliderValue());
world.meshes.clear();
for (int i = 0; i < simulation.fishList.size() + simulation.whaleList.size(); i++) {
if (i < simulation.fishList.size()) {
if (simulation.fishList[i].velocity.magnitude() > 0) {
quaternion q = get_quaternion(vector3(1, 0, 0), simulation.fishList[i].velocity);
world.meshes.push_back(simulation.fishList[i].position + fish_mesh.rotated(q));
} else {
world.meshes.push_back(simulation.fishList[i].position + fish_mesh);
}
} else {
quaternion q = get_quaternion(vector3(1, 0, 0), simulation.whaleList[i-simulation.fishList.size()].velocity);
world.meshes.push_back(simulation.whaleList[i-simulation.fishList.size()].position + whale_mesh.rotated(q));
}
}
// MARK: Get SFML Shapes
vector<triangle> tris = cam.get_view(world);
for (triangle tri: tris) {
if (tri.v2 != tri.v3) {
ConvexShape shape(3);
shape.setPoint(0, Vector2f(tri.v1.x, height-tri.v1.z));
shape.setPoint(1, Vector2f(tri.v2.x, height-tri.v2.z));
shape.setPoint(2, Vector2f(tri.v3.x, height-tri.v3.z));
shape.setFillColor(Color(tri.c.r, tri.c.g, tri.c.b));
window.draw(shape);
} else {
float dx = tri.v2.x - tri.v1.x;
float dy = tri.v2.z - tri.v1.z;
RectangleShape shape(Vector2f(sqrt(dx*dx + dy*dy), 1));
shape.setOrigin(0.5, 0.5);
shape.setPosition(tri.v1.x, height-tri.v1.z);
shape.rotate(-atan2(dy, dx) * 180.0 / 3.14159);
shape.setFillColor(Color(tri.c.r, tri.c.g, tri.c.b));
window.draw(shape);
}
}
//MARK: DRAW THEM
coh.draw(window);
sep.draw(window);
ali.draw(window);
avo.draw(window);
window.draw(cohTxt);
window.draw(sepTxt);
window.draw(aliTxt);
window.draw(avoTxt);
}
window.display();
}
return EXIT_SUCCESS;
}