I am trying to use Newton's 2nd law to simulate sun-earth-moon system. I can get the moon and earth parallely rotating around the sun. But the moon does not rotate around the earth.
I know there should be attraction between the moon and sun, and also between the moon and the earth. So I added the acceleration together. But in the 3D visualization, it seems there's no effect of the earth to the moon.
Could anyone check my code on the motion part:
def movePlanets(self):
rate(200)
G = (6.673e-11)
dt = 12*3600 # half day
for p in self.planets:
p.moveTo((p.getXPos() + dt * p.getXVel()),
(p.getYPos() + dt * p.getYVel()),
(p.getZPos() + dt * p.getZVel()))
rx = self.thesun.getXPos() - p.getXPos()
ry = self.thesun.getYPos() - p.getYPos()
rz = self.thesun.getZPos() - p.getZPos()
r = math.sqrt(rx**2 + ry**2 + rz**2)
accx = G * self.thesun.getMass()*rx/r**3
accy = G * self.thesun.getMass()*ry/r**3
accz = G * self.thesun.getMass()*rz/r**3
for pTwo in self.planets:
if(pTwo != p):
rx = pTwo.getXPos() - p.getXPos()
ry = pTwo.getYPos() - p.getYPos()
rz = pTwo.getZPos() - p.getZPos()
r = math.sqrt(rx**2 + ry**2 + rz**2)
accx = accx + (G * pTwo.getMass()*rx/r**3)
accy = accy + (G * pTwo.getMass()*ry/r**3)
accz = accz + (G * pTwo.getMass()*rz/r**3)
p.setXVel(p.getXVel() + dt * accx)
p.setYVel(p.getYVel() + dt * accy)
p.setZVel(p.getZVel() + dt * accz)
Thank you so much!