APE – Actionscript Physics Engine – is an Actionscript 3 library that create physics objects into Flash, using Flash CS3 and CS4, with Flash Player 9, powerfull utility for Flash games because suport physics, collisions and others.

APE (Actionscript Physics Engine) is a free AS3 open source 2D physics engine for use in Flash and Flex, released under the MIT License. APE is written and maintained by Alec Cove.

A little sample of APE usage:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Use key A and D to move the car.

Well to begin the APE code we need download and import the class into your actionscript file. To download it you use SVN address: http://ape.googlecode.com/svn/ and to import into actionscript you need use these lines bellow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package
{
	import flash.display.*;
	import org.cove.ape.*;
 
	public class apeSample extends MovieClip
	{
		private var apeGroup:Group;
		private var wheelr:WheelParticle;
		private var wheell:WheelParticle;
		private var wheelg:SpringConstraint;	
 
		public function apeSample():void
		{
			APEngine.init(1/4);
			APEngine.container = APEBox;
			APEngine.addForce(new VectorForce( false, 0, 2 ) );
			APEngine.damping = .98;
 
			apeGroup = new Group();
			apeGroup.collideInternal = true;
 
			APEngine.addGroup(apeGroup);
 
			APECar();
			APEState();
 
			addEventListener(Event.ENTER_FRAME, runAPE);
		}
 
		private function APECar():void
		{
			var rWheel:DisplayObject = new newBeatleWheel(); // For Custom Display
			wheelr = new WheelParticle(70,40,10,false,3);
			wheelr.setDisplay(rWheel);
			wheelr.addEventListener(CollisionEvent.COLLIDE, checkAPECollision);
 
			var lWheel:DisplayObject = new newBeatleWheel(); // For Custom Display
			wheell = new WheelParticle(144,40,10,false,3);
			wheell.setDisplay(lWheel);
 
			var carBox:DisplayObject = new carObject(); // For Custom Display
			wheelg = new SpringConstraint(wheelr,wheell,.5,true,2,1.6,true);
			wheelg.setDisplay(carBox,-1,15,180); 
 
			apeGroup.addParticle(wheelr);
			apeGroup.addParticle(wheell);
			apeGroup.addConstraint(wheelg);
		}
 
		private function APEState():void
		{
			var floor:RectangleParticle = new RectangleParticle(stage.stageWidth/2,stage.stageHeight/4,2000,20,0,true);
			apeGroup.addParticle(floor);
		}
 
		private function runAPE():void
		{
			APEngine.step();
			APEngine.paint();
		}
	}
}

A Simple sample of car running:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Use LEFT and RIGHT arrow to move the car.
Soon more codes and samples using APE Physics and bellow some links for download ando study it.

APE Site: http://www.cove.org/ape/
APE Google Group : http://groups.google.com/group/ape-general/

Read too:

