Skip to content


APE – Driving Demo – Part I

Here is my work in progress – I’ll post my code, but I wouldn’t use it out of the box here.. this is just the groundwork for some things to come. The functions will need to be updated as this movie is processor intensive. The goal for the first round is to allow the user to drive the object infinitely to the right and create a movie that will be repeated to act as the ground, although the ground is not being repeated as this is causing higher CPU cycles.

So for now.. here is my buggy demo:

Here is the class for the floor:

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

	public class Floor extends Group
	{
		private var floor:RectangleParticle;
		private var floorimage:Loader;

		public function Floor(xFloor:Number=700, yFloor:Number=690)
		{
			floor = new RectangleParticle(xFloor,yFloor,1400,4,0,true);
			floor.setStyle(0,0,0,0x333333);
			floor.init();

			floorimage = new Loader();
			floorimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/grass.jpg"));

			floor.setDisplay(floorimage, 0, 0);

			addParticle(floor);

			if (Math.floor(Math.random() * 2) == 1)
			{
				// jump
				var jumpL:RectangleParticle = new RectangleParticle(xFloor + 400,675,100,1,-.3,true);
				addParticle(jumpL);
			}
		}

		public function get px():Number
		{
			return floor.px;
		}

		public function set px(value:Number):void
		{

			floor.px = value;
		}

		public function get center():Vector
		{
			return floor.center;
		}
	}
}

Here is the class for my car/truck:

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;

		public function Truck(colorOne:uint=0x333333, colorTwo:uint=0x333333) {

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

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

			wheelParticleA = new WheelParticle(50,30,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/DriveDemo2/wheel.png"));

			wheelParticleB = new WheelParticle(250,30,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);
			addConstraint(axelConnector);

			// A] add a particle directly above wheel A
			var suspA:CircleParticle = new CircleParticle(50,-100,7,false,10);
			suspA.mass = 0.001;
			suspA.setStyle(1, colorOne, 1, colorOne);
			addParticle(suspA);

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

			// B] add a particle directly above wheel B
			var suspB:CircleParticle = new CircleParticle(250,-100,7,false,10);
			suspB.mass = 0.001;
			suspB.setStyle(1, colorOne, 1, colorOne);
			addParticle(suspB);

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

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

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

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

		public function get px():Number {
			return wheelParticleB.px;
		}
	}
}

Here is the Document Class:

package {

	import org.cove.ape.*;

	import flash.display.Stage;
	import flash.display.Sprite;

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

	public class Main extends Sprite {
		private var truck:Truck;
		private var floor:Floor;

		private var rightBuild:uint;

		public function Main() {
			rightBuild = 1;

			stage.frameRate = 55;
			addEventListener(Event.ENTER_FRAME, run);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);  

			APEngine.init(1/4);
			APEngine.container = this;
			APEngine.addMasslessForce(new Vector(0,4));

			APEngine.constraintCollisionCycles = 10;

			truck = new Truck();

			getFloor(350,690);

			APEngine.addGroup(truck);
		}

		private function run(evt:Event):void {
			APEngine.step();
			APEngine.paint();
			APEngine.container.x = -truck.px + 350;

			if (truck.px > (700 * rightBuild))
			{
				getFloor(truck.px, 690);
				rightBuild++;
			}
		}

		private function getFloor(xfloor:Number, yfloor:Number):void
		{
			floor = new Floor(xfloor, yfloor);
			APEngine.addGroup(floor);
			truck.addCollidable(floor);
		}

		private function keyPressed(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;
            }
        }  

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

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.