From 5464edb2209fa223dd572a6a52ec31327afecf74 Mon Sep 17 00:00:00 2001
From: Florian Burgener <florian.burgener@etu.hesge.ch>
Date: Mon, 13 Dec 2021 00:24:59 +0100
Subject: [PATCH] Fix trail length

---
 PlanetarySystem.c | 20 +++++++++++---------
 PlanetarySystem.h |  2 +-
 main.c            |  4 ++--
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/PlanetarySystem.c b/PlanetarySystem.c
index 0205d56..80591e9 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 21fb7e8..7c7f12c 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 34f5666..d2af39b 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);
-- 
GitLab