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

Refactoring data

parent 030e2065
Branches
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ ...@@ -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 *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)); CelestialObject *object = (CelestialObject *)malloc(sizeof(CelestialObject));
object->name = name; strcpy(object->name, name);
object->mass = mass; object->mass = mass;
object->previous_position = vector2_create_zero(); object->previous_position = vector2_create_zero();
...@@ -29,7 +29,7 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_ma ...@@ -29,7 +29,7 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_ma
return object; 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); Vector2 scaled_position = scale_position(zoom_factor, object->current_position, reference_frame);
uint32_t color = object->drawing_color; uint32_t color = object->drawing_color;
...@@ -54,5 +54,5 @@ void draw_celestial_object(CelestialObject *object, Vector2 reference_frame, dou ...@@ -54,5 +54,5 @@ void draw_celestial_object(CelestialObject *object, Vector2 reference_frame, dou
return; return;
} }
draw_text(object->name, scaled_position); draw_text(object->name, vector2_add(scaled_position, vector2_create(8, 32)));
} }
...@@ -13,10 +13,10 @@ typedef struct CelestialObject { ...@@ -13,10 +13,10 @@ typedef struct CelestialObject {
uint32_t drawing_color; uint32_t drawing_color;
Vector2 *points; Vector2 *points;
int32_t points_length; int32_t points_length;
char *name; char name[100];
} CelestialObject; } CelestialObject;
CelestialObject *celestial_object_create(char *name, 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, 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 #endif
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <GL/glut.h> #include <GL/glut.h>
#include <math.h> #include <math.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -34,50 +35,37 @@ const double EARTH_SEMI_MAJOR_AXIS = 149.598262 * 1E9; ...@@ -34,50 +35,37 @@ const double EARTH_SEMI_MAJOR_AXIS = 149.598262 * 1E9;
const double MARS_SEMI_MAJOR_AXIS = 227.943824 * 1E9; const double MARS_SEMI_MAJOR_AXIS = 227.943824 * 1E9;
PlanetarySystem *planetary_system_create() { PlanetarySystem *planetary_system_create() {
PlanetarySystem *planetary_system = (PlanetarySystem *)malloc(sizeof(PlanetarySystem)); PlanetarySystem *ps = (PlanetarySystem *)malloc(sizeof(PlanetarySystem));
planetary_system->objects_length = 9; ps->objects_length = 9;
planetary_system->objects = (CelestialObject **)malloc(sizeof(PlanetarySystem *) * planetary_system->objects_length); ps->objects = (CelestialObject **)malloc(sizeof(PlanetarySystem *) * ps->objects_length);
planetary_system->zoom_factor = 1; ps->zoom_factor = 1;
planetary_system->reference_frame_object_index = 0; ps->reference_frame_object_index = 0;
char names[][100] = {"", "Mercury", "Venus", "Earth", "Mars", "Moon", "Naboo", "Jupitaire", "Endor"}; char names[][100] = {"Sun", "Mercury", "Venus", "Earth", "Mars", "Moon", "Naboo", "Jupitaire", "Endor"};
double masses[] = {MERCURY_MASS, VENUS_MASS, EARTH_MASS, MARS_MASS, 0, 0, 0, 0}; 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};
// Sun double eccentricities[] = {0, MERCURY_ECCENTRICITY, VENUS_ECCENTRICITY, EARTH_ECCENTRICITY, MARS_ECCENTRICITY, 0.0549, 0.8, 0.04839266, 0.34};
planetary_system->objects[0] = celestial_object_create("", SUN_MASS, 0, 0, 50, 0xFFFFFF); double disc_radiuses[] = {50, 10, 20, 10, 12, 5, 12, 12, 12};
// Mercury int32_t colors[] = {0xFFFFFF, 0xDBCECA, 0x8B7D82, 0x6b93d6, 0xBC2732, 0x7D7B67, 0x005500, 0x005500, 0x005500};
planetary_system->objects[1] = celestial_object_create("Mercury", MERCURY_MASS, MERCURY_SEMI_MAJOR_AXIS, MERCURY_ECCENTRICITY, 10, 0xDBCECA);
// Venus for (int32_t i = 0; i < ps->objects_length; i += 1) {
planetary_system->objects[2] = celestial_object_create("Venus", VENUS_MASS, VENUS_SEMI_MAJOR_AXIS, VENUS_ECCENTRICITY, 20, 0x8B7D82); CelestialObject *object = celestial_object_create(names[i], masses[i], semi_major_axes[i], eccentricities[i], disc_radiuses[i], colors[i]);
// Earth ps->objects[i] = object;
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); ps->objects[5]->current_position.x += ps->objects[3]->current_position.x;
return ps;
// 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;
} }
CelestialObject *planetary_system_get_star(PlanetarySystem *planetary_system) { CelestialObject *planetary_system_get_star(PlanetarySystem *planetary_system) {
return planetary_system->objects[0]; 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(); 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) if (i == object_index)
continue; continue;
Vector2 r = vector2_substract(planetary_system->objects[i]->current_position, planetary_system->objects[object_index]->current_position); 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, ...@@ -89,7 +77,7 @@ Vector2 calculate_gravitational_acceleration(PlanetarySystem *planetary_system,
} }
void planetary_system_update(PlanetarySystem *planetary_system, double interval) { 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]; CelestialObject *object = planetary_system->objects[i];
Vector2 current_position = object->current_position; Vector2 current_position = object->current_position;
Vector2 new_position; Vector2 new_position;
...@@ -137,11 +125,16 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval) ...@@ -137,11 +125,16 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval)
} }
} }
void planetary_system_draw(PlanetarySystem *planetary_system) { void planetary_system_draw(PlanetarySystem *ps) {
Vector2 reference_frame = planetary_system->objects[planetary_system->reference_frame_object_index]->current_position; 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) { for (int32_t i = 0; i < ps->objects_length; i += 1) {
CelestialObject *object = planetary_system->objects[i]; CelestialObject *object = ps->objects[i];
draw_celestial_object(object, reference_frame, planetary_system->zoom_factor); 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));
} }
...@@ -9,7 +9,7 @@ const uint32_t SCREEN_WIDTH; ...@@ -9,7 +9,7 @@ const uint32_t SCREEN_WIDTH;
const uint32_t SCREEN_HEIGHT; const uint32_t SCREEN_HEIGHT;
typedef struct PlanetarySystem { typedef struct PlanetarySystem {
uint32_t objects_length; int32_t objects_length;
CelestialObject **objects; CelestialObject **objects;
double interval; double interval;
double zoom_factor; double zoom_factor;
......
...@@ -28,7 +28,7 @@ void draw_line(Vector2 a, Vector2 b) { ...@@ -28,7 +28,7 @@ void draw_line(Vector2 a, Vector2 b) {
void draw_text(char *text, Vector2 position) { void draw_text(char *text, Vector2 position) {
glColor3f(1.0, 1.0, 1.0); 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++) { for (int32_t i = 0; i < (int32_t)strlen(text); i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]);
...@@ -39,7 +39,7 @@ Vector2 scale_position(double zoom_factor, Vector2 object_position, Vector2 refe ...@@ -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); Vector2 unscaled_position = vector2_substract(object_position, reference_frame_position);
unscaled_position = vector2_multiply(unscaled_position, zoom_factor); 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); scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT);
return scaled_position; return scaled_position;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment