Skip to content


[AS 3] APE – Actionscript Physics Engine

Here is my first attempt at using APE. APE is an open source 2D physics engine written in actionscript 3. You can download the class files here: http://code.google.com/p/ape/. Use your keyboard arrow keys to move the car object.

Here is my Document Class:

package {
	import org.cove.ape.*;
	import flash.events.Event;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import flash.display.Sprite;

	public class APEInteraction extends Sprite {

		private var wa:WheelParticle = new WheelParticle(160,20,10,false,100,.05);
		private var wb:WheelParticle = new WheelParticle(200,20,10,false,100,.05);

		public function APEInteraction() {
			addEventListener(Event.ENTER_FRAME, run);
			APEngine.init(1/4);
			APEngine.container = this;
			APEngine.addMasslessForce(new Vector(0,2));
			var defaultGroup:Group = new Group();
			defaultGroup.collideInternal = true;

			defaultGroup.addParticle(wa);

			defaultGroup.addParticle(wb);

			var base:RectangleParticle = new RectangleParticle(300,400,1000,10,0,true);
			defaultGroup.addParticle(base);

			var backstop:RectangleParticle = new RectangleParticle(70,97,60,10,0.4,true);
			defaultGroup.addParticle(backstop);

			var ramp1:RectangleParticle = new RectangleParticle(200,100,200,10,-0.1,true);
			defaultGroup.addParticle(ramp1);

			var ramp2:RectangleParticle = new RectangleParticle(400,200,200,10,0.1,true);
			defaultGroup.addParticle(ramp2);

			var ramp3:RectangleParticle = new RectangleParticle(200,300,200,10,-0.1,true);
			defaultGroup.addParticle(ramp3);

			var ramp4:RectangleParticle = new RectangleParticle(400,350,200,10,0.1,true);
			defaultGroup.addParticle(ramp4);

			var ramp5:RectangleParticle = new RectangleParticle(220,390,80,10,-0.7,true);
			defaultGroup.addParticle(ramp5);

			var wc:SpringConstraint = new SpringConstraint(wa, wb, .8, true, 3);
			defaultGroup.addConstraint(wc);
        	APEngine.addGroup(defaultGroup);

			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_pressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_released);
		}

		function key_pressed(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.UP)
			{
				wa.angularVelocity = 0.1;
				wb.angularVelocity = 0.1;
			}

			if (event.keyCode == Keyboard.DOWN)
			{
				wa.angularVelocity =- 0.1;
				wb.angularVelocity =- 0.1;
			}
		}

		function key_released(event:KeyboardEvent):void
		{
			wa.angularVelocity = 0;
			wb.angularVelocity = 0;
		}

		private function run(evt:Event):void {
			APEngine.step();
			APEngine.paint();
		}
	}
}

Posted in APE - Actionscript Physics Engine, Actionscript 3.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.