Skip to content
Snippets Groups Projects
Commit 814a2ed6 authored by BobLeHibou's avatar BobLeHibou
Browse files

EDIT: compiles but system is not centered

parent dab469d6
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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;
......
......@@ -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) {
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment