Skip to content
Snippets Groups Projects
Commit e506aa6d authored by Boris Stefanovic's avatar Boris Stefanovic
Browse files

ADD: update_system

parent d840a2e7
No related branches found
No related tags found
No related merge requests found
......@@ -8,20 +8,16 @@
#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 1000
// arbitraire, en secondes ?
#define DELTA_T 0.125
int main() {
srand(time(NULL));
struct gfx_context_t *ctxt =
gfx_create("Planetary system", SCREEN_WIDTH, SCREEN_HEIGHT);
struct gfx_context_t *ctxt = gfx_create("Planetary system", SCREEN_WIDTH, SCREEN_HEIGHT);
if (!ctxt) {
fprintf(stderr, "Graphics initialization failed!\n");
return EXIT_FAILURE;
}
// TODO : create your system
system_t sys = create_system(DELTA_T);
system_t sys = create_system(0.125);
// end : create system
while (true) {
gfx_present(ctxt);
......
......@@ -5,28 +5,29 @@
#define G 6.67e-11
#define M_SOLEIL 1.989e30
// source: CRM
// mass
#define M_TERRE 5.9742e24
// source: wiki
#define M_MERCURE 3.3011e23
#define M_VENUS 4.8675e24
#define M_MARS 6.4171e23
// major axis
#define A_MERCURE 5.7909e10
#define A_VENUS 1.0821e11
#define A_TERRE 1.4960e11
#define A_MARS 2.2794e11
// excentricity
#define E_MERCURE 0.205630
#define E_VENUS 0.006772
#define E_TERRE 0.0167086
#define E_MARS 0.0934
//TODO: perihelion
#define PER_MERCURE
#define PER_VENUS
#define PER_TERRE
#define PER_MARS
// perihelion
#define PER_MERCURE 4.60012e10
#define PER_VENUS 1.07477e11
#define PER_TERRE 1.47095e11
#define PER_MARS 2.06700e11
#endif //PLANET_CONSTANTS_H
......@@ -47,7 +47,28 @@ 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 update_system(system_t *system, double delta_t) {
for (uint32_t i = 0; i < system->nb_planets; ++i) {
// apply the effects of the sun
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) {
if (k == i) continue;
const vec2 r_ik = vec2_sub(system->planets[k].pos, system->planets[i].pos);
const vec2 u = vec2_normalize(r_ik); // unit vector from planet i to planet k
// force already divided by system->planets[i].mass to get acceleration
const double a_norm = (G * system->planets[k].mass) / vec2_norm_sqr(r_ik);
const vec2 a_k = vec2_mul(a_norm, u);
a = vec2_add(a, a_k);
}
// update position
vec2 next_pos = vec2_mul(2.0, system->planets[i].pos);
next_pos = vec2_sub(next_pos, system->planets[i].prec_pos);
next_pos = vec2_add(next_pos, vec2_mul(delta_t * delta_t, a));
system->planets[i].prec_pos = system->planets[i].pos;
system->planets[i].pos = next_pos;
}
}
void free_system(system_t *system);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment