diff --git a/Makefile b/Makefile index 228efce7defc6232e5058eaa2814bcf653500d7f..80c1c832f96bfad4726beb81774f30b53cd6aef5 100755 --- a/Makefile +++ b/Makefile @@ -7,22 +7,23 @@ LDFLAGS:=-lm -lSDL2 #Path to the lib Vec2 VPATH:=vec2 gfx planet +HDR:=gfx/gfx.h planet/constants.h planet/planet.h vec2/vec2.h main: main.o vec2.o gfx.o planet.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -run_tests: tests +run_tests: vec2_tests ./$< -tests: vec_tests.o vec2.o +vec2_tests: vec_tests.o vec2.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) -planet.o: planet.h +planet.o: ${HDR} -vec2.o: vec2.h +vec2.o: ${HDR} -gfx.o: gfx.h +gfx.o: ${HDR} clean: rm -f *.o main tests diff --git a/main.c b/main.c index 3022f4ad445f9b41ebd38693e20a5112bf6dc1f7..b7e82e2ee7e34d9c74ceaab298a9b7cbe1e92c63 100755 --- a/main.c +++ b/main.c @@ -28,17 +28,17 @@ int main() { gfx_present(ctxt); gfx_clear(ctxt, COLOR_BLACK); // begin : draw the current state of your system - show_system(ctxt, sys); + show_system(ctxt, &sys); // end : draw state of system // begin : update your system - update_system(sys, DELTA_T); + update_system(&sys, DELTA_T); usleep(100000); // end : update system if (gfx_keypressed() == SDLK_ESCAPE) { break; } } // begin : Free your system - free_system(sys); + free_system(&sys); // end : free system gfx_destroy(ctxt); return EXIT_SUCCESS; diff --git a/planet/planet.c b/planet/planet.c index a3f1f8e7536a18d5e07f132fdf67214dcb2c74df..b10f6f70800bd9e91d65fc060cc99e5b8ce66989 100755 --- a/planet/planet.c +++ b/planet/planet.c @@ -3,6 +3,8 @@ #include "constants.h" +#define NB_PLANETS 4 + planet_t create_planet(double mass, vec2 pos) { planet_t p; @@ -18,23 +20,22 @@ system_t create_system(double delta_t) { system.star = create_planet(M_SOLEIL, vec2_create_zero()); system.nb_planets = 4; system.planets = malloc(system.nb_planets * sizeof(planet_t)); - if (NULL == system.planets) return NULL; - double masses[system.nb_planets] = { + double masses[NB_PLANETS] = { M_MERCURE, M_VENUS, M_TERRE, M_MARS}; - vec2 positions[system.nb_planets] = { + vec2 positions[NB_PLANETS] = { vec2_create(PER_MERCURE, 0.0), vec2_create(PER_VENUS, 0.0), vec2_create(PER_TERRE, 0.0), vec2_create(PER_MARS, 0.0)}; - double eccentricities[system.nb_planets] = { + double eccentricities[NB_PLANETS] = { E_MERCURE, E_VENUS, E_TERRE, E_MARS}; - double axis_major[system.nb_planets] = { + double axis_major[NB_PLANETS] = { A_MERCURE, A_VENUS, A_TERRE, @@ -47,21 +48,21 @@ system_t create_system(double delta_t) { vel = sqrt(vel); const vec2 ru = vec2_normalize(vec2_sub(system.planets[i].pos, system.star.pos)); const vec2 rp = vec2_create(-ru.y, ru.x); - const vec2 v = vec2_mul(vel, r); + const vec2 v = vec2_mul(vel, rp); system.planets[i].prec_pos = vec2_sub(system.planets[i].pos, vec2_mul(delta_t, v)); } return system; } -void show_system(struct gfx_context_t* ctxt, system_t* system) { +void show_system(struct gfx_context_t* ctxt, system_t* system, const uint32_t width, const uint32_t height) { // draw sun - const coordinates xy_sun = vec2_to_coordinates_centered(vec2_normalize(system->star.pos)); + const coordinates xy_sun = vec2_to_coordinates_centered(vec2_normalize(system->star.pos), width, height); draw_full_circle(ctxt, xy_sun.column, xy_sun.row, 50, COLOR_YELLOW); // draw planets - for (uint32_t i = 0; i < sys.nb_planets; ++i) { + for (uint32_t i = 0; i < system->nb_planets; ++i) { const coordinates xy = vec2_to_coordinates_centered( - vec2_normalize(sys.planets[i].pos), + vec2_normalize(system->planets[i].pos), ctxt->width, ctxt->height); draw_full_circle(ctxt, xy.column, xy.row, 20, COLOR_BLUE); @@ -72,6 +73,7 @@ void show_system(struct gfx_context_t* ctxt, system_t* system) { void update_system(system_t* system, double delta_t) { for (uint32_t i = 0; i < system->nb_planets; ++i) { // apply the effects of the sun + const double r_star_2 = vec2_norm_sqr(vec2_sub(system->planets[i].pos, system->star.pos)); vec2 a = vec2_mul((G * system->star.mass) / r_star_2, vec2_normalize(system->planets[i].pos)); // apply the effects of all other planets for (uint32_t k = 0; k < system->nb_planets; ++k) { diff --git a/planet/planet.h b/planet/planet.h index cedbe0647134240e6fa0aedf21bc7705110d178d..8db31c9d78cb9b6e17f05f93f414566849ea5e57 100755 --- a/planet/planet.h +++ b/planet/planet.h @@ -4,26 +4,29 @@ #include "../vec2/vec2.h" #include "../gfx/gfx.h" -typedef struct _planet -{ +typedef struct _planet { double mass; vec2 pos; // x(t) vec2 prec_pos; // x(t - dt) } planet_t; -typedef struct _system -{ +typedef struct _system { planet_t star; // ex. The sun uint32_t nb_planets; // The number of orbiting planets - planet_t *planets; // An array of orbiting planets + planet_t* planets; // An array of orbiting planets } system_t; // These functions are not mandatory to implement, // it's rather a hint of what you should have. planet_t create_planet(double mass, vec2 pos); + system_t create_system(double delta_t); -void show_system(struct gfx_context_t *ctxt, system_t *system); -void update_system(system_t *system, double delta_t); -void free_system(system_t *system); + +//void show_system(struct gfx_context_t *ctxt, system_t *system); +void show_system(struct gfx_context_t* ctxt, system_t* system, const uint32_t width, const uint32_t height); + +void update_system(system_t* system, double delta_t); + +void free_system(system_t* system); #endif