diff --git a/SolarSystem.c b/SolarSystem.c index 9fa935a4117184939256ddf089e7b21cdcf3bde4..62f3084f53162d82ab5e6002b59fda1bf920d105 100644 --- a/SolarSystem.c +++ b/SolarSystem.c @@ -22,17 +22,27 @@ SolarSystem *solar_system_create(double interval) { double perihelion_1 = 149.6 * 1E9 * (1 - 0.01671123); 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)); return solar_system; } -Vector2 gravit(CelestialObject *sun, CelestialObject *b) { - Vector2 r = vector2_multiply(b->current_position, -1); - 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); +CelestialObject *solar_system_get_star(SolarSystem *solar_system) { + return solar_system->objects[0]; +} + +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; } @@ -49,7 +59,7 @@ void solar_system_update(SolarSystem *solar_system) { if (i == 1) { perihelion_speed = sqrt((G * star->mass * (1 + 0.01671123)) / (149.6 * 1E9 * (1 - 0.01671123))); } 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; @@ -62,7 +72,7 @@ void solar_system_update(SolarSystem *solar_system) { object->current_position = new_position; } else { 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))); object->previous_position = object->current_position; object->current_position = new_position; @@ -73,18 +83,18 @@ void solar_system_update(SolarSystem *solar_system) { void solar_system_draw(SolarSystem *solar_system, struct gfx_context_t *context) { 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]; - 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); 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) { - 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) { diff --git a/gfx/gfx.c b/gfx/gfx.c index 0d7ca416e10337dddd73d89bf9fe2be71fc16c70..3821077bbbf7964c49e73b9b94d6dc123bcb9d9f 100644 --- a/gfx/gfx.c +++ b/gfx/gfx.c @@ -147,4 +147,4 @@ void draw_full_circle(struct gfx_context_t *ctxt, uint32_t c_column, uint32_t c_ } if (r > 0) draw_full_circle(ctxt, c_column, c_row, r - 1, color); -} \ No newline at end of file +} diff --git a/main.c b/main.c index db9bab2e4a9681f96ee694ac02468efd7905b7b3..a3fdb8d21ba34a00696e5a7da8d0af8e3ce4eeb7 100644 --- a/main.c +++ b/main.c @@ -2,7 +2,6 @@ #include <stdio.h> #include <stdlib.h> #include <time.h> -#include <unistd.h> #include "SolarSystem.h" #include "Vector2.h" @@ -22,19 +21,41 @@ int main() { 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_draw(solar_system, context); + b += 1; + + if ((double)b >= (double)update_per_second / hz * (a + 1)) { + a += 1; + + gfx_present(context); + solar_system_draw(solar_system, context); + + if (b >= update_per_second) { + 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; + } - if (gfx_keypressed() == SDLK_ESCAPE) { + if (gfx_keypressed() == SDLK_ESCAPE) break; - } - - sleep(0.01); + struct timespec t = {.tv_sec = 0, .tv_nsec = 1.0 / update_per_second * 1E9}; + nanosleep(&t, NULL); } gfx_destroy(context);