davepagurek / OrbitalMechanics

AS3 planetary physics simulator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OrbitalMechanics

An Actionscript 3 planetary physics simulator. A live demo is available at http://www.davepagurek.com/orbit/

Features

  • Calculates gravitational force
  • Simulates inelastic collisions
  • Provides example UI

Example

Here is an example body with an initial velocity being added to the system: ```actionscript var system:OrbitalMechanics = new OrbitalMechanics(); system.addBody(new Body(0, 50, 3, new Geovector(6, Math.PI))); system.x = 0; system.y = 0; stage.addChild(system); ```

Usage

The System

The simulation is a MovieClip-based object, which can be created like so: ```actionscript var system:OrbitalMechanics = new OrbitalMechanics(); system.x = 0; system.y = 0; stage.addChild(system); ``` Bodies can be added and removed from the system: ```actionscript system.addBody(b:Body); system.removeBody(b:Body); ```

The system can stop and start updating on ENTER_FRAME using the following functions:

system.pause();
system.resume();

The system is calculating on ENTER_FRAME by default when it is initialized.

Bodies

Bodies are initialized in the following way: ```actionscript var b:Body = new Body(x:Number, y:Number, mass:Number, velocity:Geovector); ```

Bodies have the following public properties:

velocity:Geovector
mass:Number //read-only
radius:Number //read-only

Geometric Vectors

Geometric vectors (class Geovector) are initialized like this: ```actionscript var v:Geovector = new Geovector(magnitude:Number, angle:Number); ``` All angles are in radians.

Geovectors can be added or subtracted from other geovectors:

v.add(v2:Geovector);
v.subtract(v2:Geovector);

Geovectors can be split into their components using their x and y functions:

v.x():Number
v.y():Number

Geovectors have the following public properties:

magnitude:Number
angle:Number

Math

Gravitational force is calculated using the formula F = (G * m1 * m2)/(d2), where G is the arbitrary number 100, masses are in arbitrrary units where mass = 1 is comparable to a small asteroid, and distance is measured in Flash units.

When two bodies collide (when the distance between them is less than the sum of their radii), they are replaced with a single body with their combined mass. The resulting body's velocity is found assuming it is a perfectly inelastic collision using the formula m1v1 + m2v2 = (m1 + m2)v'

About

AS3 planetary physics simulator


Languages

Language:ActionScript 89.4%Language:HTML 10.6%