I'm trying to code a solar system simulation in Java (using real world values). First of all I made a planet that rotates around the sun by using Newton's gravitational attraction formula and a starting velocity that determinates the orbit time. The code works like this: I have a list of planets (assume the sun is a planet) and on every frame I cycle trough the list and I apply the gravitational force for each planet based on the total force from all of the other ones. Each planet has a velocity vector and a position vector in a 2D space and the 2 functions that do the most work look like this:
private Point2D getAttractionForce(Planet planet){
double distanceX = planet.x-this.x;
double distanceY = planet.y-this.y;
double distance = Math.sqrt(Math.pow(distanceX, 2)+Math.pow(distanceY, 2));
double force = SolarSystem.G*this.mass*planet.mass/Math.pow(distance, 2);
double angle = Math.atan2(distanceY, distanceX);
double forceX = force * Math.cos(angle);
double forceY = force * Math.sin(angle);
return new Point2D(forceX, forceY);
}
public void updatePosition(List<Planet> planets){
double totalForceX = 0;
double totalForceY = 0;
for (Planet planet : planets){
if (planet == this) continue;
Point2D force = getAttractionForce(planet);
totalForceX += force.getX();
totalForceY += force.getY();
}
}
this.xVelocity += totalForceX/this.mass*TIMESTEP;
this.yVelocity += totalForceY/this.mass*TIMESTEP;
this.x += this.xVelocity*TIMESTEP;
this.y += this.yVelocity*TIMESTEP;
}
My problem is that when I add a moon nearby the earth:
Planet moon = new Planet(Color.GRAY, 4, 7.35e22, -(1.50e11+4e8), 0, 0, 1022);
The moon gets attracted to the sun and does not rotate around the earth as it should do. For debugging purposes I have calculated these values:
Distance moon-sun: 1.504E11
Sun force for moon: 4.313523549126089E20
Distance moon-earth: 4.0E8
Earth force for moon: 1.831019119125E20
The attraction force moon-sun that I get is 4 times bigger that the moon-earth one. I tried changing the initial velocity of the moon but all I get is the moon rotating around the sun. What am I doing wrong?
Here is the complete code (JavaFX needed): https://pastebin.com/fiU4m7Te
Any help is appreciated, thanks
Seen from the sun, both earth and moon travel at almost the same speed in almost the same direction. It makes sense for the sun to exert a strong force on the moon, but that force should mostly cause the moon to move along with earth around the sun, since both experience almost the same acceleration.
It's only when you look closer that the separate movement becomes apparent. There you have the weaker attraction to the earth, and you have also slight differences in the acceleration towards the sun due to the slightly different position.
Your are also dealing with very different speeds in the earth-moon system. If your simulation operates in discrete time steps, that may cause serious problems because it could cause the position of the earth from one point in time to be very different from the position the next point in time.
If you know which moons orbit around which planets (as in you don't need to model what would happen if one rogue planet would capture a moon and pull it away) then maybe you can get better results by focusing on differences only. Assume that for the largest part the moon moves like the planet does. Then compute the difference in acceleration, resulting from the attraction from the planet and the difference in position to the sun, and use that difference in acceleration to track the evolution of the difference in position, i.e. an orbit in the reference frame centred around the planet.
There are also ways to use more advanced numeric integration techniques, e.g. Runge-Kutta or symplectic integrators, but learning about them may take some time.