diff --git a/PlanetarySystem.c b/PlanetarySystem.c index 0205d56e72cceff57737fa7b69eb735b857d55e4..80591e9f8b4ebe845c29817c0d330bdc023573b0 100644 --- a/PlanetarySystem.c +++ b/PlanetarySystem.c @@ -68,7 +68,7 @@ Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system, return a; } -void planetary_system_update(PlanetarySystem *planetary_system, double interval, bool save_position) { +void planetary_system_update(PlanetarySystem *planetary_system, double interval) { for (uint32_t i = 1; i < planetary_system->objects_length; i += 1) { CelestialObject *object = planetary_system->objects[i]; Vector2 current_position = object->current_position; @@ -102,16 +102,18 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval, object->previous_position = object->current_position; object->current_position = new_position; - if (object->points_length < 200 && save_position) { - object->points[object->points_length] = object->current_position; - object->points_length += 1; - } else if (save_position) { - for (int32_t j = 1; j < object->points_length; j += 1) { - object->points[j - 1] = object->points[j]; - } + int32_t length = object->points_length; - object->points[object->points_length - 1] = object->current_position; + if (length > 0 && vector2_norm(vector2_substract(object->points[0], object->current_position)) < 1E9 * 1.5) + continue; + for (int32_t j = (length == 200) ? length - 1 : length; j >= 1; j -= 1) { + object->points[j] = object->points[j - 1]; } + + object->points[0] = object->current_position; + + if (length < 200) + object->points_length += 1; } } diff --git a/PlanetarySystem.h b/PlanetarySystem.h index 21fb7e80a5e45f11ad0c95e50881adfeb0dc9f9b..7c7f12c4b45ded30596fdb4cfa28bfcd2200a81a 100644 --- a/PlanetarySystem.h +++ b/PlanetarySystem.h @@ -17,7 +17,7 @@ typedef struct { } PlanetarySystem; PlanetarySystem *planetary_system_create(); -void planetary_system_update(PlanetarySystem *planetary_system, double interval, bool save_position); +void planetary_system_update(PlanetarySystem *planetary_system, double interval); void planetary_system_draw(PlanetarySystem *planetary_system); #endif diff --git a/main.c b/main.c index 34f56666af6c8b15239a9db4dcc5fb45d8cdfe50..d2af39b41d3169878b72da4a3cda1678531badb7 100644 --- a/main.c +++ b/main.c @@ -9,7 +9,7 @@ #define WINDOW_NAME "Solar System" #define REFRESH_RATE 200 -#define TIME_ELASPING_PER_SECOND 3600 * 24 * 80 +#define TIME_ELASPING_PER_SECOND 3600 * 24 * 10 #define TIME_ELASPING_PER_UPDATE 100 // https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning @@ -37,7 +37,7 @@ void update_timer() { elapsed_time += 1; for (uint32_t i = 0; i < TIME_ELASPING_PER_SECOND / REFRESH_RATE / TIME_ELASPING_PER_UPDATE; i += 1) { - planetary_system_update(planetary_system, TIME_ELASPING_PER_UPDATE, i == 0); + planetary_system_update(planetary_system, TIME_ELASPING_PER_UPDATE); } glutTimerFunc(1000 / REFRESH_RATE, update_timer, 0);