• Categories
  • actionscript 3 (7)
  • Design (10)
  • flash (6)
  • flex (6)
  • general (6)
  • inspiration (3)
  • interactivity (3)
  • MA (6)
  • photoshop (3)
  • processing (1)
  • programs (2)
  • Uncategorized (3)
  • vvvv (1)
  • APE Physics tutorial for actionscript 3

    today i thought it might be nice to write a simple tutorial about the Ape physics engine, as there aren’t that many tutorials available for it yet. So in this simple tutorial we will create this:

    first we start of with creating a new flash file which we will be naming “ApeExample.fla”, after we have done that we create a new actionscript file called “ApeExample.as” and another called “Ball.as”.

    then we go on the stage of the flash file and we create a circle object which we will convert to a movieclip symbol and we check the box “Export for ActionScript”. then you can delete the circle object from the stage as long as its still in the library its okay.

    now we go to the Ball.as file we created and we paste the following into it(the inline comments should provide with enough explanation about how it all works):

    package {
    import flash.display.MovieClip;

    public class Ball extends MovieClip {
    public function Ball() {
    }
    }
    }

    after that’s done we go into our “ApeExample.as” file and paste in the following:

    package {
    import flash.display.MovieClip;
    import org.cove.ape.*; //imports the ape library
    import flash.events.*; //this is needed for the event listeners

    public class ApeExample extends MovieClip{
    //create arrays to store the instance's of the objects
    var ball:Array = new Array();
    var ballHolder:Array = new Array();
    var ballInstance:Array = new Array();
    var spring:Array = new Array();

    //creates the groups where the made objects will be placed into
    var group1:Group = new Group();
    var group2:Group = new Group();

    //settings of the variables for the balls
    var ballPosX:Array = new Array(200,240,280,320);
    var ballPosY:Array = new Array(195,195,195,195);
    var ballRadius:Number = 20;
    var ballFixed:Boolean = false;
    var ballMass:Number = 1;
    var ballElasticity:Number = 0.4;

    //settings of the variables for the ball place holders
    var ballHolderPosX:Array = new Array(200,240,280,320);
    var ballHolderPosY:Array = new Array(55,55,55,55);

    //settings of the variables for the rope from the holder to the ball
    var springStiffness:Number = 0.3;

    public function ApeExample() {

    addEventListener(Event.ENTER_FRAME, run);
    //needed place the physics into our stage, init is the setting for the accuracy of the physics calculation
    APEngine.init(1/3);
    APEngine.container = this;
    //adds the gravity to the stage in this case strength of 2 in vertical gravity
    APEngine.addForce(new VectorForce (false,0,2)); //replace new 'VectorForce (false,0,2)' with 'Vector (0,2)' if your not using the svn repository but the download package

    //makes the objects in group1 collide with each other
    group1.collideInternal = true;

    //loop that creates our objects
    for (var i = 0; i < 4; i++){

    //creation of the physics ball and the holder for the ball
    ball[i] = new CircleParticle(ballPosX[i], ballPosY[i], ballRadius, ballFixed, ballMass, ballElasticity);
    ballHolder[i] = new CircleParticle(ballHolderPosX[i], ballHolderPosY[i], 1, true);

    //add them to 2 different groups so the balls dont collide against our holders
    group1.addParticle(ball[i]);
    group2.addParticle(ballHolder[i]);

    //here we create the spring /the rope that holds the object together
    spring[i] = new SpringConstraint (ball[i], ballHolder[i], springStiffness);
    group1.addConstraint(spring[i]);

    //here we create a instance of our ball that we had drawn in our fla file
    ballInstance[i] = new Ball();
    addChild(ballInstance[i]);
    //here we say that the physics object should take the look of our ball that we had drawn
    ball[i].setDisplay(ballInstance[i]);
    spring[i].visible = true; //toggle the visibility of the rope/elastic band
    ballHolder[i].visible = true; //toggle the visibility of the holding point

    //ballInstance[i].buttonMode = true;
    }

    //here we add the groups to the physics engine
    APEngine.addGroup(group1);
    APEngine.addGroup(group2);

    //give the first ball we created a eventlistener so that we click on it it jumps to the left and sets the whole thing in motion
    ballInstance[0].addEventListener(MouseEvent.CLICK,clicker);
    }

    function clicker (evt:MouseEvent):void {
    ball[0].px = 0;

    }

    //this function is the heart of it all as this makes all appear on the stage and calculates the stuff
    function run(evt:Event):void {
    APEngine.step();
    APEngine.paint();

    }
    }
    }

    now test your ApeExample file and your flash movie should show the nice balls now if you click on the first one it should start the bouncing. if you still might have any further questions just contact me

    files are for download here

    Joost | May 3rd, 2008 | Design, actionscript 3, flash, flex
    Copyright 2007 | Designed by Joost Harts Add to Technorati Favorites