Hello. I am trying to program the movement of the planets of the solar system around the sun (n-body task). But I ran into a problem - the Planets fly in a circle of the sun (I have a fixed point) as they should, but as soon as I add the moon, it does not circle around the Earth, or spin around the sun. If I do the interaction of all the bodies with each other, then they fly away from me. Please tell me where I could be wrong.

#define _USE_MATH_DEFINES #include <iostream> #include <vector> #include "GL/glut.h" #include "conio.h" #include "cmath" using namespace std; double MassSun = 1.99*pow(10, 30); double MassEarth = 5.98*pow(10, 24); double MassMoon = 7.32*pow(10, 22); double EccentrisityEarth = 0.01671022; double AstroUnit =1; double LunarDistance =1.00257; const int N =8; struct Particle { double x; double y; double z; double vx; double vy; double vz; double m; double ax; double ay; double az; vector<double>tx, ty,tz; }; Particle p[N]; double dt = 0.001; double c = 0; int t = 1; void display() { glClear(GL_COLOR_BUFFER_BIT); glPointSize(1); glBegin(GL_POINTS); int scale =250; for (int i = 0; i < N; i++) { if (i == 0) glColor3d(1, 0, 0); if (i == 1) glColor3d(0, 1, 0); if (i == 2) glColor3d(1, 1, 1); if (i == 3) glColor3d(1, 1, 0); if (i == 4) glColor3d(1, 0, 1); if (i == 5) glColor3d(0,1, 1); if (i == 6) glColor3d(0.5, 0, 0); if (i == 7) glColor3d(0, 0.5, 0); if (i == 8) glColor3d(0, 0, 0.5); if (i == 9) glColor3d(0.5, 0.5, 0.); for (int j = 0; j<p[i].tx.size(); j++) glVertex3d( p[i].tx[j] * scale, p[i].ty[j]* scale,p[i].tz[j]*scale); cout << p[2].x << " " << p[2].y << endl; } glEnd(); glutSwapBuffers(); } double Gr; double k = 0; void timer(int = 0) { Gr = (4 * M_PI*M_PI); for (int i = 1; i < N; i++) { p[i].ax = p[i].ay = p[i].az = 0; for (int j = 0; j < N; j++) if (i != j) { double r = pow((pow(p[i].x - p[j].x, 2.0) + pow(p[i].y - p[j].y, 2.0) + pow(p[i].z - p[j].z, 2.0)), 3.0 / 2.0); p[i].ax += -Gr*p[j].m*(p[i].x - p[j].x) / (r*p[0].m); p[i].ay += -Gr*p[j].m*(p[i].y - p[j].y) / (r*p[0].m); p[i].az += -Gr*p[j].m*(p[i].z - p[j].z) / (r*p[0].m); } } for (int i = 0; i < N; i++) { p[i].vx = p[i].vx + dt*p[i].ax; p[i].vy = p[i].vy + dt*p[i].ay; p[i].vz = p[i].vz + dt*p[i].az; p[i].x = p[i].x + dt*p[i].vx; p[i].y = p[i].y + dt*p[i].vy; p[i].z = p[i].z + dt*p[i].vz; p[i].tx.push_back(p[i].x); p[i].ty.push_back(p[i].y); p[i].tz.push_back(p[i].z); } display(); glutTimerFunc(1, timer, 0); } int main(int argc, char **argv) { /*p[0].x = 0; p[0].y = 0; p[0].z = 0; p[0].vx = 0; p[0].vy = 0; p[0].vz = 0; p[0].m = MassSun; p[0].ax = 0; p[0].ay = 0; p[0].az = 0; p[1].x = AstroUnit; p[1].y = 0; p[1].z = 0; p[1].vx = 0; p[1].vy = 2 * M_PI*AstroUnit; p[1].vz = 0; p[1].m = MassEarth; p[1].ax = 0; p[1].ay = 0; p[1].az = 0; p[2].x = LunarDistance; p[2].y = 0; p[2].z = 0; p[2].vx = 0; p[2].vy = 2 * M_PI*LunarDistance; p[2].vz = 0; p[2].m = 0.073483e24; p[2].ax = 0; p[2].ay = 0; p[2].az = 0; */ p[0].x = 0; p[0].y = 0; p[0].z = 0; p[0].vx = 0; p[0].vy =0; p[0].vz = 00; p[0].m = MassSun ; p[0].ax = 0; p[0].ay = 0; p[0].az = 0; p[1].x = 0.38710; p[1].y = 0; p[1].z = 0; p[1].vx = 0; p[1].vy =2*M_PI* 0.38710/ 0.24085; p[1].vz = 0; p[1].m = 0.33022e24; p[1].ax = 0; p[1].ay = 0; p[1].az = 0; p[2].x = 0.72333; p[2].y = 0; p[2].z = 0; p[2].vx = 0; p[2].vy = 2*M_PI*0.72333/0.61521; p[2].vz = 0; p[2].m = 4.8690e24; p[2].ax = 0; p[2].ay = 0; p[2].az = 0; p[3].x = AstroUnit; p[3].y = 0; p[3].z = 0; p[3].vx = 0; p[3].vy = 2 * M_PI*AstroUnit; p[3].vz = 0; p[3].m = MassEarth; p[3].ax = 0; p[3].ay = 0; p[3].az = 0; p[4].x = 1.52363; p[4].y = 0; p[4].z = 0; p[4].vx = 0; p[4].vy =2*M_PI* 1.52363/1.88078; p[4].vz = 0; p[4].m = 0.64191e24; p[4].ax = 0; p[4].ay = 0; p[4].az = 0; p[5].x = 5.20441; p[5].y = 0; p[5].z = 0; p[5].vx = 0; p[5].vy = 2*M_PI* 5.20441/11.8677; p[5].vz = 0; p[5].m = 1898.8e24; p[5].ax = 0; p[5].ay = 0; p[5].az = 0; p[6].x = 9.58378; p[6].y = 0; p[6].z = 0; p[6].vx = 0; p[6].vy =2*M_PI*9.58378/29.6661; p[6].vz = 0; p[6].m = 568.50e24; p[6].ax = 0; p[6].ay = 0; p[6].az = 0; p[7].x = 19.18722; p[7].y = 0; p[7].z = 0; p[7].vx = 0; p[7].vy = 2 * M_PI*19.18722 / 84.048; p[7].vz = 0; p[7].m = 86.625e24; p[7].ax = 0; p[7].ay = 0; p[7].az = 0; p[8].x = 30.02090; p[8].y = 0; p[8].z = 0; p[8].vx = 0; p[8].vy = 2 * M_PI*30.02090 / 164.491; p[8].vz = 0; p[8].m = 102.78; p[8].ax = 0; p[8].ay = 0; p[8].az = 0; p[9].x = 39.23107; p[9].y = 0; p[9].z = 0; p[9].vx = 0; p[9].vy = 2 * M_PI*39.23107 / 245.73; p[9].vz = 0; p[9].m = 0.015; p[9].ax = 0; p[9].ay = 0; p[9].az = 0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("Avada Kedabra"); glEnable(GL_POINT_SMOOTH);; glClearColor(0, 0, 0, 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10000,10000, -10000, 10000, -4000, 4000); glutDisplayFunc(display); timer(0); glutMainLoop(); } 

