26Mar

[AS 3] AS Physics Engine (APE) - Bridge Demo

4 comments so far

So the more I dug into the code of APE, I wanted to understand a little more about the Bridge that was created in the carDemo.swf file that comes packaged with the code library. I quickly wrote up and modified a little of the existing code to come up with the following demo (use your arrow keys for movement - either up/down or left/right - you may have to click the movie to put the movie in focus):

Here is the document class:

package {
	import org.cove.ape.*;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	public class APEBridgeDemo extends Sprite {

		private static var colA:uint = 0x333333;
		private static var colB:uint = 0x336699;
		private static var colC:uint = 0x000000;
		private static var boundaryColor:uint = 0x6699aa;
		private static var carColor:uint = 0x336699;
		private static var carOutlineColor:uint = 0x000000;

		private var car:Car;

		public function APEBridgeDemo() {

			stage.frameRate = 55;

			// Initialize the engine. The argument here is the time step value.
			// Higher values scale the forces in the sim, making it appear to run
			// faster or slower. Lower values result in more accurate simulations.
			APEngine.init(1/4);

			// set up the default diplay container
			APEngine.container = this;

			// gravity -- particles of varying masses are affected the same
			APEngine.addMasslessForce(new Vector(0, 3));

			// bx:Number, by:Number, yslope:Number; bsize:Number; particleSize:Number, colB:uint, colC:uint, colD:uint
			var bridge:Bridge = new Bridge(-4, 200, 11.9, 55, 5, colA, colB, colC);
			APEngine.addGroup(bridge);

			var bridge2:Bridge = new Bridge(270, 260, .4, 55, 5, colA, colB, colC);
			APEngine.addGroup(bridge2);

			var bridge3:Bridge = new Bridge(545, 262, -80.4, 55, 5, colA, colB, colC);
			APEngine.addGroup(bridge3);

			// the border
			var boundary:Boundary = new Boundary(boundaryColor);
			APEngine.addGroup(boundary);

			car = new Car(carOutlineColor, carColor);

			APEngine.addGroup(car);

			// determine what collides with what.
			car.addCollidableList(new Array(boundary, bridge, bridge2, bridge3));

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

		function key_pressed(event:KeyboardEvent):void {
			var keySpeed:Number = 0.8;

			if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.RIGHT)
			{
				car.speed = keySpeed;
			}

			if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.LEFT)
			{
				car.speed = -keySpeed;
			}
		}

		function key_released(event:KeyboardEvent):void
		{
			car.speed = 0;
		}

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

I used the original Car.as class:

package {

	import org.cove.ape.*;

	public class Car extends Group {

		private var wheelParticleA:WheelParticle;
		private var wheelParticleB:WheelParticle;

		public function Car(colC:uint, colE:uint) {

			wheelParticleA = new WheelParticle(140,10,14,false,2);
			wheelParticleA.setStyle(0, colC, 1, colE);
			addParticle(wheelParticleA);
			wheelParticleA.sprite.cacheAsBitmap = true;

			wheelParticleB = new WheelParticle(200,10,14,false,2);
			wheelParticleB.setStyle(0, colC, 1, colE);
			addParticle(wheelParticleB);
			wheelParticleB.sprite.cacheAsBitmap = true;

			var wheelConnector:SpringConstraint = new SpringConstraint(wheelParticleA, wheelParticleB, 0.5, true, 8);
			wheelConnector.setStyle(0, colC, 1, colE);
			addConstraint(wheelConnector);
		}

		public function set speed(s:Number):void {
			wheelParticleA.angularVelocity = s;
			wheelParticleB.angularVelocity = s;
		}
	}
}

I used this Boundary class to create borders around my flash stage (so the car wouldn’t fly off the stage):

package {

	import org.cove.ape.*;

	public class Boundary extends Group {

		public function Boundary(boundaryColor:uint) {
			// RectangleParticle - x, y, width, height
			var floor:RectangleParticle = new RectangleParticle(350,299,700,1,0,true);
			floor.setStyle(0, boundaryColor, 1, boundaryColor);
			addParticle(floor);

			var ceil:RectangleParticle = new RectangleParticle(350,1,700,1,0,true);
			ceil.setStyle(0, boundaryColor, 1, boundaryColor);
			addParticle(ceil);

			var leftWall:RectangleParticle = new RectangleParticle(1,250,1,500,0,true);
			leftWall.setStyle(0, boundaryColor, 1, boundaryColor);
			addParticle(leftWall);

			var rightWall:RectangleParticle = new RectangleParticle(699,250,1,500,0,true);
			rightWall.setStyle(0, boundaryColor, 1, boundaryColor);
			addParticle(rightWall);
		}
	}
}

and I updated the Bridge.as class to allow for updating parameters such as positioning, sizing and slope of the bridge.. this should be updated to allow for the number of bridge pieces to be created, anyways here it is for now:

package {

	import org.cove.ape.*;

	public class Bridge extends Group {

		public function Bridge(bx:Number, by:Number, yslope:Number, bsize:Number, particleSize:Number, colB:uint, colC:uint, colD:uint) {	

			var bridgePAA:CircleParticle = new CircleParticle(bx,by,particleSize,true);
			bridgePAA.setStyle(1, colC, 1, colB);
			addParticle(bridgePAA);

			bx += bsize;
			by += yslope;
			var bridgePA:CircleParticle = new CircleParticle(bx,by,particleSize);
			bridgePA.setStyle(1, colC, 1, colB);
			addParticle(bridgePA);

			bx += bsize;
			by += yslope;
			var bridgePB:CircleParticle = new CircleParticle(bx,by,particleSize);
			bridgePB.setStyle(1, colC, 1, colB);
			addParticle(bridgePB);

			bx += bsize;
			by += yslope;
			var bridgePC:CircleParticle = new CircleParticle(bx,by,particleSize);
			bridgePC.setStyle(1, colC, 1, colB);
			addParticle(bridgePC);

			bx += bsize;
			by += yslope;
			var bridgePD:CircleParticle = new CircleParticle(bx,by,particleSize);
			bridgePD.setStyle(1, colC, 1, colB);
			addParticle(bridgePD);

			bx += bsize;
			by += yslope;
			var bridgePDD:CircleParticle = new CircleParticle(bx,by,particleSize,true);
			bridgePDD.setStyle(1, colC, 1, colB);
			addParticle(bridgePDD);

			var bridgeConnA:SpringConstraint = new SpringConstraint(bridgePAA, bridgePA,
					0.9, true, 10, 0.8);

			// collision response on the bridgeConnA will be ignored on
			// on the first 1/4 of the constraint. this avoids blow ups
			// particular to springcontraints that have 1 fixed particle.
			bridgeConnA.fixedEndLimit = 0.25;
			bridgeConnA.setStyle(1, colC, 1, colB);
			addConstraint(bridgeConnA);

			var bridgeConnB:SpringConstraint = new SpringConstraint(bridgePA, bridgePB,
					0.9, true, 10, 0.8);
			bridgeConnB.setStyle(1, colC, 1, colB);
			addConstraint(bridgeConnB);

			var bridgeConnC:SpringConstraint = new SpringConstraint(bridgePB, bridgePC,
					0.9, true, 10, 0.8);
			bridgeConnC.setStyle(1, colC, 1, colB);
			addConstraint(bridgeConnC);

			var bridgeConnD:SpringConstraint = new SpringConstraint(bridgePC, bridgePD,
					0.9, true, 10, 0.8);
			bridgeConnD.setStyle(1, colC, 1, colB);
			addConstraint(bridgeConnD);

			var bridgeConnE:SpringConstraint = new SpringConstraint(bridgePD, bridgePDD,
					0.9, true, 10, 0.8);
			bridgeConnE.fixedEndLimit = 0.25;
			bridgeConnE.setStyle(1, colC, 1, colB);
			addConstraint(bridgeConnE);
		}
	}
}

Share/Save/Bookmark

Wednesday, March 26th, 2008 at 9:45 am and is filed under APE - Actionscript Physics Engine, Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “[AS 3] AS Physics Engine (APE) - Bridge Demo”

  1. Posted by Saad 15th June, 2008 at 6:09 pm

    Can you put out the source for the files? I can’t get this to work, even with a direct copy.

  2. Posted by admin 16th June, 2008 at 12:55 pm

    Here you go!

    http://manewc.com/projects/flash/APEBridgeDemo/APEBridgeDemo.zip

    -Morgan

  3. Posted by APE Project - Step 8 - Adding a Fixed Bridge | actionscript 3 | morgan newcomb, website and flash developer, burlington, vermont 15th August, 2008 at 10:17 am

    [...] a few past experiments [1 | 2 ] I wanted to add a bridge to my movie here. For [...]

  4. Posted by yaz 16th August, 2008 at 6:55 pm

    Morgan, this is amazing. Nice work once again. And downloadable?? very nice.

Leave a reply