Awesome Animator News

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:

mass = 1000
velocity = {x:15, y:0}

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:

mass = 50000
velocity = {x:-0.2, y:0}
We found the values of the masses mostly by experimentation. What we can do next is add the ability to drag the planets by adding the actions startDrag() and stopDrag() to the onPress and onRelease actions.
If you forget which events or references you’ve added to the MovieClip, Awesome Animator has the useful feature that when the MovieClips are selected it shows a list of events beside the shape and the reference name below the shape:
Showing events in editor

Showing events and references in editor

Other things you might like to add would be a reset button.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

Archives

All entries, chronologically...