Orbit
Press the up arrow to add a planet at the mouse position. Click and drag to launch an object with a force vector. Press c to remove all small objects, and r to remove all objects.
/**
* Copyright JZE ( http://wonderfl.net/user/JZE )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ifAl
*/
package {
import flash.geom.Point;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;
[SWF(width=600,height=600,frameRate=60)]
public class Orbit extends Sprite {
private var obs:Vector.<obj>;
private var pls:Vector.<Point>;
private const G:Number = 0.00067;
private const P_MASS:Number = 3000000;
private const O_MASS:Number = 1;
private var i:uint, j:uint;
private var angle:Number, F:Number;
private var cvs:Sprite;
private var md:Boolean = false;
private var spxy:Point;
public function Orbit() {
obs = new Vector.<obj>();
pls = new Vector.<Point>();
cvs = new Sprite();
addChild(cvs);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
stage.addEventListener(MouseEvent.MOUSE_UP, mup);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void {
cvs.graphics.clear();
cvs.graphics.lineStyle(0);
if(md) {
cvs.graphics.moveTo(spxy.x, spxy.y);
cvs.graphics.lineTo(mouseX, mouseY);
}
for(i = 0; i < obs.length; i++) {
for(j = 0; j < pls.length; j++) {
angle = Math.atan2(obs[i].y-pls[j].y,obs[i].x-pls[j].x);
F = G*P_MASS*O_MASS/((obs[i].y-pls[j].y)*(obs[i].y-pls[j].y)+(obs[i].x-pls[j].x)*(obs[i].x-pls[j].x));
obs[i].vx -= F*Math.cos(angle);
obs[i].vy -= F*Math.sin(angle);
}
obs[i].x += obs[i].vx;
obs[i].y += obs[i].vy;
cvs.graphics.beginFill(0);
cvs.graphics.drawCircle(obs[i].x, obs[i].y, 2);
cvs.graphics.endFill();
}
for(j = 0; j < pls.length; j++) {
cvs.graphics.beginFill(0);
cvs.graphics.drawCircle(pls[j].x, pls[j].y, 20);
cvs.graphics.endFill();
}
}
private function kdown(e:KeyboardEvent):void {
switch(e.keyCode) {
case 38:
pls.push(new Point(mouseX, mouseY));
break;
case 67:
obs = new Vector.<obj>();
break;
case 82:
obs = new Vector.<obj>();
pls = new Vector.<Point>();
break;
}
}
private function mdown(e:MouseEvent):void {
md = true;
spxy = new Point(mouseX, mouseY);
}
private function mup(e:MouseEvent):void {
md = false;
obs.push(new obj(spxy.x, spxy.y, mouseX-spxy.x, mouseY-spxy.y));
}
}
}
import flash.geom.Point;
class obj extends Point {
public var vx:Number, vy:Number;
public function obj(x:Number, y:Number, ivx:Number, ivy:Number) {
super(x, y);
vx = ivx/10;
vy = ivy/10;
}
}