#include #include #include #include #include #include #include #include #define DEBUGlog(f, args...) \ fprintf(stderr, "in %s: "f, __FUNCTION__, ## args) #define W_HEIGHT 480 #define W_WIDTH 640 /* N body problem */ /* parameters */ static int NUM_BODY = 3; //static float Gravitation = 6.67e-11 ; //static float delta = 100; //static float FIELD = 2e11; static float Gravitation = 100.0f; // ? static float delta = 0.05f; // 0.01 ~ 100 ? static float FIELD = 400.0f; // -100 ~ 100 static const float eps = 15.0f; /* for OpenGL Utility. */ GLUquadricObj **sphere; typedef struct { /* star's parameter. */ float weight; float a[3]; /* acceleration */ float v[3]; /* velocity */ float r[3]; /* position */ /* for SDL. */ //SDL_Rect rect; } body; body *stars_old, *stars_new; void start(void); __code initialize(int num); __code randomInit(SDL_Surface *screen, int num); __code starsInit(SDL_Surface *screen, int num); __code loop(int count, SDL_Surface *screen, int num); __code compute(int count, SDL_Surface *screen, int num); __code nextTurn(int count, SDL_Surface *screen, int num); __code moveCenter(int count, SDL_Surface *screen, int num); __code CenteringVelocity(int count, SDL_Surface *screen, int num); __code drawStars(int count, SDL_Surface *screen, int num); int main(int argc, char **argv) { int ch; while ((ch = getopt(argc, argv, "s:g:")) != -1) { switch (ch) { case 's': delta = atof(optarg); break; case 'g': Gravitation = atof(optarg); break; case '?': default: break; } } start(); return 0; } void start() { goto initialize(NUM_BODY); } __code finish(int ret, int num) { int i; DEBUGlog("Gravity = %e\n", Gravitation); DEBUGlog("FLT_MAX = %e\n", FLT_MAX); DEBUGlog("FLT_MAX_EXP = %d\n", FLT_MAX_EXP); DEBUGlog("FLT_MIN = %e\n", FLT_MIN); DEBUGlog("FLT_MIN_EXP = %d\n", FLT_MIN_EXP); DEBUGlog("FLT_EPSILON = %e\n", FLT_EPSILON); free(stars_old); free(stars_new); for( i=0; iw,screen->h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( 60.0, (float)screen->w/(float)screen->h, 1.0, 1000.0); gluLookAt( 300.0,300.0,300.0, 0.0,0.0,0.0, 1.0,0.0,0.0); glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_MODELVIEW); //DEBUGlog("scr->w=%d\n", screen->w); //DEBUGlog("scr->h=%d\n", screen->h); for( i=0; iw/(float)screen->h, 1.0, v); gluLookAt( v,v,v, 0.0,0.0,0.0, 1.0,0.0,0.0); glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_MODELVIEW); goto loop(0, screen, num); } __code CenteringVelocity(int count, SDL_Surface *screen, int num) { int i; float v0,v1,v2; v0=v1=v2=0.0; for (i=0; i dx/dt = v * dv = a*dt => dv/dt = a * a = F/m; * F = F1 + F2 + ... + Fn * Fi = G m1m2/r^2 * */ __code compute(int count, SDL_Surface *screen, int num) { int i; /* a is accel this planet receive now. */ stars_old[count].a[0]=stars_old[count].a[1]=stars_old[count].a[2]=0.0f; DEBUGlog("count=%d\n", count); for (i=0; i