Earth Moon Gravity Simulation
by Administrator on Oct.25, 2010, under Scripting
In this tutorial we shall make a basic gravitational simulation with the inverse square law of attraction. It should end up looking like this:
We draw two spheres and group each one as a MovieClip. Using the frame panel we assign a reference name to each one: earth and moon. For the moon we set up the variables by choosing the MovieClip Events > onLoad action from the action menu. And type in:
And then for the MovieClip Events > onEnterFrame we have:
//work out distance from earth to moon
dx = _root.earth._x – _x
dy = _root.earth._y – _y
r = Math.sqrt(dx*dx + dy*dy)
//get the mass of the earth from the other MovieClip
m = _root.earth.mass
//change the velocity according to the inverse square law of attraction
velocity.x += m*dx/r/r/r
velocity.y += m*dy/r/r/r
//change the position with the values of the velocity
_x += velocity.x
_y += velocity.y
We then repeat these actions for the Earth but wherever it says earth put moon. Also, the starting values for the earth MovieClip in the onLoad action should be:

Showing events and references in editor
Other things you might like to add would be a reset button.