forked from Sivecano/gravity--the-remake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
315 lines (252 loc) · 6.94 KB
/
main.cpp
File metadata and controls
315 lines (252 loc) · 6.94 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
#include "SDL2/SDL.h"
#include "SDL2/SDL2_gfxPrimitives.h"
#include <list>
#include <iostream>
#include <random>
#include "mat2.h"
#define G 6.6743e-11
#define NUM_OBJS 200
//////////////////////////////////////////////////////////////
// basic math and structs
//////////////////////////////////////////////////////////////
inline const int& max(const int& i, const int& j)
{
if (i > j)
return i;
return j;
}
struct vec
{
double x,y;
vec& operator +=(const vec& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
vec& operator -=(const vec& rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
vec& operator *= (const double& fac)
{
x *= fac;
y *= fac;
return *this;
}
vec& operator /= (const double& fac)
{
x /= fac;
y /= fac;
return *this;
}
friend vec operator+ (vec vec1, const vec& vec2)
{
vec1.x += vec2.x;
vec1.y += vec2.y;
return vec1;
}
friend vec operator- (vec vec1, const vec& vec2)
{
vec1.x -= vec2.x;
vec1.y -= vec2.y;
return vec1;
}
friend vec operator* (vec vec1, double fac)
{
vec1.x *= fac;
vec1.y *= fac;
return vec1;
}
friend vec operator* (double fac, vec vec1)
{
vec1.x *= fac;
vec1.y *= fac;
return vec1;
}
friend vec operator/ (vec vec1, double fac)
{
vec1.x /= fac;
vec1.y /= fac;
return vec1;
}
friend std::ostream& operator<<(std::ostream& os, const vec& vec1)
{
os << "(" << vec1.x << ", " << vec1.y << ")";
return os;
}
friend vec operator* (mat2 mat, vec vec1)
{
vec res;
res.x = vec1.x * mat.a + vec1.y * mat.b;
res.y = vec1.x * mat.c + vec1.y * mat.d;
return res;
}
};
struct object
{
double mass, radius;
vec pos, v;
SDL_Color color;
};
inline double len(vec vector)
{
return sqrtl(vector.x * vector.x + vector.y * vector.y);
}
inline double squarelen(vec vector)
{
return vector.x * vector.x + vector.y * vector.y;
}
inline double dotprod(vec v1, vec v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
enum tracetype
{
none,
circletrace,
linetrace
};
/////////////////////////////////////////////////
// physics code
/////////////////////////////////////////////////
void update_velocities(object* obj1, object* obj2, double dt = 0.01)
{
// gravity code
// Acceleration = Force over mass = G * prod(masses) / mass ->
// Use force without masses
vec direction = obj1->pos - obj2->pos;
double dist = len(direction);
vec force = -G / (dist * dist * dist) * direction;
obj1->v += dt * force * obj2->mass;
obj2->v -= dt * force * obj1->mass;
}
void update_pos(object* obj, double dt)
{
obj->pos += obj->v * dt;
}
bool objects_collide(object* obj1, object* obj2, double epsilon = 0.0)
{
double dist = len(obj1->pos - obj2->pos);
if (dist <= obj1->radius + obj2->radius + epsilon)
return true;
return false;
}
void handle_collision(object* obj1, object* obj2, double dt)
{
// physics basics
vec direction = obj1->pos - obj2->pos;
double d = len(direction);
direction /= d; // Unit vector in direction of distance
double cold = obj1->radius + obj2->radius + 0.01;
// collision code
if (d < cold)
{
vec dv = obj1->v - obj2->v;
double t = (d - cold) / dotprod(dv, direction); // time since collision
// go back to moment of collision
obj1->pos -= obj1->v * t;
obj2->pos -= obj2->v * t;
// Velocity parallel to distance vector, scaled by masses
vec vpar = 2.0 * direction * dotprod(dv, direction) / (obj1->mass + obj2->mass);
obj1->v -= vpar * obj2->mass;
obj2->v += vpar * obj1->mass;
// we progress by the amount of time since the collision
obj1->pos += obj1->v * t;
obj2->pos += obj2->v * t;
}
}
//////////////////////////////////////////////////
// main
//////////////////////////////////////////////////
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* win;
SDL_Renderer* ren;
win = SDL_CreateWindow("Gravity", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, 0);
ren = SDL_CreateRenderer(win, 0, 0);
if (win == NULL || ren == NULL)
SDL_Log("%s", SDL_GetError());
SDL_SetRenderDrawColor(ren, 20, 20, 40, 255);
SDL_RenderClear(ren);
SDL_RenderPresent(ren);
object objects[NUM_OBJS];
// Parameters: Mass, Radius, Position, Velocity, Color (RGBA)
objects[0] = {1e18, 10.0, { 0, 0}, {0, 0}, {255, 0, 0, 0}}; // Red 'Sun'
objects[1] = {100e12, 4.0, { 100, 0}, {0, 100}, { 0, 100, 0, 0}}; // Green 'Earth'
#if NUM_OBJS > 2
objects[2] = { 20e9, 1.0, {-100, -100}, {2, 0}, { 0, 255, 255, 0}}; // Cyan 'Comet'
std::default_random_engine generator;
std::normal_distribution<double> radius_gen(300.0,20.0);
std::normal_distribution<double> scale_gen(1.0,0.1);
for (int i = 3; i < NUM_OBJS; i++)
{
mat2 rot = {0, -1, 1, 0};
double radius, angle, scale;
bool collision = true;
int count = 0;
while (collision){
radius = radius_gen(generator);
angle = rand() * 2 * M_PI / RAND_MAX;
scale = scale_gen(generator);
objects[i] = {
100e12 * scale / 2.0, 4 * scale, // Mass and size
{radius * sin(angle), radius * cos(angle)}, // Position
{0,0}, {255, 255, 255, 0}}; // Velocity and color
// Detect if there is already a star too close by
collision = false;
for (int k = 0; k < i && !collision; ++k)
collision |= objects_collide(objects + i, objects + k, 0.0);
if (count++ > 10)
{
std::cerr << "Tried more than 10 times to find a suitable spot for new Star. No success...\n";
break;
}
}
// Give all stars a slight rotation
objects[i].v = rot * objects[i].pos / sqrt(len(objects[i].pos)) * 3.0e1;
}
#endif
// Main loop
SDL_Event e;
bool running = true;
bool pause = true;
double dt = 0.001;
const uint8_t* keys = SDL_GetKeyboardState(NULL);
while (running)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
running = false;
if (e.type == SDL_KEYUP)
{
if (e.key.keysym.sym == SDLK_SPACE) pause = !pause;
}
}
// Update screen
SDL_SetRenderDrawColor(ren, 20, 20, 40, 255);
SDL_RenderClear(ren);
for (int i = 0; i < NUM_OBJS; i++)
filledCircleRGBA(ren, objects[i].pos.x + 640, 360 - objects[i].pos.y, objects[i].radius,
objects[i].color.r, objects[i].color.g, objects[i].color.b, 255);
SDL_RenderPresent(ren);
// Restrict to 60 FPS
SDL_Delay(1000 / 60);
sim_time += dt;
if (pause) continue;
// update physics
for (int i = 0; i < NUM_OBJS; i++)
update_pos(objects + i, dt);
for (int i = 1; i < NUM_OBJS; i++)
for (int j = 0; j < i; j++)
handle_collision(objects + i, objects + j, dt);
for (int i = 1; i < NUM_OBJS; i++)
for (int j = 0; j < i; j++)
update_velocities(objects + i, objects + j, dt);
}
return 0;
}