Math and (Quasi) Physics in Action Script 3

Simple gravity model

This is a simple model to demonstrate how to simulate gravity.

In this example, gravity is not mutual but the circle A is fixed in a place and the circle B flies around it under the effect of gravity, The product of the first three terms(G * m1 * m2) in the Newton's law is simplified and set to 10, which is enough for rough simulation like this.
Try dragging A to change the orbit of B.

Newton's law of universal gravitation (in AS3)

F = G * m1 * m2 / Math.pow(r, 2);
  • F is the magnitude of the gravitational force between the two point masses
  • G is the gravitational constant (6.674 × 10−11 N m2 kg-2)
  • m1 is the mass of the first point mass
  • m2 is the mass of the second point mass
  • r is the distance between the two point masses

See ‘Newton's law of universal gravitation - Wikipedia’ for more about law of universal gravitation.

Main.as

package {
  import flash.display.*;
  import flash.events.*;
  import flash.geom.Point;
  public class Main extends PpGraphics {
    private var gravity:Number = 20;
    private var o1:PpObject,o2:PpObject;
    public function Main() {
      origin(stage.stageWidth / 2,stage.stageHeight / 2);
      
      o1 = new PpObject(0,0);
      o2 = new PpObject(150,0);
      o1.radius = 20;
      o2.radius = 8;
      o2.vx = 0;
      o2.vy = 4;
      
      circle(o1,o1.radius = 20,C_DRAGGABLE,"A");
      circle(o2,o2.radius = 8,C_MOVABLE, "B");
      path(o1,o2);
      
      enableDrag(o1);
    }
    
    override protected function step():void {
      var dist = Point.distance(o1,o2);
      if (dist >= o1.radius + o2.radius) {
        o2.vx += (o1.x - o2.x) / Math.pow(dist, 2) * gravity;
        o2.vy += (o1.y - o2.y) / Math.pow(dist, 2) * gravity;
        o2.x += o2.vx;
        o2.y += o2.vy;
      } else {
        o2.vx = o2.vy = 0;
      }
    }
    
  }
}

PpGraphics.as

