Skip to content


Truck Design with APE

So I found this truck image and then manipulated it to use on my vehicle object in APE project.

Here is my Main.as document class file:

package {
	import org.cove.ape.*;

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

	public class Main 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 truck:Truck;

		public function Main() {

			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));

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

			truck = new Truck(carOutlineColor, carColor);

			APEngine.addGroup(truck);

			// determine what collides with what.
			truck.addCollidableList(new Array(boundary));

			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)
			{
				truck.speed = keySpeed;
			}

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

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

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

The original boundary class file

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 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);
		}
	}
}

The truck.as class file I used to create the springs and load in the images:

package {

	import org.cove.ape.*;
	import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;  

	public class Truck extends Group {

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

		private var wheelAimage:Loader;
		private var wheelBimage:Loader;
		private var truckImage:Loader;

		public function Truck(colorOne:uint, colorTwo:uint) {

			// Load in the image
            wheelAimage = new Loader();  

            wheelAimage.load(new URLRequest("http://manewc.com/projects/flash/APETruck/wheel.png"));

			wheelParticleA = new WheelParticle(30,0,50,false,2);

			/*
			============================
			Assigns a DisplayObject to be used when painting this particle.
			Parameters

				d:DisplayObject
				offsetX:Number (default = 0)
 				offsetY:Number (default = 0)
 				rotation:Number (default = 0)
			============================
			*/

			wheelParticleA.setDisplay(wheelAimage, -50, -50);

			addParticle(wheelParticleA);

			// Load in the image
            wheelBimage = new Loader();
            wheelBimage.load(new URLRequest("http://manewc.com/projects/flash/APETruck/wheel.png"));

			wheelParticleB = new WheelParticle(270,0,50,false,2);

			/*
			============================
			Assigns a DisplayObject to be used when painting this particle.
			Parameters

				d:DisplayObject
				offsetX:Number (default = 0)
 				offsetY:Number (default = 0)
 				rotation:Number (default = 0)
			============================
			*/

			wheelParticleB.setDisplay(wheelBimage, -50, -50);

			addParticle(wheelParticleB);

			// connect the front and rear wheels
			var axelConnector:SpringConstraint = new SpringConstraint(wheelParticleA, wheelParticleB, .7, false, 8);
			axelConnector.setStyle(0, colorOne, 1, colorTwo);
			axelConnector.visible = false;
			addConstraint(axelConnector);

			// A] add a particle to left wheel A
			var suspA:CircleParticle = new CircleParticle(0,-80,7,false,10);
			suspA.mass = 0.001;
			suspA.setStyle(1, colorOne, 1, colorOne);
			suspA.visible = false;
			addParticle(suspA);

			// connect particle [A] to wheel A
			var shockAConnector:SpringConstraint = new SpringConstraint(wheelParticleA, suspA, .6, false, 8);
			shockAConnector.setStyle(0, colorOne, 1, colorTwo);
			shockAConnector.visible = false;
			addConstraint(shockAConnector);

			// B] add a particle the right wheel B
			var suspB:CircleParticle = new CircleParticle(300,-90,7,false,10);
			suspB.mass = 0.001;
			suspB.setStyle(1, colorOne, 1, colorOne);
			suspB.visible = false;
			addParticle(suspB);

			// connect particle [B] to wheel B
			var shockBConnector:SpringConstraint = new SpringConstraint(wheelParticleB, suspB, .6, false, 8);
			shockBConnector.setStyle(0, colorOne, 1, colorTwo);
			shockBConnector.visible = false;
			addConstraint(shockBConnector);

			// connect shockAConnector to Wheel B
			var wheelBConnector:SpringConstraint = new SpringConstraint(wheelParticleB, suspA, 0.5, false, 8);
			wheelBConnector.setStyle(0, colorOne, 1, colorTwo);
			wheelBConnector.visible = false;
			addConstraint(wheelBConnector);

			// connect shockBConnector to Wheel A
			var wheelAConnector:SpringConstraint = new SpringConstraint(wheelParticleA, suspB, .7, false, 8);
			wheelAConnector.setStyle(0, colorOne, 1, colorTwo);
			wheelAConnector.visible = false;
			addConstraint(wheelAConnector);

			// connect the top 2 shocks and add the body of the truck
			truckImage = new Loader();
            truckImage.load(new URLRequest("http://manewc.com/projects/flash/APETruck/truckbodySmall.png"));
			var truckConnector:SpringConstraint = new SpringConstraint(suspA, suspB, .7, false, 8);
			addConstraint(truckConnector);
			truckConnector.setDisplay(truckImage, -210, -80);
		}

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

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.