diff --git a/CelestialObject.h b/CelestialObject.h index 228f870a4be2ef9e06bae066d3971edb1a53d608..6c6c2c9e65dd4e82fc3e3226d8342c3020ed1cbf 100644 --- a/CelestialObject.h +++ b/CelestialObject.h @@ -1,3 +1,12 @@ +/** + * @file CelestialObject.h + * @author Gawen ACKERMANN and Florian BURGENER + * @brief Set of functions and structures allowing the simulation of a celestial object. + * @version 1.0 + * @date 2021-12-19 + * + */ + #ifndef CELESTIAL_OBJECT_H #define CELESTIAL_OBJECT_H @@ -16,14 +25,89 @@ typedef struct CelestialObject { int32_t previous_positions_length; } CelestialObject; +/** + * @brief Creates a new CelestialObject. + * + * @param name + * @param mass + * @param semi_major_axis + * @param eccentricity + * @param drawing_disc_radius + * @param drawing_color + * @return CelestialObject* + */ CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, int32_t drawing_disc_radius, int32_t drawing_color); + +/** + * @brief Destroys a PlanetarySystem. + * + * @param object + */ void celestial_object_destroy(CelestialObject *object); + +/** + * @brief Gets the radius of the circle after applying the zoom. + * + * @param object + * @param zoom_factor + * @return int32_t + */ int32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_factor); + +/** + * @brief Calculates the gravitational acceleration applied to the object. + * + * @param object_index + * @param objects + * @param objects_length + * @return Vector2 + */ Vector2 calculate_gravitational_acceleration(int32_t object_index, CelestialObject **objects, int32_t objects_length); + +/** + * @brief Calculates the first update. + * + * @param object_index + * @param objects + * @param objects_length + * @param main_object_index + */ void celestial_object_first_update(int32_t object_index, CelestialObject **objects, int32_t objects_length, int32_t main_object_index); + +/** + * @brief Updates the object. + * + * @param object_index + * @param objects + * @param objects_length + * @param interval + * @param previous_interval + */ void celestial_object_update(int32_t object_index, CelestialObject **objects, int32_t objects_length, double interval, double previous_interval); + +/** + * @brief Updates the previous positions of the object. + * + * @param object + */ void celestial_object_update_previous_positions(CelestialObject *object); + +/** + * @brief Draws the object. + * + * @param object + * @param reference_frame + * @param zoom_factor + */ void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor); + +/** + * @brief Draws the name of the object. + * + * @param object + * @param reference_frame + * @param zoom_factor + */ void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame, double zoom_factor); #endif diff --git a/PlanetarySystem.c b/PlanetarySystem.c index 55179051da159e45f14ecfb5850ad78b162487c3..bccc01aeabe188d2d3eadba3ed5810309e4e409a 100644 --- a/PlanetarySystem.c +++ b/PlanetarySystem.c @@ -79,7 +79,7 @@ const int32_t NABOO_DRAWING_RADIUS = 13; const int32_t NABOO_COLOR = 0x00008B; // Endor const char ENDOR_NAME[] = "Endor"; -const double ENDOR_MASS = 1E23; +const double ENDOR_MASS = 0.1 * 1E24; const double ENDOR_SEMI_MAJOR_AXIS = 100 * 1E9; const double ENDOR_ECCENTRICITY = 0.34; const int32_t ENDOR_DRAWING_RADIUS = 8; diff --git a/PlanetarySystem.h b/PlanetarySystem.h index 66d4959cdd63f15a2b37d13f1d31d3972bfbe3c3..16ae69f236b03f0bff70390390aed597132164ca 100644 --- a/PlanetarySystem.h +++ b/PlanetarySystem.h @@ -1,3 +1,12 @@ +/** + * @file PlanetarySystem.h + * @author Gawen ACKERMANN and Florian BURGENER + * @brief Set of functions and structures allowing the simulation of a planetary system. + * @version 1.0 + * @date 2021-12-19 + * + */ + #ifndef PLANETARY_SYSTEM_H #define PLANETARY_SYSTEM_H @@ -18,11 +27,48 @@ typedef struct PlanetarySystem { bool show_names; } PlanetarySystem; +/** + * @brief Creates a new PlanetarySystem. + * + * @return PlanetarySystem* + */ PlanetarySystem *planetary_system_create(); + +/** + * @brief Destroys a PlanetarySystem. + * + * @param planetary_system + */ void planetary_system_destroy(PlanetarySystem *planetary_system); + +/** + * @brief Gets the reference frame vector. + * + * @param planetary_system + * @return Vector2 + */ Vector2 planetary_system_get_reference_frame(PlanetarySystem *planetary_system); + +/** + * @brief Updates the planetary system. + * + * @param planetary_system + * @param interval + */ void planetary_system_update(PlanetarySystem *planetary_system, double interval); + +/** + * @brief Draws the planetary system. + * + * @param planetary_system + */ void planetary_system_draw(PlanetarySystem *planetary_system); + +/** + * @brief Draws the names of the objects. + * + * @param planetary_system + */ void planetary_system_draw_object_names(PlanetarySystem *planetary_system); #endif diff --git a/README.md b/README.md index cb7494df7b8e0b0214fb857ca5965448bf5e3be4..f61b12d5a2930cb4abda38ef09d1bb2e45fa108a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ -# Rapport du travail pratique de physique - SolarSystemSimulator +# Rapport du travail pratique de physique - Simulation d'un système planétaire -Ce travail est réalisé par M. Ackermann Gawen et M. Burgener Florian, dans le cours de physique appliquée à l'ingénirie. +Ce travail est réalisé par M. Gawen ACKERMANN et M. Florian BURGENER, dans le cours du cours de physique appliquée à l'Ingénierie. ## But -Le but de ce projet est de réaliser un simulateur de système planétaire. +Le but de ce projet est de créer une simulation d'un système planétaire, il nous permet notamment d'appliquer les 3 lois de Newton vu dans le cours de Physique ainsi que de manipuler des vecteurs. + +## Installation et Compilation ## Projet @@ -13,13 +15,20 @@ Afin de mener ce projet à terme, nous avons dû créer certaines de nos structu ### Données -| Objet céleste | Masse | Excentricité | Demi-grand axe | +https://nssdc.gsfc.nasa.gov/planetary/factsheet/ +https://nssdc.gsfc.nasa.gov/planetary/factsheet/sunfact.html +https://nssdc.gsfc.nasa.gov/planetary/factsheet/mercuryfact.html +https://nssdc.gsfc.nasa.gov/planetary/factsheet/venusfact.html +https://nssdc.gsfc.nasa.gov/planetary/factsheet/earthfact.html +https://nssdc.gsfc.nasa.gov/planetary/factsheet/moonfact.html + +| Nom | Masse | Excentricité | Demi-grand axe | |:-------------:|:---------------:|:------------:|:----------------:| -| Soleil | 1.988900 * 1E30 | / | / | -| Mercure | 0.33018 * 1E24 | 0.20563069 | 579.09227 * 1E8 | -| Venus | 4.8685 * 1E24 | 0.00677323 | 108.208475 * 1E9 | -| Terre | 5.9736 * 1E24 | 0.01671022 | 149.598262 * 1E9 | -| Mars | 0.64185 * 1E24 | 0.09341233 | 227.943824 * 1E9 | +| Soleil | 1988500 * 1E24 | / | / | +| Mercure | 0.33010 * 1E24 | 0.2056 | 57.909 * 1E9 | +| Venus | 4.8673 * 1E24 | 0.0067 | 108.209 * 1E9 | +| Terre | 5.9722 * 1E24 | 0.0167 | 149.596 * 1E9 | +| Mars | 0.64169 * 1E24 | 0.0935 | 227.923 * 1E9 | ### Mise en place @@ -29,23 +38,31 @@ Pour utiliser le simulateur, il faut en premier temps clone ce repository. Ensui #### CelestialObject -Représente un corps céleste tel que : Une planète ou une étoile. +Représente un corps céleste comme par exmple : une planète ou une étoile. ```c typedef struct CelestialObject { + char name[100]; double mass; Vector2 previous_position; Vector2 position; double semi_major_axis; double eccentricity; - uint32_t drawing_disc_radius; - uint32_t drawing_color; - Vector2 *points; - int32_t points_length; + int32_t drawing_disc_radius; + int32_t drawing_color; + Vector2 *previous_positions; + int32_t previous_positions_length; } CelestialObject; -CelestialObject *celestial_object_create(double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color); - +CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, int32_t drawing_disc_radius, int32_t drawing_color); +void celestial_object_destroy(CelestialObject *object); +int32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_factor); +Vector2 calculate_gravitational_acceleration(int32_t object_index, CelestialObject **objects, int32_t objects_length); +void celestial_object_first_update(int32_t object_index, CelestialObject **objects, int32_t objects_length, int32_t main_object_index); +void celestial_object_update(int32_t object_index, CelestialObject **objects, int32_t objects_length, double interval, double previous_interval); +void celestial_object_update_previous_positions(CelestialObject *object); +void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor); +void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame, double zoom_factor); ``` #### PlanetarySystem @@ -53,33 +70,35 @@ CelestialObject *celestial_object_create(double mass, double semi_major_axis, do Représente un système planétaire composé d'une étoile en son centre et des planètes orbitant autour. ```c -typedef struct { - uint32_t objects_length; +typedef struct PlanetarySystem { CelestialObject **objects; - double interval; + int32_t objects_length; + int32_t reference_frame_index; double zoom_factor; - int32_t reference_frame_object_index; + double previous_interval; + bool show_names; } PlanetarySystem; PlanetarySystem *planetary_system_create(); +void planetary_system_destroy(PlanetarySystem *planetary_system); +Vector2 planetary_system_get_reference_frame(PlanetarySystem *planetary_system); void planetary_system_update(PlanetarySystem *planetary_system, double interval); void planetary_system_draw(PlanetarySystem *planetary_system); +void planetary_system_draw_object_names(PlanetarySystem *planetary_system); ``` -#### Vector 2 +#### Vector2 Représente un vecteur en 2 dimensions. ```c -typedef struct Vector2 Vector2; -struct Vector2 { +typedef struct Vector2 { double x; double y; -}; +} Vector2; Vector2 vector2_create(double x, double y); Vector2 vector2_create_zero(); -bool vector2_is_zero(Vector2 v); Vector2 vector2_add(Vector2 a, Vector2 b); Vector2 vector2_substract(Vector2 a, Vector2 b); Vector2 vector2_multiply(Vector2 v, double scalar); @@ -87,8 +106,7 @@ double vector2_dot_product(Vector2 a, Vector2 b); double vector2_norm_sqr(Vector2 v); double vector2_norm(Vector2 v); Vector2 vector2_normalize(Vector2 v); -bool vector2_is_similiar(Vector2 a, Vector2 b, double epsilon); -Vector2 vector2_fit_canvas(Vector2 v, uint32_t width, uint32_t height); +Vector2 vector2_fit_canvas(Vector2 v, int32_t width, int32_t height); void vector2_print(Vector2 v); ``` diff --git a/Vector2.c b/Vector2.c index 1f8931f78d8c23bf10267b081dbf263cba9d199b..4d1a6f2574c25faed47343598ece10a0cf6a3cd5 100644 --- a/Vector2.c +++ b/Vector2.c @@ -13,10 +13,6 @@ Vector2 vector2_create_zero() { return vector2_create(0, 0); } -bool vector2_is_zero(Vector2 v) { - return v.x == 0 && v.y == 0; -} - Vector2 vector2_add(Vector2 a, Vector2 b) { return vector2_create(a.x + b.x, a.y + b.y); } diff --git a/Vector2.h b/Vector2.h index 31f8483305ce698bacaef6db335506d287ceb35c..317401ab78ebdae72a1984d1397cc92fafc21a89 100644 --- a/Vector2.h +++ b/Vector2.h @@ -1,3 +1,12 @@ +/** + * @file Vector2.h + * @author Gawen ACKERMANN and Florian BURGENER + * @brief Set of functions and structures allowing the manipulation of a vector 2. + * @version 1.0 + * @date 2021-12-19 + * + */ + #ifndef VECTOR2_H #define VECTOR2_H @@ -9,17 +18,97 @@ typedef struct Vector2 { double y; } Vector2; +/** + * @brief Creates a new Vector2. + * + * @param x + * @param y + * @return Vector2 + */ Vector2 vector2_create(double x, double y); + +/** + * @brief Creates a new Vector2 where x=0 and y=0. + * + * @return Vector2 + */ Vector2 vector2_create_zero(); -bool vector2_is_zero(Vector2 v); + +/** + * @brief Sum two vectors and return the result in a new one. + * + * @param a + * @param b + * @return Vector2 + */ Vector2 vector2_add(Vector2 a, Vector2 b); + +/** + * @brief Subtract two vectors and return the result in a new one. + * + * @param a + * @param b + * @return Vector2 + */ Vector2 vector2_substract(Vector2 a, Vector2 b); + +/** + * @brief Multiply two vectors and return the result in a new one. + * + * @param v + * @param scalar + * @return Vector2 + */ Vector2 vector2_multiply(Vector2 v, double scalar); + +/** + * @brief Computes the dot product of two vectors. + * + * @param a + * @param b + * @return double + */ double vector2_dot_product(Vector2 a, Vector2 b); + +/** + * @brief Calculates the norm of a vector and returns the square of the norm. + * + * @param v + * @return double + */ double vector2_norm_sqr(Vector2 v); + +/** + * @brief Calculates the norm of a vector. + * + * @param v + * @return double + */ double vector2_norm(Vector2 v); + +/** + * @brief Normalizes a vector and return the result in a new one. + * + * @param v + * @return Vector2 + */ Vector2 vector2_normalize(Vector2 v); + +/** + * @brief Convert a vector (-1 to 1) into a width x height rectangle. + * + * @param v + * @param width + * @param height + * @return Vector2 + */ Vector2 vector2_fit_canvas(Vector2 v, int32_t width, int32_t height); + +/** + * @brief Displays a vector in the console. + * + * @param v + */ void vector2_print(Vector2 v); #endif diff --git a/drawing.h b/drawing.h index 955ea7c1ee606a63976a76cbd630265f99afe352..63ec755aa512ffa9b6240526f763d54599b16db8 100644 --- a/drawing.h +++ b/drawing.h @@ -1,3 +1,12 @@ +/** + * @file drawing.h + * @author Gawen ACKERMANN and Florian BURGENER + * @brief Set of functions for drawing shapes and other things. + * @version 1.0 + * @date 2021-12-19 + * + */ + #ifndef DRAWING_H #define DRAWING_H @@ -5,10 +14,48 @@ #include "Vector2.h" +/** + * @brief Scales the position to the planetary system and takes into account the referential and the zoom level. + * + * @param position + * @param reference_frame + * @param zoom_factor + * @return Vector2 + */ Vector2 scale_position(Vector2 position, Vector2 reference_frame, double zoom_factor); + +/** + * @brief Draws a disc. + * + * @param position + * @param radius + */ void draw_disc(Vector2 position, int32_t radius); + +/** + * @brief Draws a line. + * + * @param p1 + * @param p2 + */ void draw_line(Vector2 p1, Vector2 p2); + +/** + * @brief Draws lines and scale them to the planetary system. + * + * @param points + * @param points_length + * @param reference_frame + * @param zoom_factor + */ void draw_scaled_lines(Vector2 *points, int32_t points_length, Vector2 reference_frame, double zoom_factor); + +/** + * @brief Draws text at the desired position. + * + * @param text + * @param position + */ void draw_text(char *text, Vector2 position); #endif diff --git a/main.c b/main.c index de71c1c353483305f98991ba7390a49e7494e4ce..f4beb6dcd77f5ea0cd4269870970cf8695287ec5 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,12 @@ +/** + * @file main.c + * @author Gawen ACKERMANN and Florian BURGENER + * @brief Simulation of a planetary system. + * @version 1.0 + * @date 2021-12-19 + * + */ + #include <GL/freeglut.h> #include <GL/glut.h> #include <stdint.h> @@ -48,12 +57,21 @@ int32_t simulation_speed = DEFAULT_SIMULATION_SPEED_IN_SECONDS; int64_t start_time; int64_t previous_time; +/** + * @brief Get the current time in microseconds. + * + * @return int64_t + */ int64_t get_current_time() { struct timeval time; gettimeofday(&time, NULL); return (int64_t)time.tv_sec * (int64_t)ONE_SECOND_IN_MICROSECONDS + (int64_t)time.tv_usec; } +/** + * @brief Updates the planetary system. + * + */ void update() { int64_t current_time = get_current_time(); int64_t elapsed_time = current_time - previous_time; @@ -71,11 +89,19 @@ void update() { previous_time = current_time; } +/** + * @brief Timer for the "update" function. + * + */ void update_timer() { update(); glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, update_timer, 0); } +/** + * @brief Draws the planetary system. + * + */ void draw() { glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); @@ -94,24 +120,45 @@ void draw() { glutSwapBuffers(); } +/** + * @brief Timer for the "draw" function. + * + */ void draw_timer() { glutPostRedisplay(); glutTimerFunc(ONE_SECOND_IN_MILLISECONDS / REFRESH_RATE, draw_timer, 0); } +/** + * @brief Event that retrieves the keyboard inputs. + * + * @param key + * @param UNUSED + * @param UNUSED + */ void handle_keyboard_input(unsigned char key, int UNUSED(x), int UNUSED(y)) { if (key == GLUT_KEY_ESCAPE) { + // Quits the program. glutLeaveMainLoop(); return; } if (key == GLUT_KEY_T) { + // Toggles the display of object names. planetary_system->show_names = !planetary_system->show_names; } } +/** + * @brief Event that retrieves the special keyboard inputs. + * + * @param key + * @param UNUSED + * @param UNUSED + */ void handle_special_keyboard_input(int key, int UNUSED(x), int UNUSED(y)) { if (key == GLUT_KEY_LEFT) { + // Follows the previous object. planetary_system->reference_frame_index -= 1; if (planetary_system->reference_frame_index < 0) { @@ -120,11 +167,13 @@ void handle_special_keyboard_input(int key, int UNUSED(x), int UNUSED(y)) { } if (key == GLUT_KEY_RIGHT) { + // Follows the next object. planetary_system->reference_frame_index += 1; planetary_system->reference_frame_index %= planetary_system->objects_length; } if (key == GLUT_KEY_UP) { + // Increases the simulation speed. simulation_speed += ONE_DAY_IN_SECONDS; if (simulation_speed > ONE_DAY_IN_SECONDS * SIMULATION_MAXIMUM_SPEED_IN_DAYS) { @@ -133,6 +182,7 @@ void handle_special_keyboard_input(int key, int UNUSED(x), int UNUSED(y)) { } if (key == GLUT_KEY_DOWN) { + // Decreases the simulation speed. simulation_speed -= ONE_DAY_IN_SECONDS; if (simulation_speed < 0) { @@ -141,18 +191,35 @@ void handle_special_keyboard_input(int key, int UNUSED(x), int UNUSED(y)) { } } +/** + * @brief Event that retrieves actions performed with the mouse. + * + * @param button + * @param state + * @param UNUSED + * @param UNUSED + */ void handle_mouse_input(int button, int state, int UNUSED(x), int UNUSED(y)) { if (button == 3 || button == 4) { if (state == GLUT_UP) return; if (button == 3) { + // Zoom in. planetary_system->zoom_factor *= ZOOM_MULTIPLIER; } else { + // Zoom out. planetary_system->zoom_factor /= ZOOM_MULTIPLIER; } } } +/** + * @brief Entry point of the program. + * + * @param argc + * @param argv + * @return int + */ int main(int argc, char *argv[]) { planetary_system = planetary_system_create(); @@ -180,6 +247,7 @@ int main(int argc, char *argv[]) { glutMainLoop(); + // Destroys the window and the planetary system. glutDestroyWindow(window); planetary_system_destroy(planetary_system);