-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.cpp
More file actions
349 lines (315 loc) · 12.9 KB
/
simulator.cpp
File metadata and controls
349 lines (315 loc) · 12.9 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "simulator.h"
std::vector<CollisionPoint> collisionPoints;
bool isSpacePressed() {
HWND foregroundWindow = GetForegroundWindow();
DWORD currentProcessId;
GetWindowThreadProcessId(foregroundWindow, ¤tProcessId);
DWORD thisProcessId = GetCurrentProcessId();
if (currentProcessId != thisProcessId) {
return false;
}
return GetAsyncKeyState(VK_SPACE) & 0x8000;
}
void restartExecutable(GLFWwindow* window) {
std::cout << "Restarting...\n";
run(window);
return;
}
void hsvToRgb(float h, float s, float v, float &r, float &g, float &b) {
int i = floor(h * 6);
float f = h * 6 - i;
float p = v * (1 - s);
float q = v * (1 - f * s);
float t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
}
void updateBodies(std::vector<Body>& particles, std::vector<Body*>& mobileParticles, double dt) {
const size_t maxOrbitSize = 1000;
const float squaredLimit = 1.5 * 1.5;
const double collisionThreshold = 0.000001;
#pragma omp parallel for
for (size_t i = 0; i < mobileParticles.size(); ++i) {
Body* particle = mobileParticles[i];
if (!particle->alive)
continue;
float fx = 0.0, fy = 0.0;
std::vector<CollisionPoint> localCollisionPoints;
for (size_t j = 0; j < particles.size(); ++j) {
Body* other = &particles[j];
if (particle == other || !other->alive)
continue;
float dx = particle->x - other->x;
float dy = particle->y - other->y;
double distanceSquared = dx * dx + dy * dy;
if (particle->collision && other->mobile && distanceSquared < collisionThreshold) {
float totalMass = particle->mass + other->mass;
float invTotalMass = 1.0 / totalMass;
#pragma omp critical
{
particle->vx = (particle->mass * particle->vx + other->mass * other->vx) * invTotalMass;
particle->vy = (particle->mass * particle->vy + other->mass * other->vy) * invTotalMass;
particle->mass = totalMass;
other->alive = false;
localCollisionPoints.emplace_back(CollisionPoint{particle->x, particle->y, totalMass, std::chrono::steady_clock::now()});
}
continue;
}
if (other->massive) {
float distance = std::max(distanceSquared, 0.1);
if (distance <= 1.0) {
float force = (G * particle->mass * other->mass) / (distance);
fx += force * (-dx / distance);
fy += force * (-dy / distance);
}
}
}
float dtByMass = dt / particle->mass;
float ax = fx * dtByMass;
float ay = fy * dtByMass;
#pragma omp atomic
particle->vx += ax;
#pragma omp atomic
particle->vy += ay;
#pragma omp atomic
particle->x += particle->vx * dt;
#pragma omp atomic
particle->y += particle->vy * dt;
particle->orbit.push_back({particle->x, particle->y}); // expensive operation
while (particle->orbit.size() > maxOrbitSize) {
particle->orbit.erase(particle->orbit.begin());
}
if ((particle->x * particle->x + particle->y * particle->y) > squaredLimit) {
particle->alive = false;
}
#pragma omp critical
{
collisionPoints.insert(collisionPoints.end(), localCollisionPoints.begin(), localCollisionPoints.end());
}
}
}
void drawCollisionDots() {
auto currentTime = std::chrono::steady_clock::now();
for (auto it = collisionPoints.begin(); it != collisionPoints.end();) {
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - it->timestamp).count();
if (elapsedTime >= 500) {
it = collisionPoints.erase(it);
} else {
float alpha = std::exp(-elapsedTime / 200.0f);
glPointSize(std::log(it->mass) * alpha);
glBegin(GL_POINTS);
glColor4f(1.0f, 1.0f, 1.0f, alpha);
glVertex2f(it->x, it->y);
++it;
glEnd();
}
}
}
void drawOrbit(const Body& particle, float trailAlpha) {
glBegin(GL_LINE_STRIP);
for (size_t i = 0; i < particle.orbit.size(); ++i) {
float alpha = particle.currentAlpha - std::pow(trailAlpha, static_cast<float>(i) / particle.orbit.size());
//std::cout << alpha << std::endl;
glColor4f(particle.color.r, particle.color.g, particle.color.b, alpha);
glVertex2d(particle.orbit[i].first, particle.orbit[i].second);
}
glEnd();
if (particle.alive) {
float trailLength = std::sqrt(particle.vx * particle.vx + particle.vy * particle.vy);
glBegin(GL_LINE_STRIP);
size_t startIndex = particle.orbit.size() > trailLength ? particle.orbit.size() - trailLength : 0;
for (size_t i = startIndex; i < particle.orbit.size(); ++i) {
glColor4f(particle.color.r, particle.color.g, particle.color.b, 1.0);
glVertex2d(particle.orbit[i].first, particle.orbit[i].second);
}
glEnd();
} else {
particle.currentAlpha = std::max(particle.currentAlpha - 0.01, 0.0);
}
}
void computePosVel(
float& x, float& y, float& vx, float& vy,
float minVel, float maxVel, float minRadius, float maxRadius,
std::mt19937& gen) {
std::uniform_real_distribution<float> vel_dist(minVel, maxVel);
std::uniform_real_distribution<float> radius_dist(minRadius, maxRadius);
std::uniform_real_distribution<float> angle_dist(0.0, 2.0 * pi);
float radius = radius_dist(gen);
float angle = angle_dist(gen);
x = radius * cos(angle);
y = radius * sin(angle);
float tangent_angle = atan2(y, x) + pi / 2;
std::uniform_real_distribution<float> velocity_angle_dist(tangent_angle - pi / 5, tangent_angle + pi / 5);
float velocity_angle = velocity_angle_dist(gen);
float vel_amount = vel_dist(gen);
std::uniform_int_distribution<int> sign_gen(0, 1);
int sign = sign_gen(gen) * 2 - 1;
vx = sign * cos(velocity_angle) * vel_amount;
vy = sign * sin(velocity_angle) * vel_amount;
}
int run(GLFWwindow* window) {
std::ifstream inputFile("settings.txt");
if (!inputFile.is_open()) {
std::cerr << "Error opening settings file." << std::endl;
return 1;
}
double dt;
int singleStart, numBodiesGenerator, collidingBodies, numMainBodies;
double singleStartDelta;
float trailAlpha;
float minMass, maxMass, minHueSize, maxHueSize, minVel, maxVel, minRadius, maxRadius;
inputFile >> dt >> singleStart >> numBodiesGenerator >> collidingBodies >> numMainBodies
>> singleStartDelta >> trailAlpha >> minMass >> maxMass >> minHueSize >> maxHueSize
>> minVel >> maxVel >> minRadius >> maxRadius;
inputFile.close();
std::cout << "dt: " << dt << std::endl;
std::cout << "singleStart: " << singleStart << std::endl;
std::cout << "numBodiesGenerator: " << numBodiesGenerator << std::endl;
std::cout << "collidingBodies: " << collidingBodies << std::endl;
std::cout << "numMainBodies: " << numMainBodies << std::endl;
std::cout << "singleStartDelta: " << singleStartDelta << std::endl;
std::cout << "trailAlpha: " << trailAlpha << std::endl;
std::cout << "minMass: " << minMass << std::endl;
std::cout << "maxMass: " << maxMass << std::endl;
std::cout << "minHueSize: " << minHueSize << std::endl;
std::cout << "maxHueSize: " << maxHueSize << std::endl;
std::cout << "minVel: " << minVel << std::endl;
std::cout << "maxVel: " << maxVel << std::endl;
std::cout << "minRadius: " << minRadius << std::endl;
std::cout << "maxRadius: " << maxRadius << std::endl;
std::cout << "Press SPACE to restart." << std::endl;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> mass_dist(minMass, maxMass);
std::uniform_real_distribution<float> hue_size_dist(minHueSize, maxHueSize);
std::uniform_real_distribution<float> color_dist(0.3, 1.0);
float x_s, y_s, vx_s, vy_s;
computePosVel(x_s, y_s, vx_s, vy_s, minVel, maxVel, minRadius, maxRadius, gen);
std::uniform_real_distribution<float> x_dist(x_s, x_s + singleStartDelta);
std::uniform_real_distribution<float> y_dist(y_s, y_s + singleStartDelta);
float colorStart = color_dist(gen);
float colorEnd = colorStart + hue_size_dist(gen);
std::uniform_real_distribution<float> hue_range_dist(colorStart, colorEnd);
std::vector<Body> particles;
// std::vector<Body*> massiveParticles;
std::vector<Body*> mobileParticles;
collisionPoints.clear();
for (int i = 0; i < numBodiesGenerator; ++i) {
float x, y, vx, vy;
computePosVel(x, y, vx, vy, minVel, maxVel, minRadius, maxRadius, gen);
double mass = mass_dist(gen);
bool mobile = true;
bool massive = true;
bool collision = false;
if (collidingBodies == 1) {
collision = true;
}
bool alive = true;
float hue = hue_range_dist(gen);
float lightness = color_dist(gen);
float saturation = color_dist(gen);
if (singleStart == 1) {
x = x_dist(gen);
y = y_dist(gen);
vx = vx_s/2;
vy = vy_s/2;
mass = 1e2;
massive = false;
float hue = static_cast<float>((x - x_s) / singleStartDelta);
hue = fmod(hue + 1.0f, 1.0f);
hue = colorStart + hue * (colorEnd - colorStart);
}
float r, g, b;
hsvToRgb(hue, saturation, lightness, r, g, b);
Color color = Color(r,g,b);
particles.emplace_back(x, y, vx, vy, mass, mobile, massive, collision, alive, color);
}
if (numMainBodies == 1) {
particles.emplace_back(0.0, 0.0, 0.0, 0.0, 1e7, false, true, false, true, Color(1.0, 1.0, 1.0));
} else if (numMainBodies >= 2) {
particles.emplace_back(-0.1, 0.0, 0.0, 0.0, 1e6, false, true, false, true, Color(1.0, 1.0, 1.0));
particles.emplace_back(0.1, 0.0, 0.0, 0.0, 1e6, false, true, false, true, Color(1.0, 1.0, 1.0));
}
if (numMainBodies == 3) {
particles.emplace_back(0.0, 0.15, 0.0, 0.0, 1e6, false, true, false, true, Color(1.0, 1.0, 1.0));
}
// for (auto& particle : particles) {
// if (particle.massive) {
// massiveParticles.push_back(&particle);
// }
// }
for (auto& particle : particles) {
if (particle.mobile) {
mobileParticles.push_back(&particle);
}
}
auto start_time = std::chrono::steady_clock::now();
double lastTime = glfwGetTime();
int frameCount = 0;
while (!glfwWindowShouldClose(window)) {
if (isSpacePressed()) {
return 0;
}
auto current_time = std::chrono::steady_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::minutes>(current_time - start_time).count();
if (elapsed_time >= 3) {
return 0;
}
glClear(GL_COLOR_BUFFER_BIT);
drawCollisionDots();
frameCount++;
for (const auto& particle : particles) {
drawOrbit(particle, trailAlpha);
if (particle.alive) {
glPointSize(std::log(particle.mass)/3);
glBegin(GL_POINTS);
glColor3f(particle.color.r, particle.color.g, particle.color.b);
glVertex2d(particle.x, particle.y);
glEnd();
}
}
updateBodies(particles, mobileParticles, dt);
glfwSwapBuffers(window);
glfwPollEvents();
double currentTime = glfwGetTime();
if (currentTime - lastTime >= 1.0) {
double fps = frameCount / (currentTime - lastTime);
std::cout << "Frame Rate: " << fps << " fps" << std::endl;
frameCount = 0;
lastTime = currentTime;
}
}
glfwTerminate();
std::cout << "Closing" << std::endl;
return 1;
}
int main() {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
GLFWwindow* window;
window = glfwCreateWindow(500, 500, "N-Body Simulation", NULL, NULL);
if (!window) {
glfwTerminate();
std::cerr << "Failed to create GLFW window" << std::endl;
return -1;
}
glfwMakeContextCurrent(window);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (true) {
int result = run(window);
if (result != 0) {
return 0;
}
}
std::cout << "Closing" << std::endl;
return 0;
}