• home
  • forum
  • my
  • kt
  • download
  • Gravity Simulation in Flash

    Author: 2008-10-29 13:35:03 From:

    I’ve got some kind of compulsory disorder. Sometimes I just have to do some simulations, play with physics and get a break from normal web development. I haven’t had an “episode” in over two years now (view the last one), but a few days ago all this pent up need just had to be released and this tutorial is the uber-geeky result. Just touch the spheres with the mouse pointer, not too fast or you’re gonna break out of orbit!

    So how do we accomplish the above? The answer is modified physical laws plus some equation solving and scripting, we’ll go through everything.

    Let’s start with the AS in the .fla file (from top to bottom):

    var oldPoint = new V2D(0, 0);
    var curPoint = new V2D(0, 0);
    var mouseSpeed = new V2D(0, 0);
    var maxSpeed = 8;
    var moveInterval = 50;
    We initiate some globals to control various things:

    oldPoint: The position the mouse pointer had in the previous frame in time. The object

    keeping track of this is a V2D from 2 Dimensional Vector. A vector is basically just

    an X value and an Y value which will result in a direction and length/power. We’ll get

    to the details of this one shortly.

    curPoint: The current position of the mouse pointer.

    mouseSpeed: With the help of the oldPoint and the curPoint we can calculate the speed

    and direction of the mouse pointer and store the result in mouseSpeed. This value will

    be used to calculate in which direction and with which speed a sphere shall move

    after being hit by the mouse pointer.

    maxSpeed: With some imagination this value could be thought of as the speed of light. It’s a dampening value that will be applied to moving objects to dampen their oscillations, it would be pretty boring if they broke loose from their wells after like 2 seconds.

    moveInterval: This is the amount in time frames that has to pass before the mouse can hit a particular sphere again after hitting it. Set it to 1 and you’ll see why we need it.

    var objects = new Array(
    	new AdvObj(circle0), 
    	new AdvObj(circle1)
    );
    
    var wells = new Array();
    for(i in objects){
    	wells[i] = (new GravityWell(objects[i], _root["well"+i]._x, _root["well"+i]._y, 2, 1e9, 1000));
    	objects[i].moving = -1;
    }

    We will store the objects we want to be affected by the gravity wells in an array we call objects, they will be of type AdvObj. Circle0 and 1 refers to the movieclip instances on the stage, I named them in the properties area. Next we create our wells by looping through our spheres/circles and passing them as the first argument to the GravityWell constructor (we will go through the GravityWell class shortly).

    They will be mapped to the positions of two invisible movieclips on stage called well0 and 1. The final three arguments are the power we want the gravity to diminish by as a function of the distance (it’s 2 in the real universe).

    The second last argument would constitute m1*m2 in the gravity formula and the final argument is a value that we manipulate the behavior of our gravity with, this last value will be involve in some logic that breaks the na

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (16)
      3rd Party (7)
      Navigation (63)
      Actionscripting (158)
      Optimization (17)
      Animation (108)
      Projector (11)
      Audio (52)
      Special Effects (152)
      Backend (26)
      Text Effects (82)
      Drawing (32)
      Tips and Techniques (47)
      Dynamic Content (31)
      Tricks (8)
      Games (97)
      Utilities (21)
      Getting Started (91)
      Video (24)
      Interactivity (43)
      Web Design (29)

    New

    Hot