Skip to content

Commit 2d05219

Browse files
Add files via upload
1 parent d0078b0 commit 2d05219

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

videos/three-body-stars.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "conf.h"
2+
#include "raylib.h"
3+
#include <vector>
4+
#include <string>
5+
6+
int main() {
7+
InitWindow(800, 600, "SemiEuler");
8+
std::vector<Particle> particles;
9+
particles.emplace_back(-5e10, 0, 0, 15000, 2e30, 7e8, "StarA");
10+
particles.emplace_back(5e10, 0, 0, -15000, 2e30, 7e8, "StarB");
11+
particles.emplace_back(0, 5e10, -7500, 0, 2e30, 7e7, "StarC");
12+
13+
14+
SetTargetFPS(0); // Unlock FPS
15+
16+
double dt = 0.4;
17+
double camX = 0.0;
18+
double camY = 0.0;
19+
double simTime = 0.0;
20+
double scale = 1.5e6;
21+
int steps = 10000;
22+
double camSpeed = 5000000;
23+
bool paused = false;
24+
25+
while (!WindowShouldClose()) {
26+
if (IsKeyDown(KEY_W)) camY -= camSpeed;
27+
if (IsKeyDown(KEY_S)) camY += camSpeed;
28+
if (IsKeyDown(KEY_A)) camX -= camSpeed;
29+
if (IsKeyDown(KEY_D)) camX += camSpeed;
30+
if (IsKeyDown(KEY_UP)) scale *= 1.005;
31+
32+
if (IsKeyDown(KEY_DOWN)) scale /= 1.005;
33+
if (IsKeyPressed(KEY_SPACE)) {
34+
paused = !paused;
35+
}
36+
37+
// If its unpaused, run the loop, otherwise, dont.
38+
if(!paused) {
39+
for (int s = 0; s < steps; s++) {
40+
for (size_t i = 0; i < particles.size(); i++) {
41+
for (size_t j = i + 1; j < particles.size(); j++) {
42+
Gravity(particles[i], particles[j], dt);
43+
}
44+
}
45+
46+
for (auto& p : particles) {
47+
p.x += p.vx * dt;
48+
p.y += p.vy * dt;
49+
}
50+
51+
simTime += dt;
52+
}
53+
}
54+
55+
// Draw particles
56+
BeginDrawing();
57+
ClearBackground(BLACK);
58+
for (auto& p : particles) {
59+
60+
int radius = (int)(p.r / scale);
61+
if (radius < 1) radius = 1;
62+
63+
Color color = WHITE;
64+
if (p.name == "StarA") color = BLUE; // if particle name "Earth" set color blue
65+
else if (p.name == "StarB") color = YELLOW; // if particle name "Sun" set color yellow
66+
else if (p.name == "StarC") color = PURPLE; // if particle name "Moon" set color gray
67+
68+
DrawCircle(
69+
(int)((p.x - camX) / scale) + 400,
70+
(int)((p.y - camY) / scale) + 300,
71+
radius,
72+
color
73+
);
74+
}
75+
76+
if (paused) {
77+
DrawText("PAUSED", 10, 10, 20, WHITE);
78+
}
79+
80+
EndDrawing();
81+
}
82+
}

videos/three-body-stars.mp4

81.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)