Rocketeer Lesson 2: Orbital Dynamics in Actionscript 2.0.

If you want to follow along as you read, you can download the flash source from lesson 1 (click right and select save file). When you open the document, you will see something like this:

Now, add the following code to "onClipEvent(load)."

	km2pix = 150.0/7368.0;
	xs = (_x - W/2)/km2pix;
	ys = (_y - H/2)/km2pix;
	G = 398601.2;
	dt = 360.0/25.0;

On the screen, 150 pixels correspond to 7368.0 meters in space, so the conversion factor to go from space coordinates to pixel coordinates is km2pix = 150.0/7368.0. The vector (xs,ys) is the vector from the center of the earth (which is going to be at the center of the screen), to the rocketship, and the units are kilometers. The variable G is the gravitational constant of the earth. We set dt = 360.0/25.0 because we want time to advance six minutes (360 seconds) every 25 frames (1 second in real time).

Now we can implement the equations of motion rocket as it orbits the earth. We want the rocket to be in circular orbit at its starting position, so the initial velocity will be circular orbit velocity.

   r = Math.sqrt(xs*xs+ys*ys);
   u = -Math.sqrt(G/r);  // initial vertical velocity
   v = 0.0               // initial horizaontal velocity
Now, we will write a function to compute the gravitational acceleration. Add the following actionscript to the end of the onClipEvent(enterFrame) event handler (the functions must be inside an onClipEvent handler):
   function gravity() {
      r2 = xs*xs + ys*ys;
      r = Math.sqrt(r2);
      a = -G/r2;
      ax = a*xs/r; ay = a*ys/r;
   }
Next, right after or just before the gravity function, we add a function to propagate the position and velocity of the spaceship.
	function propagate() {

		gravity();
		x1 = xs;
		y1 = ys;
		xs = xs + dt*v/2.0 + dt*dt*ax/8.0;
		ys = ys + dt*u/2.0 + dt*dt*ay/8.0;
		gravity();

		v1 = v;
		u1 = u;
		v = v + dt*ax;
		u = u + dt*ay;
		xs = x1 + dt*(v1+.5*dt*ax);
		ys = y1 + dt*(u1+.5*dt*ay);
		
		_x = km2pix*xs + W/2;
		_y = km2pix*ys + H/2;
	}
This function updates the position xs,ys and velocity v,u and the moves the graphic of the ship to the new location on the screen. Now test the movie and watch the rocket slowly orbit the earth in a circular orbit.

That brings us to the end of lesson 2.

If you are ready, you can proceed to lesson 3 now.