APE – Actionscript Physics Engine for beginners
February 25th, 2009
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:
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:
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/





February 27th, 2009 at 05:18
thanks sooo much!!!
February 27th, 2009 at 08:35
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
February 27th, 2009 at 08:49
@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
February 27th, 2009 at 08:50
@ellarose: needing only tell me what XD Cya
February 27th, 2009 at 13:04
[...] 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, [...]
February 28th, 2009 at 04:55
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
February 28th, 2009 at 14:55
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.
March 9th, 2009 at 18:56
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.
March 9th, 2009 at 21:59
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.
March 10th, 2009 at 21:52
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
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?
March 11th, 2009 at 08:25
@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:
Really i dont test this code but this will return a SpringContraint
March 11th, 2009 at 13:02
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.
March 11th, 2009 at 13:59
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.
March 12th, 2009 at 13:19
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.
March 12th, 2009 at 14:13
@Rashid : When you create your game show for us here.
March 14th, 2009 at 21:52
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?
March 15th, 2009 at 14:49
LOVE your site, will visit again
Submitted this post to Google News Reader.
March 15th, 2009 at 16:43
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
March 18th, 2010 at 09:23
great post thanks for the info
March 20th, 2010 at 12:44
Do you guys think that she would do well in this year ?