20 Responses to “APE – Actionscript Physics Engine for beginners”

  1. ellarose Says:

    thanks sooo much!!!

  2. John Barrett Says:

    I could not get this working:( Am I doing something wrong?
    (1) Downloaded the packages from the APE Site
    (2) created a apeSample.as file using the above code
    (3) create a apeSample.fla file, and used my as file as the document class

    saved both files in ape_a045/Source then I could access the org.cove.ape.*

    Many errors.
    Thanks for your help, great blog by the way`-`
    Johnny

  3. Pablo Davi Says:

    @John Barrett: Well, i had some problems when i begin because i was export the flash project using flash player 10. Other thing that you need see is the library folder, if it was imported into your libs or in this project. You can see this going to Edit > Preferences > Actionscript > Actionscript 3 Settings and add the folder that have “org.cove.ape” into your source path.

    Try it XD and Good lucky

  4. Pablo Davi Says:

    @ellarose: needing only tell me what XD Cya

  5. » Probable errors when working with APE for beginners | DES 84 - Blog of DES84 Website Says:

    [...] can search help into APE – General Group. The link is in my last post about the APE:   APE – Actionscript Physics Engine for beginners Escrito por: Pablo Davi e Gravado em: ActionScript 3, Flash, Games, Physics, [...]

  6. John Barrett Says:

    Hi Pablo, thanks for your help,and yes your solution was the key to get it going. I got a blank swf file but no errors`-` Now I have to figure out how to access the as example file from your site. I am checking the other post that you posted tonight and hopefully I can figure this out.

    Thanks so much for your help,
    Johnny

  7. Pablo Davi Says:

    Well @John Barret if you need more help only talk here. Im studing too the APE and other Physic Engines, but i like so much more APE. Soon i will post other samples and maybe simples games using physics.

  8. Nick Says:

    Hi. Been using ape for a while now for some games that I’ve made at work. Really cool tool.

    I have a question for Pablo about the Group class. Best to ask as an example:

    SCENARIO: I have a document class called “Main.as” and a custom class that extends Group called Vehicle.as (this class makes all the parts of the vehicle in the game).

    PROBLEM: In the constructor of Main.as I am doing this (private var vehicle:Vehicle; is above constructor.

    vehicle = new Vehicle();
    APEngine.addGroup(vehicle);

    trace(vehicle.px);
    trace(vehicle.py);

    QUESTION: These traces are throwing a NULL error… WHY?

    Any help is appreciated. Wasn’t sure if you had problems checking on a Group’s x and y location.

  9. Pablo Davi Says:

    Hi Nick, thaks for the comment.
    Well reading the APE Doc in: http://www.cove.org/ape/docs/api/org/cove/ape/Group.html#Group() you will read that “Group class is the main organizational class for APE. Once groups are created and populated with particles, constraints, and composites, they are added to the APEngine. Groups may contain particles, constraints, and composites. Composites may only contain particles and constraints.

    eg. you can take the px and py of particles but not of group. One way to resolve your problem you can create your vehicle into your vehicle.as and return an SpringConstraint or individual particle.
    Try it XD i hope help you.

  10. Nick Says:

    ok, cool. what I was looking for was a confirmation that you cannot put px or py on Group and you did just that. Thanks!

    Yeah, I am getting the px and py by digging into a particle that lives in the vehicle group as a “work around”, but I guess thats the way I have to do it :D

    Can you give an example of returning a SpringConstraint or particle? Also, how did you reposition the Beatle above if you can’t move the group?

  11. Pablo Davi Says:

    @Nick: the reposition was done using a new function that verify the py of car. This function is called into the runAPE() (an EnterFrame) and alway that the py is more than 700px i removeParticles and removeConstraint and run again the APECar() to create it again.

    About the SpringContraint you can try it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    private function APECar():SpringConstraint
    {
    	wheelr = new WheelParticle(70,40,10,false,3);
    	wheelr.setDisplay(rWheel);
     
    	wheell = new WheelParticle(144,40,10,false,3);
    	wheell.setDisplay(lWheel);
     
    	wheelg = new SpringConstraint(wheelr,wheell,.5,true,2,1.6,true);
    	wheelg.setDisplay(carBox,-1,15,180);
     
    	return wheelg;
    }

    Really i dont test this code but this will return a SpringContraint :)

  12. Rashid Says:

    hi there Pablo. I am a budding game developer and just a few weeks back i came across the APE. I have made a few applications. I have made a level Editor in APE which supports bridges, rectangles, circles and spring constraints.All you have to do is give the required paremeters through an XML. So that saves alot of time if u wanna generate a long level. I have a few issues though, hope you can help.

    I also want that i can have a semicircle particle too,which will help to make slopes ( i know this can be done by rectangleparticle), but i wont be able to achive the curve. Hope u get my point bro. waiting 4 ur reply.

  13. Pablo Davi Says:

    Hi and thansk for read and comment @Rashid, i dont see something to create the custom terrain or custom object like a semicircle, buuuuut you can try one simple solution that create a fixed circle sequence creating the custom object format that you want. I will continue reading and studing more about and if i find other solution i will post here.

  14. Rashid Says:

    thanks alot Pablo, i was finally able to achive a semicircle using rectabgle particles joining them with spring constraints. I have actually done a hell lot of thngs with the APE :

    1. Path maker
    2. level editor
    3. now a semicircle

    Now m in to making springs as well.

  15. Pablo Davi Says:

    @Rashid : When you create your game show for us here.

  16. Adi Says:

    neat demo! I am trying to add a canvas with buttons as my display for a particle but that does not seem to work… any suggestions for me?

  17. ArcadeNano Says:

    LOVE your site, will visit again :) Submitted this post to Google News Reader.

  18. Pablo Davi Says:

    Hi Adi, well… i tryed add dynamic content into my Displayobject and put it using setDisplay to my particle but with out success. You can add mouseEvent to particles but dynamic content i dont know yet but i will try other ideas and if have success i will post here to you. GoodLucky

  19. Ellie Tasse Says:

    great post thanks for the info

  20. Blanche Langwell Says:

    Do you guys think that she would do well in this year ?

Leave a Reply