-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cpp
More file actions
628 lines (582 loc) · 24.1 KB
/
Copy pathsimulation.cpp
File metadata and controls
628 lines (582 loc) · 24.1 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include "simulation.h"
#include <algorithm>
#include <cmath>
#include <numeric>
#include <unordered_map>
using namespace std;
static constexpr float kPi = 3.14159f;
Vector2 Add(Vector2 a, Vector2 b) {
return {a.x + b.x, a.y + b.y};
}
Vector2 Sub(Vector2 a, Vector2 b) {
return {a.x - b.x, a.y - b.y};
}
Vector2 Mul(Vector2 v, float s) {
return {v.x * s, v.y * s};
}
float Length(Vector2 v) {
return sqrt(v.x * v.x + v.y * v.y);
}
float Distance(Vector2 a, Vector2 b) {
return Length(Sub(a, b));
}
Vector2 Normalize(Vector2 v) {
float len = Length(v);
if (len <= 0.0001f) return {0, 0};
return {v.x / len, v.y / len};
}
float RadiusFromMass(float mass, BodyType type) {
if (type == BLACK_HOLE) return sqrt(mass) * 0.65f;
if (type == NEUTRON_STAR) return 8.0f;
if (type == STAR) return sqrt(mass) * 1.5f;
if (type == ASTEROID) return sqrt(mass) * 1.3f;
return sqrt(mass) * 1.8f;
}
float CollisionRadiusFromMass(float mass, BodyType type) {
if (type == BLACK_HOLE) return sqrt(mass) * 0.25f;
if (type == NEUTRON_STAR) return 8.0f;
return RadiusFromMass(mass, type);
}
static void UpdateBodyRadii(Body& body) {
body.visualRadius = RadiusFromMass(body.mass, body.type);
body.collisionRadius = CollisionRadiusFromMass(body.mass, body.type);
body.radius = body.visualRadius;
}
static bool IsFixedAnchor(const Body& body, const SimulationSettings& settings) {
if (body.type == WHITE_HOLE) return true;
return settings.physicsMode == PHYSICS_SANDBOX && body.type == BLACK_HOLE;
}
void AddBody(
vector<Body>& bodies,
Vector2 position,
Vector2 velocity,
float mass,
Color color,
BodyType type
)
{
Body body;
body.position = position;
body.velocity = velocity;
body.mass = mass;
body.color = color;
body.type = type;
body.rotation = (float)GetRandomValue(0, 360);
UpdateBodyRadii(body);
bodies.push_back(body);
}
static Color RandomBrightColor() {
int choice = GetRandomValue(0, 5);
if (choice == 0) return SKYBLUE;
else if (choice == 1) return ORANGE;
else if (choice == 2) return GREEN;
else if (choice == 3) return PINK;
else if (choice == 4) return GOLD;
return VIOLET;
}
static vector<Vector2> ComputeAccelerations(
const vector<Body>& bodies,
float gravityStrength,
float eps2
);
void SpawnAsteroidBurst(vector<Body>& bodies, Vector2 center) {
for (int i = 0; i < 25; i++) {
float angle = GetRandomValue(0, 360) * kPi / 180.0f;
float distance = GetRandomValue(10, 80);
float speed = GetRandomValue(40, 180);
Vector2 position = {
center.x + cos(angle) * distance,
center.y + sin(angle) * distance
};
Vector2 velocity = {
cos(angle) * speed,
sin(angle) * speed
};
float mass = GetRandomValue(3, 12);
AddBody(bodies, position, velocity, mass, RandomBrightColor(), ASTEROID);
}
}
void SpawnBinaryStars(vector<Body>& bodies, Vector2 center) {
AddBody(bodies, {center.x - 90.0f, center.y}, {0, -36.0f}, 500.0f, GOLD, STAR);
AddBody(bodies, {center.x + 90.0f, center.y}, {0, +36.0f}, 500.0f, PURPLE, STAR);
}
void SpawnBlackHole(vector<Body>& bodies, Vector2 position) {
Body bh;
bh.position = position;
bh.velocity = {0, 0};
bh.mass = 1200.0f;
bh.color = {5, 0, 10, 255};
bh.type = BLACK_HOLE;
bh.rotation = 0.0f;
UpdateBodyRadii(bh);
bodies.push_back(bh);
}
void SpawnWhiteHole(vector<Body>& bodies, Vector2 position) {
Body wh;
wh.position = position;
wh.velocity = {0, 0};
wh.mass = 600.0f;
wh.color = {220, 250, 255, 255};
wh.type = WHITE_HOLE;
wh.rotation = 0.0f;
UpdateBodyRadii(wh);
bodies.push_back(wh);
}
void SpawnNeutronStar(vector<Body>& bodies, Vector2 position) {
Body ns;
ns.position = position;
ns.velocity = {0, 0};
ns.mass = 400.0f;
ns.color = {180, 220, 255, 255};
ns.type = NEUTRON_STAR;
ns.rotation = 0.0f;
UpdateBodyRadii(ns);
bodies.push_back(ns);
}
void ResetSimulation(vector<Body>& bodies) {
bodies.clear();
int screenWidth = GetScreenWidth();
int screenHeight = GetScreenHeight();
Vector2 center = {
screenWidth / 2.0f,
screenHeight / 2.0f
};
AddBody(bodies, center, {0, 0}, 900.0f, YELLOW, STAR);
AddBody(bodies, {center.x + 230.0f, center.y}, {0, -145.0f}, 25.0f, BLUE, PLANET);
AddBody(bodies, {center.x - 330.0f, center.y}, {0, 115.0f}, 45.0f, GREEN, PLANET);
AddBody(bodies, {center.x, center.y + 170.0f}, {180.0f, 0}, 12.0f, ORANGE, ASTEROID);
}
static void MergeBodies(vector<Body>& bodies, int a, int b) {
Body& first = bodies[a];
Body& second = bodies[b];
float totalMass = first.mass + second.mass;
Vector2 newPosition = {
(first.position.x * first.mass + second.position.x * second.mass) / totalMass,
(first.position.y * first.mass + second.position.y * second.mass) / totalMass
};
Vector2 newVelocity = {
(first.velocity.x * first.mass + second.velocity.x * second.mass) / totalMass,
(first.velocity.y * first.mass + second.velocity.y * second.mass) / totalMass
};
Color newColor = first.mass >= second.mass ? first.color : second.color;
first.position = newPosition;
first.velocity = newVelocity;
first.mass = totalMass;
first.color = newColor;
if (first.type == BLACK_HOLE || second.type == BLACK_HOLE)
first.type = BLACK_HOLE;
else if (first.type == NEUTRON_STAR || second.type == NEUTRON_STAR)
first.type = NEUTRON_STAR;
else if (first.type == WHITE_HOLE || second.type == WHITE_HOLE)
first.type = WHITE_HOLE;
else
first.type = totalMass >= 200.0f ? STAR : (totalMass >= 20.0f ? PLANET : ASTEROID);
UpdateBodyRadii(first);
first.trail.clear();
bodies.erase(bodies.begin() + b);
}
static void FragmentBodies(vector<Body>& bodies, vector<Particle>& particles, int a, int b) {
Body first = bodies[a];
Body second = bodies[b];
float totalMass = first.mass + second.mass;
Vector2 center = {
(first.position.x * first.mass + second.position.x * second.mass) / totalMass,
(first.position.y * first.mass + second.position.y * second.mass) / totalMass
};
Vector2 centerVelocity = {
(first.velocity.x * first.mass + second.velocity.x * second.mass) / totalMass,
(first.velocity.y * first.mass + second.velocity.y * second.mass) / totalMass
};
float impactSpeed = Length(Sub(second.velocity, first.velocity));
int physicalCount = bodies.size() < 900 ? PhysicalFragmentCount(totalMass) : 0;
int debrisCount = VisualDebrisCount(totalMass);
float fragmentMass = physicalCount > 0 ? max(2.0f, totalMass * 0.35f / physicalCount) : 0.0f;
float spawnRadius = max(first.collisionRadius + second.collisionRadius + 10.0f, 24.0f);
for (int k = 0; k < debrisCount; k++) {
float angle = k * (2.0f * kPi / debrisCount) + (float)GetRandomValue(-20, 20) * kPi / 180.0f;
Vector2 dir = {cosf(angle), sinf(angle)};
float speed = impactSpeed * 0.35f + (float)GetRandomValue(20, 120);
Color color = k % 2 == 0 ? first.color : second.color;
Vector2 position = Add(center, Mul(dir, (float)GetRandomValue(4, 18)));
particles.push_back({position, Mul(dir, speed * 0.45f), color, 1.0f, (float)GetRandomValue(3, 7)});
}
for (int k = 0; k < physicalCount; k++) {
float angle = k * (2.0f * kPi / physicalCount) + (float)GetRandomValue(-12, 12) * kPi / 180.0f;
Vector2 dir = {cosf(angle), sinf(angle)};
float speed = impactSpeed * 0.25f + (float)GetRandomValue(35, 95);
Color color = k % 2 == 0 ? first.color : second.color;
Vector2 position = Add(center, Mul(dir, spawnRadius + (float)GetRandomValue(0, 18)));
Vector2 velocity = Add(centerVelocity, Mul(dir, speed));
AddBody(bodies, position, velocity, fragmentMass, color, ASTEROID);
}
int hi = max(a, b);
int lo = min(a, b);
bodies.erase(bodies.begin() + hi);
bodies.erase(bodies.begin() + lo);
}
struct QuadNode {
float x0, y0, x1, y1;
float totalMass, cx, cy;
int bodyIdx;
int ch[4];
};
static vector<QuadNode> qtPool;
static int qtBuild(const vector<Body>& bodies, vector<int>& idx, float x0, float y0, float x1, float y1) {
if (idx.empty()) return -1;
int ni = (int)qtPool.size();
qtPool.push_back({x0, y0, x1, y1, 0.0f, 0.0f, 0.0f, -1, {-1, -1, -1, -1}});
float tm = 0.0f, cx = 0.0f, cy = 0.0f;
for (int i : idx) {
tm += bodies[i].mass;
cx += bodies[i].position.x * bodies[i].mass;
cy += bodies[i].position.y * bodies[i].mass;
}
qtPool[ni].totalMass = tm;
qtPool[ni].cx = cx / tm;
qtPool[ni].cy = cy / tm;
if (idx.size() == 1 || (x1 - x0) < 0.5f) {
qtPool[ni].bodyIdx = idx[0];
return ni;
}
float mx = (x0 + x1) * 0.5f, my = (y0 + y1) * 0.5f;
vector<int> q[4];
for (int i : idx) {
int qi = (bodies[i].position.x >= mx ? 1 : 0) + (bodies[i].position.y >= my ? 2 : 0);
q[qi].push_back(i);
}
int c0 = qtBuild(bodies, q[0], x0, y0, mx, my);
int c1 = qtBuild(bodies, q[1], mx, y0, x1, my);
int c2 = qtBuild(bodies, q[2], x0, my, mx, y1);
int c3 = qtBuild(bodies, q[3], mx, my, x1, y1);
qtPool[ni].ch[0] = c0;
qtPool[ni].ch[1] = c1;
qtPool[ni].ch[2] = c2;
qtPool[ni].ch[3] = c3;
return ni;
}
static void qtForce(const vector<Body>& bodies, int bi, int ni, Vector2& accel, float G, float eps2, float theta) {
if (ni < 0) return;
const QuadNode& n = qtPool[ni];
if (n.bodyIdx == bi) return;
float dx = n.cx - bodies[bi].position.x;
float dy = n.cy - bodies[bi].position.y;
float dist2 = dx * dx + dy * dy;
float dist = sqrtf(dist2);
if (dist < 0.001f) return;
if (ShouldApproximateBarnesHutNode(
n.x0,
n.y0,
n.x1,
n.y1,
bodies[bi].position,
n.bodyIdx >= 0,
dist,
theta
)) {
float f = G * n.totalMass / (dist2 + eps2);
accel.x += f * dx / dist;
accel.y += f * dy / dist;
} else {
for (int c : n.ch) qtForce(bodies, bi, c, accel, G, eps2, theta);
}
}
static vector<Vector2> ComputeAccelerations(
const vector<Body>& bodies,
const SimulationSettings& settings,
float eps2
)
{
vector<Vector2> accelerations(bodies.size(), {0, 0});
if (bodies.empty()) return accelerations;
float bx0 = 1e9f, by0 = 1e9f, bx1 = -1e9f, by1 = -1e9f;
for (const Body& b : bodies) {
bx0 = min(bx0, b.position.x);
by0 = min(by0, b.position.y);
bx1 = max(bx1, b.position.x);
by1 = max(by1, b.position.y);
}
float pad = max(1.0f, (bx1 - bx0 + by1 - by0) * 0.01f);
bx0 -= pad;
by0 -= pad;
bx1 += pad;
by1 += pad;
vector<int> allIdx((int)bodies.size());
iota(allIdx.begin(), allIdx.end(), 0);
qtPool.clear();
qtPool.reserve(bodies.size() * 4);
int root = qtBuild(bodies, allIdx, bx0, by0, bx1, by1);
const float theta = 0.5f;
for (int i = 0; i < (int)bodies.size(); i++)
qtForce(bodies, i, root, accelerations[i], settings.gravityStrength, eps2, theta);
if (settings.physicsMode == PHYSICS_REALISM) return accelerations;
for (int wh = 0; wh < (int)bodies.size(); wh++) {
if (bodies[wh].type != WHITE_HOLE) continue;
for (int i = 0; i < (int)bodies.size(); i++) {
if (i == wh) continue;
float dx = bodies[wh].position.x - bodies[i].position.x;
float dy = bodies[wh].position.y - bodies[i].position.y;
float dist2 = dx * dx + dy * dy;
float dist = sqrtf(dist2);
if (dist < 0.001f) continue;
float f = settings.gravityStrength * bodies[wh].mass / (dist2 + eps2);
accelerations[i].x -= 2.0f * f * dx / dist;
accelerations[i].y -= 2.0f * f * dy / dist;
}
}
return accelerations;
}
static void TriggerSupernova(vector<Body>& bodies, vector<Shockwave>& shockwaves, int idx) {
Vector2 pos = bodies[idx].position;
float mass = bodies[idx].mass;
Color col = bodies[idx].color;
float shockR = 500.0f;
for (int i = 0; i < (int)bodies.size(); i++) {
if (i == idx) continue;
float d = Distance(bodies[i].position, pos);
if (d < shockR && d > 1.0f) {
Vector2 dir = Normalize(Sub(bodies[i].position, pos));
bodies[i].velocity = Add(bodies[i].velocity, Mul(dir, 600.0f * (1.0f - d / shockR)));
}
}
const Color hotColors[] = {WHITE, YELLOW, ORANGE, {255, 180, 30, 255}, {255, 220, 80, 255}};
SupernovaRemnant remnant = ClassifySupernovaRemnant(mass);
float remnantMass = 0.0f;
if (remnant == SUPERNOVA_BLACK_HOLE_REMNANT) remnantMass = BlackHoleMassFrom(mass);
else if (remnant == SUPERNOVA_NEUTRON_STAR_REMNANT) remnantMass = NeutronStarMassFrom(mass);
float ejectaMass = max(0.0f, mass - remnantMass);
int spawnStart = (int)bodies.size();
float debrisMass = max(3.0f, ejectaMass / 22.0f);
for (int i = 0; i < 22; i++) {
float angle = i * (2.0f * kPi / 22.0f);
float speed = (float)GetRandomValue(150, 420);
Color dc = hotColors[GetRandomValue(0, 4)];
AddBody(bodies, pos, {cosf(angle) * speed, sinf(angle) * speed}, debrisMass, dc, ASTEROID);
}
for (int k = spawnStart; k < (int)bodies.size(); k++) {
bodies[k].position.x += bodies[k].velocity.x * 0.1f;
bodies[k].position.y += bodies[k].velocity.y * 0.1f;
}
shockwaves.push_back({pos, 0.0f, 90.0f, WHITE, 1.0f, 4.0f, true});
shockwaves.push_back({pos, 0.0f, 500.0f, col, 1.0f, 0.65f, false});
shockwaves.push_back({pos, 0.0f, 820.0f, {255, 100, 20, 255}, 1.0f, 0.32f, false});
if (remnant == SUPERNOVA_BLACK_HOLE_REMNANT) {
bodies[idx].type = BLACK_HOLE;
bodies[idx].mass = remnantMass;
bodies[idx].velocity = {0, 0};
bodies[idx].color = {5, 0, 10, 225};
UpdateBodyRadii(bodies[idx]);
} else if (remnant == SUPERNOVA_NEUTRON_STAR_REMNANT) {
bodies[idx].type = NEUTRON_STAR;
bodies[idx].mass = remnantMass;
bodies[idx].velocity = {0, 0};
bodies[idx].color = {180, 220, 255, 255};
UpdateBodyRadii(bodies[idx]);
} else {
bodies.erase(bodies.begin() + idx);
}
}
static void TidalDisrupt(
vector<Body>& bodies,
vector<Shockwave>& shockwaves,
vector<Particle>& particles,
int disruptorIdx,
int victimIdx
)
{
Vector2 pos = bodies[victimIdx].position;
Vector2 victimVel = bodies[victimIdx].velocity;
float vmass = bodies[victimIdx].mass;
float dx = pos.x - bodies[disruptorIdx].position.x;
float dy = pos.y - bodies[disruptorIdx].position.y;
float debrisAngle = atan2f(dy, dx);
int numDebris = 12;
for (int i = 0; i < numDebris; i++) {
float angle = debrisAngle + i * (2.0f * kPi / numDebris) + ((float)GetRandomValue(-15, 15) * kPi / 180.0f);
float speed = (float)GetRandomValue(60, 220);
Color dc = {
(unsigned char)GetRandomValue(220, 255),
(unsigned char)GetRandomValue(130, 200),
(unsigned char)GetRandomValue(30, 90),
230
};
particles.push_back({
pos,
{victimVel.x + cosf(angle) * speed, victimVel.y + sinf(angle) * speed},
dc,
1.0f,
(float)GetRandomValue(4, 8)
});
}
bodies[disruptorIdx].mass += vmass * 0.3f;
shockwaves.push_back({pos, 0.0f, 180.0f, {255, 160, 60, 200}, 1.0f, 1.5f, false});
bodies.erase(bodies.begin() + victimIdx);
}
void StepSimulation(
vector<Body>& bodies,
vector<Shockwave>& shockwaves,
vector<Particle>& particles,
float dt,
const SimulationSettings& settings
)
{
float maxR = 0.0f;
float maxSpeed = 0.0f;
for (const Body& b : bodies) {
maxR = max(maxR, b.collisionRadius);
maxSpeed = max(maxSpeed, Length(b.velocity));
}
int substeps = settings.physicsMode == PHYSICS_REALISM ? 8 : 4;
if (settings.physicsMode == PHYSICS_REALISM && maxSpeed > 700.0f) substeps = 12;
if (settings.physicsMode == PHYSICS_REALISM && maxSpeed > 1200.0f) substeps = 16;
float subDt = dt / substeps;
const float eps2 = settings.softening * settings.softening;
for (int s = 0; s < substeps; s++) {
auto accel0 = ComputeAccelerations(bodies, settings, eps2);
for (int i = 0; i < (int)bodies.size(); i++) {
if (IsFixedAnchor(bodies[i], settings)) continue;
bodies[i].position = Add(
bodies[i].position,
Add(
Mul(bodies[i].velocity, subDt),
Mul(accel0[i], 0.5f * subDt * subDt)
)
);
}
auto accel1 = ComputeAccelerations(bodies, settings, eps2);
for (int i = 0; i < (int)bodies.size(); i++) {
if (IsFixedAnchor(bodies[i], settings)) {
bodies[i].velocity = {0, 0};
continue;
}
bodies[i].velocity = Add(
bodies[i].velocity,
Mul(Add(accel0[i], accel1[i]), 0.5f * subDt)
);
}
if (settings.collisionsEnabled) {
for (int i = 0; i < (int)bodies.size(); i++) {
for (int j = i + 1; j < (int)bodies.size(); j++) {
float d = Distance(bodies[i].position, bodies[j].position);
if (d < bodies[i].collisionRadius + bodies[j].collisionRadius) {
if (bodies[i].type == BLACK_HOLE || bodies[j].type == BLACK_HOLE) continue;
bool eitherWH = bodies[i].type == WHITE_HOLE || bodies[j].type == WHITE_HOLE;
if (settings.physicsMode == PHYSICS_SANDBOX && eitherWH && d > 0.1f) {
Vector2 normal = Normalize(Sub(bodies[j].position, bodies[i].position));
bodies[i].velocity = Sub(bodies[i].velocity, Mul(normal, 300.0f));
bodies[j].velocity = Add(bodies[j].velocity, Mul(normal, 300.0f));
} else if (
settings.physicsMode == PHYSICS_REALISM &&
CanFragmentOnImpact(bodies[i].type, bodies[j].type) &&
Length(Sub(bodies[j].velocity, bodies[i].velocity)) >= 120.0f
) {
FragmentBodies(bodies, particles, i, j);
i = (int)bodies.size();
break;
} else {
MergeBodies(bodies, i, j);
j--;
}
}
}
}
} else {
unordered_map<int, vector<int>> grid;
float cellSize = max(100.0f, maxR * 2.0f);
auto hashCell = [](int cx, int cy) { return (cx * 73856093) ^ (cy * 19349663); };
for (int i = 0; i < (int)bodies.size(); i++) {
int cx = (int)floor(bodies[i].position.x / cellSize);
int cy = (int)floor(bodies[i].position.y / cellSize);
grid[hashCell(cx, cy)].push_back(i);
}
for (int i = 0; i < (int)bodies.size(); i++) {
int cx = (int)floor(bodies[i].position.x / cellSize);
int cy = (int)floor(bodies[i].position.y / cellSize);
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
auto it = grid.find(hashCell(cx + dx, cy + dy));
if (it == grid.end()) continue;
for (int j : it->second) {
if (j <= i) continue;
if (bodies[i].type == BLACK_HOLE || bodies[j].type == BLACK_HOLE) continue;
float d = Distance(bodies[i].position, bodies[j].position);
float minDist = bodies[i].collisionRadius + bodies[j].collisionRadius;
if (d < minDist && d > 0.1f) {
Vector2 normal = Normalize(Sub(bodies[j].position, bodies[i].position));
Vector2 relVel = Sub(bodies[j].velocity, bodies[i].velocity);
float velAlongNormal = relVel.x * normal.x + relVel.y * normal.y;
if (velAlongNormal < 0) {
float impulse = (2.0f * velAlongNormal) / (1.0f / bodies[i].mass + 1.0f / bodies[j].mass);
bodies[i].velocity = Add(bodies[i].velocity, Mul(normal, impulse / bodies[i].mass));
bodies[j].velocity = Add(bodies[j].velocity, Mul(normal, -impulse / bodies[j].mass));
float overlap = minDist - d;
float totalMass = bodies[i].mass + bodies[j].mass;
bodies[i].position = Sub(bodies[i].position, Mul(normal, overlap * bodies[j].mass / totalMass));
bodies[j].position = Add(bodies[j].position, Mul(normal, overlap * bodies[i].mass / totalMass));
}
}
}
}
}
}
}
for (int i = (int)bodies.size() - 1; i >= 0; i--) {
if (
bodies[i].type == STAR &&
ClassifySupernovaRemnant(bodies[i].mass) != SUPERNOVA_NO_REMNANT
) {
TriggerSupernova(bodies, shockwaves, i);
break;
}
}
for (int i = (int)bodies.size() - 1; i >= 0; i--) {
if (bodies[i].type != NEUTRON_STAR) continue;
for (int j = (int)bodies.size() - 1; j >= 0; j--) {
if (j == i) continue;
BodyType jt = bodies[j].type;
if (jt == BLACK_HOLE || jt == WHITE_HOLE || jt == NEUTRON_STAR) continue;
float dx = bodies[j].position.x - bodies[i].position.x;
float dy = bodies[j].position.y - bodies[i].position.y;
float dist = sqrtf(dx * dx + dy * dy);
float roche = 3.5f * bodies[j].collisionRadius * cbrtf(bodies[i].mass / bodies[j].mass);
if (dist < roche) {
TidalDisrupt(bodies, shockwaves, particles, i, j);
i = (int)bodies.size();
break;
}
}
}
for (int i = 0; i < (int)bodies.size(); i++) {
if (bodies[i].type != BLACK_HOLE) continue;
for (int j = (int)bodies.size() - 1; j >= 0; j--) {
if (j == i || bodies[j].type == BLACK_HOLE) continue;
if (Distance(bodies[i].position, bodies[j].position) < bodies[i].collisionRadius + bodies[j].collisionRadius) {
bodies[i].mass += bodies[j].mass;
UpdateBodyRadii(bodies[i]);
bodies[i].trail.clear();
bodies.erase(bodies.begin() + j);
if (j < i) i--;
}
}
}
}
for (int i = 0; i < (int)bodies.size(); i++) {
bodies[i].trail.push_back(bodies[i].position);
if ((int)bodies[i].trail.size() > settings.maxTrailLength) bodies[i].trail.pop_front();
if (bodies[i].type == BLACK_HOLE) bodies[i].rotation += dt * 60.0f;
if (bodies[i].type == STAR) bodies[i].rotation += dt * 15.0f;
if (bodies[i].type == NEUTRON_STAR) bodies[i].rotation += dt * 200.0f;
}
}
void UpdateEffects(vector<Shockwave>& shockwaves, vector<Particle>& particles, float dt) {
for (int i = (int)shockwaves.size() - 1; i >= 0; i--) {
shockwaves[i].life -= dt * shockwaves[i].decayRate;
shockwaves[i].radius = (1.0f - shockwaves[i].life) * shockwaves[i].maxRadius;
if (shockwaves[i].life <= 0.0f) shockwaves.erase(shockwaves.begin() + i);
}
for (int i = (int)particles.size() - 1; i >= 0; i--) {
particles[i].position.x += particles[i].velocity.x * dt;
particles[i].position.y += particles[i].velocity.y * dt;
particles[i].life -= dt * 0.9f;
if (particles[i].life <= 0.0f) particles.erase(particles.begin() + i);
}
}