Friday, August 14, 2009

Getting started with Physics in AS3 : APE Tutorial

Today, we're going to see a very simple tutorial for using the Actionscript Physics Engine (APE). There are several Physics engines in the AS3 but APE is probably the easiest to use. So, download the library via SVN here and lets get started.
Here is the code :

package
{
import flash.display.Sprite;
import flash.events.Event;

import org.cove.ape.APEngine;
import org.cove.ape.CircleParticle;
import org.cove.ape.Group;
import org.cove.ape.RectangleParticle;
import org.cove.ape.VectorForce;

[SWF(width = 600, height = 400, backgroundColor='0x000000')]
public class APExample Sprite
{
public function APExample():void
{
initAPE();

initObjects();

addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

private function initAPE():void
{
// Initialize APE
APEngine.init();

// Set the container for APE
APEngine.container = this;

// Add gravity
APEngine.addForce(new VectorForce(false, 0, 2));
}

private function initObjects():void
{
// Create a group to hold all the objects with collision
var group:Group = new Group(true);

// Create a rectangle for rhe floor
var floor:RectangleParticle = new RectangleParticle(0, 400, 1200, 25, 0, true);

// Create a circle
var circle:CircleParticle = new CircleParticle(225, -100, 25);

// Add the floor to the group
group.addParticle(floor);

// Add the circle to the group
group.addParticle(circle);

// Add the group to the engine
APEngine.addGroup(group);
}

private function onEnterFrame(e:Event):void
{
// Update and Draw
APEngine.step();
APEngine.paint();
}

}

}
Compile and run. You should see a white rectangle at the bottom as the "floor" and the ball will fall down. Experiment a little bit by chaanging the gravity value for some interesting results. For example, you could set a positive value for x which would make the ball move sideways. But still, one ball falling down and bouncing isn't much of an example. In the coming tutorials, we'll be developing a simple game using APE. Stay tuned. :D

UPDATE : Certain compiler errors occur (changes in several classes) if you are using the given download link in the APE homepage. The one i am using here is from svn.
I've also uploaded the project with the APE files for those without SVN. You can download it here. Sorry for the mmistake.

4 comments:

  1. Tried it and got various compiler errors within the ape classes (APEngine.as and CollisionDetector.as)

    ReplyDelete
  2. @Anonymous. Which version are you using? Try to get the latest version via svn. http://ape.googlecode.com/svn/trunk/

    ReplyDelete
  3. You'll need to rename the Vector class to something else (like "APEVector") otherwise it won't work with the Flash 10 Player. This has to be done in *every* class... by hand! But it but will work once you do this. Hopefully some AS3.0 developers will show some interest in APE to bring it up to date.

    ReplyDelete
  4. where can I find useful websites about physics? especially physics that deal with engineering

    Thank you!!

    ReplyDelete