diff --git a/CelestialObject.c b/CelestialObject.c index 1d8298086abdbcc2c81574f0ad633b27402e013c..60f06c643ed407fa1c4769168f65c60594c0a6f3 100644 --- a/CelestialObject.c +++ b/CelestialObject.c @@ -12,7 +12,7 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color) { CelestialObject *object = (CelestialObject *)malloc(sizeof(CelestialObject)); - object->name = name; + strcpy(object->name, name); object->mass = mass; object->previous_position = vector2_create_zero(); @@ -29,7 +29,7 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_ma return object; } -void draw_celestial_object(CelestialObject *object, Vector2 reference_frame, double zoom_factor) { +void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor) { Vector2 scaled_position = scale_position(zoom_factor, object->current_position, reference_frame); uint32_t color = object->drawing_color; @@ -54,5 +54,5 @@ void draw_celestial_object(CelestialObject *object, Vector2 reference_frame, dou return; } - draw_text(object->name, scaled_position); + draw_text(object->name, vector2_add(scaled_position, vector2_create(8, 32))); } diff --git a/CelestialObject.h b/CelestialObject.h index 02b013e7fe80d479b1a4fb1bc9cefbb3ca0a3426..cecf815d72fff0d3b5d29e380834ef519d64c3eb 100644 --- a/CelestialObject.h +++ b/CelestialObject.h @@ -13,10 +13,10 @@ typedef struct CelestialObject { uint32_t drawing_color; Vector2 *points; int32_t points_length; - char *name; + char name[100]; } CelestialObject; CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color); -void draw_celestial_object(CelestialObject *object, Vector2 reference_frame, double zoom_factor); +void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor); #endif diff --git a/PlanetarySystem.c b/PlanetarySystem.c index eac70b63b614fe6316969e1b43cd535bf7b87f78..72efa145628e26b8c2b60cf91cf81b8b7a40eca5 100644 --- a/PlanetarySystem.c +++ b/PlanetarySystem.c @@ -2,6 +2,7 @@ #include <GL/glut.h> #include <math.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -34,50 +35,37 @@ const double EARTH_SEMI_MAJOR_AXIS = 149.598262 * 1E9; const double MARS_SEMI_MAJOR_AXIS = 227.943824 * 1E9; PlanetarySystem *planetary_system_create() { - PlanetarySystem *planetary_system = (PlanetarySystem *)malloc(sizeof(PlanetarySystem)); - - planetary_system->objects_length = 9; - planetary_system->objects = (CelestialObject **)malloc(sizeof(PlanetarySystem *) * planetary_system->objects_length); - planetary_system->zoom_factor = 1; - planetary_system->reference_frame_object_index = 0; - - char names[][100] = {"", "Mercury", "Venus", "Earth", "Mars", "Moon", "Naboo", "Jupitaire", "Endor"}; - double masses[] = {MERCURY_MASS, VENUS_MASS, EARTH_MASS, MARS_MASS, 0, 0, 0, 0}; - - // Sun - planetary_system->objects[0] = celestial_object_create("", SUN_MASS, 0, 0, 50, 0xFFFFFF); - // Mercury - planetary_system->objects[1] = celestial_object_create("Mercury", MERCURY_MASS, MERCURY_SEMI_MAJOR_AXIS, MERCURY_ECCENTRICITY, 10, 0xDBCECA); - // Venus - planetary_system->objects[2] = celestial_object_create("Venus", VENUS_MASS, VENUS_SEMI_MAJOR_AXIS, VENUS_ECCENTRICITY, 20, 0x8B7D82); - // Earth - planetary_system->objects[3] = celestial_object_create("Earth", EARTH_MASS, EARTH_SEMI_MAJOR_AXIS, EARTH_ECCENTRICITY, 20, 0x6b93d6); - // Mars - planetary_system->objects[4] = celestial_object_create("Mars", MARS_MASS, MARS_SEMI_MAJOR_AXIS, MARS_ECCENTRICITY, 12, 0xBC2732); - - // Moon - planetary_system->objects[5] = celestial_object_create("Moon", 7.34767309 * 1E22, 384.399 * 1E6, 0.0549, 5, 0x7D7B67); - planetary_system->objects[5]->current_position.x += planetary_system->objects[3]->current_position.x; - - // Fake planets are all green. - // Fake Planet 1 - planetary_system->objects[6] = celestial_object_create("Naboo", 1000000, 800.598262 * 1E9, 0.8, 12, 0x005500); - // Fake Planet 2 (Fake Jupyter) - planetary_system->objects[7] = celestial_object_create("Jupitaire", 1898.6 * 1E10, 778.340821 * 1E9, 0.04839266, 12, 0x005500); - // Fake Planet 3 - planetary_system->objects[8] = celestial_object_create("Endor", 1E23, 100 * 1E9, 0.34, 12, 0x005500); - - return planetary_system; + PlanetarySystem *ps = (PlanetarySystem *)malloc(sizeof(PlanetarySystem)); + + ps->objects_length = 9; + ps->objects = (CelestialObject **)malloc(sizeof(PlanetarySystem *) * ps->objects_length); + ps->zoom_factor = 1; + ps->reference_frame_object_index = 0; + + char names[][100] = {"Sun", "Mercury", "Venus", "Earth", "Mars", "Moon", "Naboo", "Jupitaire", "Endor"}; + double masses[] = {SUN_MASS, MERCURY_MASS, VENUS_MASS, EARTH_MASS, MARS_MASS, 7.34767309 * 1E22, 1000000, 1898.6 * 1E10, 1E23, 100 * 1E9}; + double semi_major_axes[] = {0, MERCURY_SEMI_MAJOR_AXIS, VENUS_SEMI_MAJOR_AXIS, EARTH_SEMI_MAJOR_AXIS, MARS_SEMI_MAJOR_AXIS, 384.399 * 1E6, 800.598262 * 1E9, 778.340821 * 1E9, 100 * 1E9}; + double eccentricities[] = {0, MERCURY_ECCENTRICITY, VENUS_ECCENTRICITY, EARTH_ECCENTRICITY, MARS_ECCENTRICITY, 0.0549, 0.8, 0.04839266, 0.34}; + double disc_radiuses[] = {50, 10, 20, 10, 12, 5, 12, 12, 12}; + int32_t colors[] = {0xFFFFFF, 0xDBCECA, 0x8B7D82, 0x6b93d6, 0xBC2732, 0x7D7B67, 0x005500, 0x005500, 0x005500}; + + for (int32_t i = 0; i < ps->objects_length; i += 1) { + CelestialObject *object = celestial_object_create(names[i], masses[i], semi_major_axes[i], eccentricities[i], disc_radiuses[i], colors[i]); + ps->objects[i] = object; + } + + ps->objects[5]->current_position.x += ps->objects[3]->current_position.x; + return ps; } CelestialObject *planetary_system_get_star(PlanetarySystem *planetary_system) { return planetary_system->objects[0]; } -Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system, uint32_t object_index) { +Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system, int32_t object_index) { Vector2 a = vector2_create_zero(); - for (uint32_t i = 0; i < planetary_system->objects_length; i += 1) { + for (int32_t i = 0; i < planetary_system->objects_length; i += 1) { if (i == object_index) continue; Vector2 r = vector2_substract(planetary_system->objects[i]->current_position, planetary_system->objects[object_index]->current_position); @@ -89,7 +77,7 @@ Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system, } void planetary_system_update(PlanetarySystem *planetary_system, double interval) { - for (uint32_t i = 1; i < planetary_system->objects_length; i += 1) { + for (int32_t i = 1; i < planetary_system->objects_length; i += 1) { CelestialObject *object = planetary_system->objects[i]; Vector2 current_position = object->current_position; Vector2 new_position; @@ -137,11 +125,16 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval) } } -void planetary_system_draw(PlanetarySystem *planetary_system) { - Vector2 reference_frame = planetary_system->objects[planetary_system->reference_frame_object_index]->current_position; +void planetary_system_draw(PlanetarySystem *ps) { + CelestialObject *object_reference_frame = ps->objects[ps->reference_frame_object_index]; + Vector2 reference_frame = object_reference_frame->current_position; - for (uint32_t i = 0; i < planetary_system->objects_length; i += 1) { - CelestialObject *object = planetary_system->objects[i]; - draw_celestial_object(object, reference_frame, planetary_system->zoom_factor); + for (int32_t i = 0; i < ps->objects_length; i += 1) { + CelestialObject *object = ps->objects[i]; + celestial_object_draw(object, reference_frame, ps->zoom_factor); } + + char text[200]; + sprintf(text, "Focused Object : %s", object_reference_frame->name); + draw_text(text, vector2_create(8, 64)); } diff --git a/PlanetarySystem.h b/PlanetarySystem.h index 9617542b1d05a56d09ff6e173da685bb973ba052..4a5effdde4945e387f9246b9d11bfbccba4b4385 100644 --- a/PlanetarySystem.h +++ b/PlanetarySystem.h @@ -9,7 +9,7 @@ const uint32_t SCREEN_WIDTH; const uint32_t SCREEN_HEIGHT; typedef struct PlanetarySystem { - uint32_t objects_length; + int32_t objects_length; CelestialObject **objects; double interval; double zoom_factor; diff --git a/drawing.c b/drawing.c index a8a799b6a5594676d729351b987085443e933a07..b8eefeb65e5607fc22224d1b38d08a6d6781ea07 100644 --- a/drawing.c +++ b/drawing.c @@ -28,7 +28,7 @@ void draw_line(Vector2 a, Vector2 b) { void draw_text(char *text, Vector2 position) { glColor3f(1.0, 1.0, 1.0); - glRasterPos2f(position.x + 8, position.y + 32); + glRasterPos2f(position.x, position.y); for (int32_t i = 0; i < (int32_t)strlen(text); i++) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]); @@ -39,7 +39,7 @@ Vector2 scale_position(double zoom_factor, Vector2 object_position, Vector2 refe Vector2 unscaled_position = vector2_substract(object_position, reference_frame_position); unscaled_position = vector2_multiply(unscaled_position, zoom_factor); - Vector2 scaled_position = vector2_multiply(unscaled_position, 1.0 / (300 * 1E9)); + Vector2 scaled_position = vector2_multiply(unscaled_position, 1.0 / (280 * 1E9)); scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT); return scaled_position; }