Closed due to the fact that the issue is too common for Kromster participants, aleksandr barakin , Harry , Alex , user194374 6 Dec '16 at 7:46 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Specify the problem, in the calculations, or in the drawing? And according to this, remove the extra code and label from the question. - Kromster
  • Judging by the description, the problem is in the calculations. Maybe you do not have a massive planet? - VladD
  • @VladD doesn’t seem to be the same if it flies around the sun normally (about formulas), but to turn the planets is not an option, since the task is worth it - use real weights - Rams
  • Well, if you mix in the masses and distances from each other, it will not fly there. Or the initial speed may not be the same. - VladD
  • Gr is a gravitational constant? A strange value ... - avp

2 answers 2

Try this ...

  • Step 1) Draw all the planets with the "center of the world" in the sun.

  • Step 2) Move the planets relative to the sun (if needed)

  • 3 step) Transfer the "center of the world" to the earth to draw the moon relatively
    land.

The whole secret is offset from the object.

  • Attempts were. I even tied the moon to the ground separately, then I connected the sun, it either scatters or revolves around the sun. I have already broken my head. What can already be broken ? - Rams
  • The efficiency of a program that models the behavior of such bodies cannot depend in any way on the choice of the “center of the world”. Therefore, it is not clear what kind of "secret" you are talking about. - AnT
  • @AnT Then it turns out, most likely a problem in the code, if you talk like that? - Rams

I would also normalize the problem, bringing the physical quantities to imputed dimensionless (not all 25th degrees). And I would try to set the correct initial conditions.

  • I tried, but the task was set in using real masses. It turns out that if I separately consider the sun-earth, the earth-moon, then they move well and the moon revolves around the earth, but as soon as I add the influence of the sun (in the second case), it returns back home. Those. again the moon becomes a "planet." and if you connect everything with everyone, it will fly apart. - Rams
  • So the step should be reduced. Or try even the movement of the moon count with a different (smaller) step. Are you sure about the initial distance to the moon? (1 AU is approximately 150000000km, i.e. you have 150188550 to the Moon or only 188550km behind the Earth, but in fact from L to Z db around 380000km) - avp
  • @avp Oh, I'm sorry, there was a typo, the unit is superfluous (corrected), although I’m making calculations from 1.00257. But on the subject of the step I will try now. - Rams