Skip to content
Snippets Groups Projects
Commit 647574ca authored by Florian Burgener's avatar Florian Burgener
Browse files

Add configurable timing to the simulation

parent 1ac257db
No related branches found
No related tags found
No related merge requests found
...@@ -22,17 +22,27 @@ SolarSystem *solar_system_create(double interval) { ...@@ -22,17 +22,27 @@ SolarSystem *solar_system_create(double interval) {
double perihelion_1 = 149.6 * 1E9 * (1 - 0.01671123); double perihelion_1 = 149.6 * 1E9 * (1 - 0.01671123);
solar_system->objects[1] = celestial_object_create(5.972 * 1E24, vector2_create(-perihelion_1, 0)); solar_system->objects[1] = celestial_object_create(5.972 * 1E24, vector2_create(-perihelion_1, 0));
double perihelion_2 = 227.9 * 1E9 * (1 - 0.02934); double perihelion_2 = 227.9 * 1E9 * (1 - 0.09341233);
solar_system->objects[2] = celestial_object_create(6.39 * 1E23, vector2_create(-perihelion_2, 0)); solar_system->objects[2] = celestial_object_create(6.39 * 1E23, vector2_create(-perihelion_2, 0));
return solar_system; return solar_system;
} }
Vector2 gravit(CelestialObject *sun, CelestialObject *b) { CelestialObject *solar_system_get_star(SolarSystem *solar_system) {
Vector2 r = vector2_multiply(b->current_position, -1); return solar_system->objects[0];
double afff = G * sun->mass * (1.0 / pow(vector2_norm(r), 3)); }
Vector2 a = vector2_multiply(r, afff);
// Vector2 a = vector2_multiply(f, 1.0 / b->mass); Vector2 calculate_gravitational_acceleration(SolarSystem *solar_system, int32_t object_index) {
Vector2 a = vector2_create_zero();
for (int32_t i = 0; i < solar_system->objects_length; i += 1) {
if (i == object_index)
continue;
Vector2 r = vector2_substract(solar_system->objects[i]->current_position, solar_system->objects[object_index]->current_position);
double a_scalar = G * solar_system->objects[i]->mass * pow(pow(vector2_norm(r), 2), -1);
a = vector2_add(a, vector2_multiply(vector2_normalize(r), a_scalar));
}
return a; return a;
} }
...@@ -49,7 +59,7 @@ void solar_system_update(SolarSystem *solar_system) { ...@@ -49,7 +59,7 @@ void solar_system_update(SolarSystem *solar_system) {
if (i == 1) { if (i == 1) {
perihelion_speed = sqrt((G * star->mass * (1 + 0.01671123)) / (149.6 * 1E9 * (1 - 0.01671123))); perihelion_speed = sqrt((G * star->mass * (1 + 0.01671123)) / (149.6 * 1E9 * (1 - 0.01671123)));
} else if (i == 2) { } else if (i == 2) {
perihelion_speed = sqrt((G * star->mass * (1 + 0.02934)) / (227.9 * 1E9 * (1 - 0.02934))); perihelion_speed = sqrt((G * star->mass * (1 + 0.09341233)) / (227.9 * 1E9 * (1 - 0.09341233)));
} }
Vector2 tmp = object->current_position; Vector2 tmp = object->current_position;
...@@ -62,7 +72,7 @@ void solar_system_update(SolarSystem *solar_system) { ...@@ -62,7 +72,7 @@ void solar_system_update(SolarSystem *solar_system) {
object->current_position = new_position; object->current_position = new_position;
} else { } else {
Vector2 new_position = vector2_substract(vector2_multiply(object->current_position, 2), object->previous_position); Vector2 new_position = vector2_substract(vector2_multiply(object->current_position, 2), object->previous_position);
Vector2 a = gravit(solar_system->objects[0], object); Vector2 a = calculate_gravitational_acceleration(solar_system, i);
new_position = vector2_add(new_position, vector2_multiply(a, pow(solar_system->interval, 2))); new_position = vector2_add(new_position, vector2_multiply(a, pow(solar_system->interval, 2)));
object->previous_position = object->current_position; object->previous_position = object->current_position;
object->current_position = new_position; object->current_position = new_position;
...@@ -73,18 +83,18 @@ void solar_system_update(SolarSystem *solar_system) { ...@@ -73,18 +83,18 @@ void solar_system_update(SolarSystem *solar_system) {
void solar_system_draw(SolarSystem *solar_system, struct gfx_context_t *context) { void solar_system_draw(SolarSystem *solar_system, struct gfx_context_t *context) {
gfx_clear(context, COLOR_BLACK); gfx_clear(context, COLOR_BLACK);
for (uint32_t i = 0; i < solar_system->objects_length; i += 1) { for (int32_t i = 0; i < solar_system->objects_length; i += 1) {
CelestialObject *object = solar_system->objects[i]; CelestialObject *object = solar_system->objects[i];
Vector2 scaled_position = vector2_multiply(object->current_position, 1.0 / (250 * 1E9)); Vector2 scaled_position = vector2_multiply(object->current_position, 1.0 / (300 * 1E9));
scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT); scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT);
if (i == 0) { if (i == 0) {
draw_full_circle(context, scaled_position.x, scaled_position.y, 70, COLOR_YELLOW); draw_full_circle(context, scaled_position.x, scaled_position.y, 50, COLOR_YELLOW);
} }
if (i == 1) { if (i == 1) {
draw_full_circle(context, scaled_position.x, scaled_position.y, 15, COLOR_BLUE); draw_full_circle(context, scaled_position.x, scaled_position.y, 20, COLOR_BLUE);
} }
if (i == 2) { if (i == 2) {
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "SolarSystem.h" #include "SolarSystem.h"
#include "Vector2.h" #include "Vector2.h"
...@@ -22,19 +21,41 @@ int main() { ...@@ -22,19 +21,41 @@ int main() {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
SolarSystem *solar_system = solar_system_create(10000); int32_t time_elapsing_per_second = 3600 * 24 * 365;
int32_t update_per_second = 1000;
printf("%d\n", time_elapsing_per_second / update_per_second);
SolarSystem *solar_system = solar_system_create(time_elapsing_per_second / update_per_second);
int32_t hz = 240;
int32_t a = 0;
int32_t b = 0;
int32_t elapsed_seconds = 0;
while (true) {
gfx_present(context);
while (true) {
solar_system_update(solar_system); solar_system_update(solar_system);
b += 1;
if ((double)b >= (double)update_per_second / hz * (a + 1)) {
a += 1;
gfx_present(context);
solar_system_draw(solar_system, context); solar_system_draw(solar_system, context);
if (gfx_keypressed() == SDLK_ESCAPE) { if (b >= update_per_second) {
break; b = 0;
elapsed_seconds += 1;
char title[100];
sprintf(title, "Solar System (%d)", elapsed_seconds);
SDL_SetWindowTitle(context->window, title);
}
if (a % hz == 0)
a = 0;
} }
sleep(0.01); if (gfx_keypressed() == SDLK_ESCAPE)
break;
struct timespec t = {.tv_sec = 0, .tv_nsec = 1.0 / update_per_second * 1E9};
nanosleep(&t, NULL);
} }
gfx_destroy(context); gfx_destroy(context);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment