/* body3.c three body gravitational attraction */ /* F = G m1 m2 / d^2 F, A, V, S have x,y,z components */ /* F = m2 v^2 / d d^2 = (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 */ /* F = m A acceleration along force components */ /* V = V + A dt velocity update at delta time dt */ /* S = S + V dt position update at delta time dt */ #include #include #include #include #include /* define number of masses NM, add initial values */ #define NM 3 static GLfloat theta[] = {0.0,0.0,0.0}; /* display angle */ GLUquadricObj *obj; typedef struct { double x; double y; double z; } xyz; /* subscript is mass index */ static xyz S[NM] = {{0.0,0.0,0.0}, {1.0,0.0,0.0}, {3.0,0.0,0.0}}; static xyz V[NM]; /* initialized to orbit */ static xyz A[NM]; /* computed */ static xyz F[NM]; /* computed */ static GLfloat R[NM] = {0.125, 0.0625, 0.03125}; /* radius of mass */ static double M[NM] = {1.0, 0.125, 0.00625}; /* normalized mass */ static GLfloat color[NM][3] = {{1.0,0.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}}; static double G = 2.0; /* normalized gravitational constant */ static double dt = 0.001; static debug = 0; static void printstring(void *font, char *string) { int len, i; len = (int) strlen(string); for (i = 0; i < len; i++) glutBitmapCharacter(font, string[i]); } /* end printstring */ static void display(void) { int i; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); for(i=0; i