-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGalaxy.cpp
271 lines (241 loc) · 6.75 KB
/
Galaxy.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
#include "Galaxy.h"
Galaxy::Galaxy(unsigned int amount, sf::Vector2i screen)
{
Root = nullptr;
velocities.reserve(amount);
forces.reserve(amount);
for (int i = 0; i < amount; i++)
add(sf::Vector2i(rand() % screen.x, rand() % screen.y));
for (int i = -200; i < 200; i+=5){
for (int j = -200; j < 200; j+=5){
// add(sf::Vector2i(j+400,i+300));
//velocities.back().x = -6000;
// add(sf::Vector2i(j + 400, i + 300));
//velocities.back().x = (rand() % 15000) -7500;
//velocities.back().y = (rand() % 15000) -7500;
}
}
for (int i = 0; i <4000; i++){
float phi = rand() % 10000 * (2 * M_PI * 10000);
int r = rand() % 300;
add(sf::Vector2i((int)r*cos(phi)+400, (int)r*sin(phi)+400));
velocities.back() = sf::Vector2f(-(0.09*r*r+2000) * sin(phi),(0.09*r*r+2000) * cos(phi));
}
}
Galaxy::~Galaxy()
{
clear(Root);
}
void Galaxy::add(sf::Vector2i pos)
{
stars.append(sf::Vertex(sf::Vector2f(pos.x, pos.y), sf::Color::White));
insert(stars.getVertexCount() - 1);
velocities.push_back(sf::Vector2f(0, 0));
forces.push_back(sf::Vector2f(0, 0));
}
void Galaxy::clear(StarNode* star)
{
if (star == nullptr)return;
for (int c = 0; c < 4; clear(star->quads[c++]));
delete star;
}
void Galaxy::update(float timestep)
{
InfoString.clear();
InfoString += "Stars: " + to_string((int)stars.getVertexCount()) + '\n';
for (auto& f : forces){ f = sf::Vector2f(0, 0); }
calcForce(Root, timestep);
step(timestep);
clear(Root);
Root->mass = 1;
Root = nullptr;
for (int i = 0; i < stars.getVertexCount(); i++)
insert(i);
//std::cout << time(0) << ' ' <<Root->_1<< ' ' <<Root->_2<< ' ' <<Root->_3<< ' ' <<Root->_4<< std::endl;
}
void Galaxy::render(sf::RenderWindow& window,sf::Shader& glow)
{
window.draw(stars,&glow);
//window.draw(DebugCOM);
//drawBoxes(window,Root);
}
void Galaxy::drawBoxes(sf::RenderWindow& window, StarNode* node)
{
sf::RectangleShape rect;
rect.setOutlineColor(sf::Color(255, 255, 255, 30));
rect.setOutlineThickness(1);
rect.setFillColor(sf::Color::Transparent);
for (int i = 0; i < 4; i++){
if (node->quads[i] != nullptr)
drawBoxes(window, node->quads[i]);
else{
rect.setPosition(node->region.x, node->region.y);
rect.setSize(sf::Vector2f(node->region.z - 1, node->region.z - 1));
window.draw(rect);
}
}
}
Galaxy::StarNode::StarNode(unsigned int index)
{
this->index = index;
quads[0] = quads[1] = quads[2] = quads[3] = nullptr;
mass = 1;
leaf = true;
}
Galaxy::StarNode::StarNode(sf::Vector3f reg, unsigned int index)
{
this->index = index;
quads[0] = quads[1] = quads[2] = quads[3] = nullptr;
mass = 1;
leaf = true;
region = reg;
}
void Galaxy::insert(unsigned int index)
{
if (Root == nullptr){
sf::FloatRect box = stars.getBounds();
Root = new StarNode(0);
Root->com.x = stars[0].position.x;
Root->com.y = stars[0].position.y;
Root->region = sf::Vector3f(box.left, box.top, std::max(box.width, box.height));
}
else
insert(Root, index, 0,true);
}
void Galaxy::insert(StarNode* node, unsigned int index, unsigned int depth, bool incmass)
{
if (depth > maxdepth) return;
if (node->leaf){
node->leaf = false;
depth++;
insert(node, node->index, depth,false);
insert(node, index, depth,true);
}
else { // Update COM, insert star
depth++;
node->com = (node->com*node->mass + stars[index].position) / (node->mass+1);
if (incmass)node->mass += 1.f;
int reg = getRegion(node->region, stars[index].position);
if (node->quads[reg] == nullptr){
node->quads[reg] = new StarNode(subregion(node->region, reg), index);
node->quads[reg]->com = stars[index].position;
}
else{
insert(node->quads[reg], index, depth,true);
node->quads[reg]->leaf = false;
}
}
}
void Galaxy::calcForce(StarNode* start, float timestep)
{
// "Force == Acceleration"
if (start == nullptr)return; // No stars
// Search individual stars and calculate forces
for (int reg = 0; reg < 4; reg++)
calcForce(start->quads[reg], timestep);
if (start->leaf){
calcForce(start, Root); // Start calculating at root
velocities[start->index] += forces[start->index] * timestep;
}
if (forces[start->index].x == 0){
InfoString[0] = 'E';
}
}
void Galaxy::calcForce(StarNode* ref, StarNode* other)
{ // Incoming target body ref
// other
static int callcount = 0;
if (other == nullptr)return;
StarNode** otherQuads = other->quads;
for (int q = 0; q < 4; q++){
if (otherQuads[q] != nullptr && otherQuads[q] != ref && ref != nullptr){
if (otherQuads[q]->leaf){
forces[ref->index] += newtonGravity(stars[ref->index].position,
stars[otherQuads[q]->index].position, 1.f);
}
else{
// Star cluster
if (otherQuads[q]->region.z / vabs(stars[ref->index].position - otherQuads[q]->com)<Theta){
// Cluster is far away
// Calculate force between cluster and star
forces[ref->index] += newtonGravity(stars[ref->index].position,
otherQuads[q]->com, otherQuads[q]->mass);
}
else{
// Cluster is nearby => Repeat procedure for clustered stars
for (int reg = 0; reg < 4; reg++)
calcForce(ref, otherQuads[q]->quads[reg]);
}
}
}
}
}
sf::Vector2f Galaxy::newtonGravity(sf::Vector2f target, sf::Vector2f other, float mass)
{
float r = vabs(other - target);
sf::Vector2f r1r2 = other - target;
return r1r2*mass*(_G_ / ((float)(pow(r, 2))));
}
void Galaxy::step(float timestep)
{
for (int i = 0; i < stars.getVertexCount(); i++)
stars[i].position += velocities.at(i)*timestep;
}
sf::Vector3f Galaxy::subregion(sf::Vector3f region, unsigned char q)
{ // q in {0,1,2,3}
switch (q){
case 0: break;
case 1: region.x += region.z / 2.f; break;
case 2: region.y += region.z / 2.f; break;
case 3: region.x += region.z / 2.f; region.y += region.z / 2.f; break;
default: std::cerr << "Invalid use of subregion" << std::endl;
}
region.z /= 2.f;
return region;
}
short Galaxy::getRegion(sf::Vector3f region, sf::Vector2f pos)
{
if (pos.x > region.x + region.z / 2.f){
if (pos.y > region.y + region.z / 2.f)
return 3;
else return 1;
}
else{
if (pos.y > region.y + region.z / 2.f)
return 2;
else return 0;
}
}
static float vabs(sf::Vector2f vec)
{
return sqrt(vec.x*vec.x + vec.y*vec.y);
}
static sf::Vector2f norm(sf::Vector2f v)
{
float a = vabs(v);
return sf::Vector2f(v.x / a, v.y / a);
}
sf::Vector2f operator+(sf::Vector2f v1, sf::Vector2f v2)
{
return sf::Vector2f(v1.x + v2.x, v1.y + v2.y);
}
sf::Vector2f operator-(sf::Vector2f v1, sf::Vector2f v2)
{
return sf::Vector2f(v1.x - v2.x, v1.y - v2.y);
}
sf::Vector2f operator*(sf::Vector2f v, float c)
{
return sf::Vector2f(c*v.x, c*v.y);
}
sf::Vector2f operator+(sf::Vector2f v, float c)
{
return sf::Vector2f(c + v.x, c + v.y);
}
sf::Vector2f operator-(sf::Vector2f v, float c)
{
return sf::Vector2f(v.x - c, v.y - c);
}
sf::Vector2f operator/(sf::Vector2f v, float c)
{
return sf::Vector2f(v.x / c, v.y / c);
}