package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  class PpGraphics extends Sprite {
    protected var canvas:Sprite;
    protected var gCanvas:Sprite;
    protected var tCanvas:Sprite;
    private var textout:TextField;
    private var shapeList:Array;
    private var pathList:Array;
    private var dragTarget:Sprite = null;
    private var handleList:Array;
    
    protected const C_MOVABLE:uint = 0xFF6600;
    protected const C_DRAGGABLE:uint = 0x0066FF;
    protected const C_FIXED:uint = 0x000000;
    protected const C_GUIDE:uint = 0xAAAAAA;
    
    public function PpGraphics():void {
      pathList = new Array();
      shapeList = new Array();
      handleList = new Array();
      
      addChild(gCanvas = new Sprite());
      gCanvas.graphics.lineStyle(0, C_GUIDE, 0.5);
      addChild(canvas = new Sprite());
      addChild(tCanvas = new Sprite());
      addChild(textout = getTextout());
      addEventListener(Event.ENTER_FRAME,h_enterFrame0);
      addEventListener(Event.ENTER_FRAME,h_enterFrame1);
      addEventListener(Event.ENTER_FRAME,h_enterFrame2);
    }
    protected function h_enterFrame0(evt:Event):void {
      updateDrag();
    }
    
    protected function h_enterFrame1(evt:Event):void {
      step();
    }
    
    protected function step():void {}
    
    private function h_enterFrame2(evt:Event):void {
      updateGraphics();
    }
    
    private function updateDrag():void {
      if (!dragTarget) { return }
      
      var item:Object;
      var i:uint;
      
      dragTarget.x = canvas.mouseX;
      dragTarget.y = canvas.mouseY;
      
      for (i = 0; i < handleList.length; i ++) {
        item = handleList[i];
        item.obj.x = item.sprite.x;
        item.obj.y = item.sprite.y;
      }
    }
        
    private function updateGraphics():void {
      var g:Graphics = canvas.graphics;
      var item:Object;
      var i:uint;
      
      for (i = 0; i < shapeList.length; i ++) {
        item = shapeList[i];
        item.sprite.x = item.obj.x;
        item.sprite.y = item.obj.y;
      }
      g.clear();
      for (i = 0; i < pathList.length; i ++) {
        item = pathList[i];
        g.lineStyle(item.w, item.col, 0.5);
        g.moveTo(item.obj1.x, item.obj1.y);
        g.lineTo(item.obj2.x, item.obj2.y);
      }
    }
    
    /*
    * origin
    */
    
    protected function origin(x,y):void {
      gCanvas.x = canvas.x = x;
      gCanvas.y = canvas.y = y;
    }
    
    /*
    * guide
    */
    
    protected function guide(x1:Number,y1:Number,x2:Number,y2:Number):void {
      var g:Graphics = gCanvas.graphics;
      g.moveTo(x1,y1);
      g.lineTo(x2,y2);
    }
    
    protected function guideLabel(x:Number,y:Number,str:String):void {
      var t:TextField = label(str, C_GUIDE);
      t.x = x;
      t.y = y;
      gCanvas.addChild(t);
    }
    
    
    /*
    * graphic items
    */
    
    protected function circle(obj:PpObject, radius:Number = 3, col:uint = C_FIXED,labelStr:String = "") {
      var s:Sprite = new Sprite();
      s.graphics.beginFill(col, 0.5);
      s.graphics.drawCircle(0,0,radius);
      s.graphics.endFill();
      if (labelStr.length > 0) {
        var l:TextField = label(labelStr,col);
        s.addChild(l);
      }
      return addShape(obj, s);
    }
    
    private function addShape(obj:PpObject,s:Sprite):Object {
      var item:Object = {obj:obj,sprite:s};
      s.x = obj.x;
      s.y = obj.y;
      canvas.addChild(s);
      shapeList.push(item);
      return item
    }
    
    protected function path(obj1:PpObject, obj2:PpObject,col:uint = C_MOVABLE, w:Number = 0):Object {
      var item:Object = {obj1:obj1,obj2:obj2,col:col,w:w};
      pathList.push(item);
      return item
    }

    /*
    * text
    */
    
    private function label(str:String, col:uint):TextField {
      var t:TextField = new TextField();
      var tf:TextFormat = new TextFormat();
      t.autoSize = TextFieldAutoSize.LEFT;
      t.selectable = false;
      tf.font = "_sans";
      tf.color = col;
      t.defaultTextFormat = tf;
      t.text = str;
      return t;
    }
    
    private function getTextout():TextField {
      var t:TextField = new TextField();
      var tf:TextFormat = new TextFormat();
      t.width = stage.width - 20;
      t.height = stage.height - 20;
      t.x = t.y = 10;
      t.multiline = true;
      t.autoSize = TextFieldAutoSize.LEFT;
      t.selectable = false;
      tf.font = "_sans";
      tf.color = C_GUIDE;
      t.defaultTextFormat = tf;
      return t;
    }
    
    protected function print(str:String):void {
      textout.appendText(str + "\n");
    }
    protected function clearText():void {
      textout.text = "";
    }
    
    
    /*
    * drag
    */
    
    protected function enableDrag(...args):void {
      var s:Sprite;
      var item:Object;
      for (var i:uint = 0; i < args.length; i ++) {
        for (var j:uint = 0; j < shapeList.length; j ++) {
          if (shapeList[j].obj == args[i]) { 
            s = shapeList[j].sprite;
            s.addEventListener(MouseEvent.MOUSE_DOWN, h_draggable_mouseDown);      
            s.addEventListener(MouseEvent.MOUSE_UP, h_draggable_mouseUp);
            s.useHandCursor = true;
            s.buttonMode = true;
            s.tabEnabled = false;
            item = {obj:args[i],sprite:s};
            handleList.push(item);
          }
        }
       
      }
    }

    
    private function h_draggable_rollOver(evt:Event):void {
      evt.currentTarget.alpha = 0.5;
    }
    
    private function h_draggable_rollOut(evt:Event):void {
      evt.currentTarget.alpha = 1;
    }
    
    
    private function h_draggable_mouseDown(evt:Event):void {
      dragTarget = evt.currentTarget as Sprite;
    }
    
    private function h_draggable_mouseUp(evt:Event):void {
      dragTarget = null;
    }
    
    /*
    * util
    */
    protected function radToDeg(n:Number):Number {
      return n / Math.PI * 180;
    }
    protected function degToRad(n:Number):Number {
      return n * Math.PI / 180;
    }
    protected function rd(n:Number):Number {
      return Math.round(n * 100) / 100;
    }
  }
}

PpObject.as

package {
  import flash.geom.Point;
  public dynamic class PpObject extends Point {
    public function PpObject(x:Number,y:Number):void {
      super(x,y);
    }
  }
}