icfp12/ovm/physics.h

66 lines
1.7 KiB
C

#ifndef OVM_PHYSICS_H
#define OVM_PHYSICS_H
#include "base.h"
#include <math.h>
#define G 6.67428e-11
#define Me 6.0e24
#define Re 6.357e6
typedef struct {
gdouble x, y;
} vector_t;
typedef struct {
vector_t g, v;
gdouble v_abs;
} move_data_t;
typedef struct {
vector_t pos_old, pos;
gdouble rad2_old, rad_old, rad2, rad;
vector_t dv_old, dv;
gboolean b_old, b; /* whether the following was calculated */
move_data_t move_old, move;
} satellite_t;
static inline void satellite_update_pos(satellite_t *s, gdouble x, gdouble y) {
s->pos_old = s->pos;
s->rad2_old = s->rad2;
s->rad_old = s->rad;
s->dv_old = s->dv;
s->b_old = s->b;
s->move_old = s->move;
s->pos.x = x; s->pos.y = y;
s->rad2 = s->pos.x*s->pos.x + s->pos.y*s->pos.y;
s->rad = sqrt(s->rad2);
s->dv.x = 0.0; s->dv.y = 0.0;
s->b = FALSE;
}
static inline void satellite_update_move(satellite_t *s) {
if (!s->b_old) {
s->move_old.g.x = -(G*Me/(s->rad2_old*s->rad_old)) * s->pos_old.x;
s->move_old.g.y = -(G*Me/(s->rad2_old*s->rad_old)) * s->pos_old.y;
s->move_old.v.x = s->pos.x - s->pos_old.x - 0.5*(s->move_old.g.x + s->dv_old.x);
s->move_old.v.y = s->pos.y - s->pos_old.y - 0.5*(s->move_old.g.y + s->dv_old.y);
s->move_old.v_abs = sqrt(s->move_old.v.x*s->move_old.v.x+s->move_old.v.y*s->move_old.v.y);
s->b_old = TRUE;
}
if (!s->b) {
s->move.g.x = -(G*Me/(s->rad2*s->rad)) * s->pos.x;
s->move.g.y = -(G*Me/(s->rad2*s->rad)) * s->pos.y;
s->move.v.x = s->move_old.v.x + s->dv_old.x + 0.5*(s->move_old.g.x + s->move.g.x);
s->move.v.y = s->move_old.v.y + s->dv_old.y + 0.5*(s->move_old.g.y + s->move.g.y);
s->move.v_abs = sqrt(s->move.v.x*s->move.v.x+s->move.v.y*s->move.v.y);
s->b = TRUE;
}
}
#endif