# Programming a simulation that shows the orbits of the moon and a spacecraft around the Earth

I am trying to use GlowScript to make a visual representation of the moon orbiting the Earth and spherical spacecraft orbiting both the moon and the Earth using concepts in physics (i.e. Newton’s 2nd Law, kinematics, planetary motion, etc.). I was able to program the moon orbiting the earth just fine, but I can’t seem to get the spacecraft to orbit the moon properly. I tried using the same procedure, this time calculating the forces on the earth and the moon on the craft, but I seem to be missing something. The part of the code I am struggling with is shown below.

``    craft.pos = craft.pos + v_craft*dt  f_earthcraft_mag = G*m_earth*m_craft/mag(craft.pos-earth.pos)**2 f_earthcraft_dir =  norm(earth.pos-craft.pos) f_earthcraft = f_earthcraft_mag*f_earthcraft_dir   f_mooncraft_mag = G*m_moon*m_craft/mag(craft.pos-moon.pos)**2 f_mooncraft_dir = norm(moon.pos-craft.pos) f_mooncraft = f_mooncraft_mag*f_mooncraft_dir   f_craft = f_mooncraft  a_craft = f_craft/m_craft  v_craft = v_craft + a_craft*dt ``

and for reference, the part of the code that IS working correctly is below as well:

``    #move the moon with the current velocity for a time dt, xf = xi + v*t moon.pos = moon.pos + v_moon*dt   #calculate magnitude of the gravitational force on the moon at its current position, Fg = GMm/r^2 f_moon_mag = G*m_earth*m_moon/mag(moon.pos-earth.pos)**2  #calculate the unit vector that gives direction of the gravitational force (towards earth) f_moon_dir = norm(earth.pos-moon.pos)   #multiply the magnitude of the force by its direction to calculate the gravitational force vector f_moon = f_moon_mag*f_moon_dir   #calculate the acceleration of the moon using Newton's 2nd law, F = ma a_moon = f_moon/m_moon   #calculate the new velocity of the moon, vf = vi + a*t v_moon = v_moon + a_moon*dt  ``

Any help would be greatly appreciated : )

Posted on Categories buy elite proxies