-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (189 loc) · 8.9 KB
/
main.cpp
File metadata and controls
228 lines (189 loc) · 8.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
#include <SFML/Graphics.hpp>
#include <vector>
#include <random>
#include <memory>
#include <cmath>
#include "Include/Astre.hpp"
#include "Include/Planete.hpp"
#include "Include/Lune.hpp"
#include "Include/Asteroide.hpp"
#include "Include/Physics.hpp"
#include "Include/Background.hpp"
#include "Include/Constants.hpp"
using namespace sim;
void setupSolarSystem(std::vector<std::unique_ptr<Astre>>& bodies) {
// central sun (type générique Astre)
bodies.push_back(std::make_unique<Astre>(800, 450, 0, 0, 100000, 20, sf::Color::Yellow, "Sun"));
auto UA = [](double d) { return d * UA_SCALE; };
// helper lambda to add a planet and optionally some moons
auto addPlanet = [&](double dist, double mass, double radius, sf::Color color,
const std::vector<std::pair<double,double>>& moons = {}, std::string name) -> Planete* {
double vel = std::sqrt((G * 100000) / dist) * PLANET_SPEED_FACTOR;
double px = 800 + dist;
double py = 450;
double pvx = 0;
double pvy = vel;
auto planet = std::make_unique<Planete>(px, py, pvx, pvy, mass, radius, color, name);
Planete* pPtr = planet.get();
bodies.push_back(std::move(planet));
// add moons around this planet
for (auto [md, mm] : moons) {
double mx = px + md;
double my = py;
// orbital speed around the planet : orthogonal to (md, 0)
double mvel = std::sqrt((G * mass) / md);
// We choose the upward direction so that the moon rotates counter-clockwise
double mvx = pvx;
double mvy = pvy + mvel;
// The angular velocity can be multiplied by a random factor close to 1.
static std::mt19937 genMoon(12345);
std::uniform_real_distribution<double> dist01(0,1);
double speedFactor = MOON_SPEED_FACTOR_MIN +
(MOON_SPEED_FACTOR_MAX - MOON_SPEED_FACTOR_MIN) * dist01(genMoon);
auto moon = std::make_unique<Lune>(mx, my, mvx, mvy, mm, mm*2, sf::Color(200,200,200), pPtr, speedFactor);
pPtr->addMoon(moon.get());
bodies.push_back(std::move(moon));
}
return pPtr;
};
// planets with approximate distances, masses and moon specs (distance,mass)
addPlanet(UA(0.39), 0.5, 3, sf::Color(150,150,150), {},"Mercury"); // Mercury
addPlanet(UA(0.72), 0.9, 4, sf::Color(200,180,50), {},"Venus"); // Venus
addPlanet(UA(1.00), 10, 5, sf::Color::Blue, {{15, 0.1}}, "Earth"); // Earth + Moon
addPlanet(UA(1.52), 3, 4, sf::Color(200,100,100), {{10,0.01},{20,0.01}}, "Mars"); // Mars (Phobos/Deimos)
addPlanet(UA(5.20), 300, 12, sf::Color(200,150,100), {{60,0.5},{70,0.5},{75,0.6},{80,0.7}}, "Jupiter"); // Jupiter
Planete* saturn = addPlanet(UA(9.58), 80, 11, sf::Color(180,180,200), {{60,0.5},{70,0.3},{80,0.2}, {90, 0.4}, {75, 0.1}}, "Saturn"); // Saturn
if (saturn) {
// anneaux : inner and outer in pixels (for ex. 17 and 25 around radius 11)
saturn->setRings(17, 25, sf::Color(200,200,150,150));
}
addPlanet(UA(19.22), 40, 9, sf::Color(150,200,200), {{60,0.5},{70,0.2},{80,0.3}, {75, 0.4}, {65, 0.1}}, "Uranus"); // Uranus
addPlanet(UA(30.05), 40, 9, sf::Color(100,100,200), {{80, 0.5}, {70, 0.3}}, "Neptune"); // Neptune
addPlanet(UA(39.48), 0.002, 3, sf::Color(180,160,150), {}, "Pluto"); // Pluto
// main asteroid belt between Mars and Jupiter
std::mt19937 gen(150);
std::uniform_real_distribution<double> angle(0, 2*M_PI);
std::uniform_real_distribution<double> radiusDist(UA(2.2), UA(3.2));
std::uniform_real_distribution<double> smallPert(-0.02, 0.02);
for (int i = 0; i < ASTEROID_BELT_COUNT; ++i) {
double r = radiusDist(gen);
double a = angle(gen);
double x = 800 + r * cos(a);
double y = 450 + r * sin(a);
double v = std::sqrt((G * 100000) / r) * ASTEROID_SPEED_FACTOR;
double vx = -sin(a) * v;
double vy = cos(a) * v;
bodies.push_back(std::make_unique<Asteroide>(x, y, vx, vy, 0.01, 2, sf::Color(150,150,150,150)));
}
// Kuiper belt beyond Neptune
std::uniform_real_distribution<double> radiusDist2(UA(40.0), UA(50.0));
for (int i = 0; i < KUIPER_BELT_COUNT; ++i) {
double r = radiusDist2(gen);
double a = angle(gen);
double x = 800 + r * cos(a);
double y = 450 + r * sin(a);
double v = std::sqrt((G * 100000) / r) * ASTEROID_SPEED_FACTOR;
double vx = -sin(a) * v;
double vy = cos(a) * v;
bodies.push_back(std::make_unique<Asteroide>(x, y, vx, vy, 0.005, 2, sf::Color(200,200,255,120)));
}
}
int main() {
sf::RenderWindow window(sf::VideoMode(1600, 900), "Galaxy N-Body Simulation");
window.setFramerateLimit(60);
sf::View view(sf::FloatRect(0, 0, 1600, 900));
window.setView(view);
// Encapsulated background
Background background(window.getSize());
sf::Clock clock;
std::vector<std::unique_ptr<Astre>> bodies;
setupSolarSystem(bodies);
// Load font
sf::Font font;
if (!font.loadFromFile("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf")) {
std::cerr << "Impossible de charger la police\n";
}
sf::Text nameText;
nameText.setFont(font);
nameText.setCharacterSize(18);
nameText.setFillColor(sf::Color::White);
while (window.isOpen()) {
// ================= EVENTS =================
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::T)
sim::dt *= 1.1;
if (event.key.code == sf::Keyboard::Y)
sim::dt *= 0.9;
}
}
// ================= CAMERA =================
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Add) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Equal))
view.zoom(0.95f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Subtract) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen))
view.zoom(1.05f);
float panSpeed = 10.0f * view.getSize().x / 1600.0f;
// for different keyboards type
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
view.move(-panSpeed, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::D))
view.move(panSpeed, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
view.move(0, -panSpeed);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::S))
view.move(0, panSpeed);
window.setView(view);
// ================= PHYSICS =================
double subDt = sim::dt / 5.0;
for (int step = 0; step < 5; ++step) {
std::vector<Vector2> oldAccels;
for (auto& b : bodies) {
oldAccels.push_back(b->acceleration);
b->updatePosition(subDt);
b->resetAcceleration();
}
for (size_t i = 0; i < bodies.size(); ++i)
for (size_t j = i + 1; j < bodies.size(); ++j)
computeGravity(*bodies[i], *bodies[j]);
handleCollisions(bodies);
for (size_t i = 0; i < bodies.size(); ++i)
bodies[i]->updateVelocity(subDt, oldAccels[i]);
}
// ================= RENDER =================
window.clear(sf::Color(5, 5, 20)); // first we clear
// draw background
background.update(clock.getElapsedTime().asSeconds(), view.getCenter());
background.draw(window);
// draw simulation
window.setView(view);
for (auto& b : bodies)
b->draw(window);
// ================= HOVER NAME =================
sf::Vector2i mousePixel = sf::Mouse::getPosition(window);
sf::Vector2f mouseWorld = window.mapPixelToCoords(mousePixel);
bool showName = false;
for (auto& b : bodies) {
float dx = mouseWorld.x - b->position.x;
float dy = mouseWorld.y - b->position.y;
float dist = std::sqrt(dx*dx + dy*dy);
float tolerance = std::max(b->radius * 1.5f, 10.0);
if (dist <= tolerance) {
nameText.setString(b->name);
showName = true;
break;
}
}
if (showName) {
window.setView(window.getDefaultView());
nameText.setPosition(10, 10);
window.draw(nameText);
}
window.display();
}
return 0;
}