/*

===========ORBIT==================

This source code is under

GNU GENERAL PUBLIC LICENSE

Copyright 2009 Han Erim

            ALICE LAW

http://www.aliceinphysics.com

This script is written with ActionScript 3.0

==================================

*/

 

var orbitRadius:Number=125;

var angularSpeedOfMercury:Number=0;

var angularSpeedOfEarth:Number=0;

 

var speedOfLight=1;

var rememberX,rememberY,remember:Number=0;

var ticket:Boolean=false;

var earthMovement:Boolean=true;

var distX,distY,distance=Number;

var mc:imagePackage;

var animationTicket:Boolean=false;

 

sun.graphics.lineStyle(0,0x333333,1);

sun.graphics.drawEllipse(-300,-300,600,600);

earth.x=sun.x+300;

 

 

 

function action(e:Event) {

            angularSpeedOfMercury+=2;

            speedOfLight=ns.value;

 

            //Orbital movement of Mercury

            mercury.x=sun.x + Math.cos(angularSpeedOfMercury*Math.PI/180)*orbitRadius;

            mercury.y=sun.y +  Math.sin(angularSpeedOfMercury*Math.PI/180)*orbitRadius;

 

            //Orbital movement of Earth

            if (earthMovement == true) {

                        angularSpeedOfEarth+=.2;

                        earth.x= Math.cos(angularSpeedOfEarth*Math.PI/180)*control.slider.x+sun.x;

                        earth.y = Math.sin(angularSpeedOfEarth*Math.PI/180)*control.slider.x+sun.y;

            }

   

 

    

            //===========MERCURY============

            //The distance between Mercury and Earth

            distX=mercury.x-earth.x;

            distY=mercury.y-earth.y;

            distance=Math.sqrt(distX*distX+distY*distY);

 

            //Sets a new image package for Mercury

            mc=new imagePackage();

            mc.arrivalTime=distance/speedOfLight;

            mc.positionX=distX;

            mc.positionY=distY;

            mc.ghost=ghost;

            mc.earth=earth;

 

             //===========SUN============

            //The distance between Sun and Earth

            distX=sun.x-earth.x;

            distY=sun.y-earth.y;

            distance=Math.sqrt(distX*distX+distY*distY);

 

            //Sets a new image package for  Sun

            mc=new imagePackage();

            mc.arrivalTime=distance/speedOfLight;

            mc.positionX=distX;

            mc.positionY=distY;

            mc.ghost=sun_ghost;

            mc.earth=earth;

 

 

            //Draw the orbit of the ghost of Mercury.

            if (ticket==true) {

                        this.graphics.lineTo(ghost.x,ghost.y);

            }

}

 

 

 

//===================CONTROLS======================

 

drawButton.addEventListener(MouseEvent.MOUSE_DOWN,startDraw);

clearButton.addEventListener(MouseEvent.MOUSE_DOWN,clearDraw);

 

function startDraw(e:MouseEvent):void {

            ticket=true;

            this.graphics.clear();

            this.graphics.moveTo(ghost.x,ghost.y);

            this.graphics.lineStyle(0,0xCC0000,1);

}

 

function clearDraw(e:MouseEvent):void {

            ticket=false;

            this.graphics.clear();

}

 

//===================================================

 

control.addEventListener(MouseEvent.MOUSE_DOWN, orbitOfTheEarth);

 

function orbitOfTheEarth(e:MouseEvent):void {

            this.addEventListener(Event.ENTER_FRAME, sizerMove);

            stage.addEventListener(MouseEvent.MOUSE_UP, sizerUp);

            function sizerMove(e:Event):void {

                        sun.graphics.clear();

                        sun.graphics.lineStyle(0,0x333333,1);

                        sun.graphics.drawEllipse(-2*control.slider.x/2,-2*control.slider.x/2,2*control.slider.x,2*control.slider.x);

                        earth.x= Math.cos(angularSpeedOfEarth*Math.PI/180)*control.slider.x+sun.x;

                        earth.y = Math.sin(angularSpeedOfEarth*Math.PI/180)*control.slider.x+sun.y;

            }

            function sizerUp(e:MouseEvent):void {

                        removeEventListener(Event.ENTER_FRAME, sizerMove);

                        stage.removeEventListener(MouseEvent.MOUSE_DOWN, sizerUp);

            }

}

 

 

//=====================================

startButton.addEventListener(MouseEvent.MOUSE_DOWN,startAnimation);

stopButton.addEventListener(MouseEvent.MOUSE_DOWN,stopAnimation);

 

function startAnimation(e:MouseEvent):void {

            if (animationTicket==false) {

                        this.addEventListener(Event.ENTER_FRAME, action);

            }

            animationTicket=true;

}

 

function stopAnimation(e:MouseEvent):void {

            if (animationTicket==true) {

                        this.removeEventListener(Event.ENTER_FRAME, action);

            }

            animationTicket=false;

}

 

 

//===============IMAGE PACKAGE MOVIE CLIP================ 

// Created image package movie clip has a timer that sets itself according to arrival time.

// The place of the ghost will depend on positionX and positionY values 

// which the main function pass these values in the beginning.

// When the timer function run,  sets the place of the ghost.

 

 

var earth:MovieClip;

var ghost:MovieClip;

var arrivalTime:Number;

var positionX:Number;

var positionY:Number;

 

var timer:Timer=new Timer(arrivalTime,1);

timer.addEventListener("timer", timerHandler);

timer.start();

 

function timerHandler(event:TimerEvent):void {

            ghost.x=earth.x+positionX;

            ghost.y=earth.y+positionY;

            timer.removeEventListener("timer", timerHandler);

}