Skip to content
Snippets Groups Projects
Commit 5464edb2 authored by florian.burgener's avatar florian.burgener
Browse files

Fix trail length

parent 3285d3c5
No related branches found
No related tags found
No related merge requests found
...@@ -68,7 +68,7 @@ Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system, ...@@ -68,7 +68,7 @@ Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system,
return a; 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) { for (uint32_t i = 1; i < planetary_system->objects_length; i += 1) {
CelestialObject *object = planetary_system->objects[i]; CelestialObject *object = planetary_system->objects[i];
Vector2 current_position = object->current_position; Vector2 current_position = object->current_position;
...@@ -102,16 +102,18 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval, ...@@ -102,16 +102,18 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval,
object->previous_position = object->current_position; object->previous_position = object->current_position;
object->current_position = new_position; object->current_position = new_position;
if (object->points_length < 200 && save_position) { int32_t length = object->points_length;
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];
}
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;
} }
} }
......
...@@ -17,7 +17,7 @@ typedef struct { ...@@ -17,7 +17,7 @@ typedef struct {
} PlanetarySystem; } PlanetarySystem;
PlanetarySystem *planetary_system_create(); 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); void planetary_system_draw(PlanetarySystem *planetary_system);
#endif #endif
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#define WINDOW_NAME "Solar System" #define WINDOW_NAME "Solar System"
#define REFRESH_RATE 200 #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 #define TIME_ELASPING_PER_UPDATE 100
// https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning // https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning
...@@ -37,7 +37,7 @@ void update_timer() { ...@@ -37,7 +37,7 @@ void update_timer() {
elapsed_time += 1; elapsed_time += 1;
for (uint32_t i = 0; i < TIME_ELASPING_PER_SECOND / REFRESH_RATE / TIME_ELASPING_PER_UPDATE; i += 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); glutTimerFunc(1000 / REFRESH_RATE, update_timer, 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment