27May

Physics Engine

No comments

So I want to get more into APE and was hoping I could integrate it within one of my new projects. Here is just a basic animation that may evolve into something cool/useful.

Here is my Main Document class file

package {
    import flash.display.Sprite;
    import flash.events.Event;
	import flash.events.MouseEvent;
    import org.cove.ape.*;

	public class Main extends Sprite {

		private var bottle:Bottle;

		public function Main()
		{
			init();
		}

		private function init():void
		{

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

			APEngine.addMasslessForce(new Vector(0,8));

			var defaultGroup:Group = new Group();

			defaultGroup.collideInternal = true;

            var floor:RectangleParticle = new RectangleParticle(350, 600, 700, 2, 0, true);
            defaultGroup.addParticle(floor);

           	bottle = new Bottle();
			APEngine.addGroup(bottle);

			APEngine.addGroup(defaultGroup);

			// this will allow both groups to interact with one another.
			bottle.addCollidable (defaultGroup);

			addEventListener(Event.ENTER_FRAME, run);
		}

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

	}
}

and another class file for the Bottle Group

package {

	import org.cove.ape.*;

	import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;
	import flash.display.MovieClip;

	public class Bottle extends Group
	{
		private var top:WheelParticle;
		private var wheelParticleB:WheelParticle;
		private var wheelTL:WheelParticle;
		private var wheelTR:WheelParticle;

		private var wheelimage:Loader;
		private var wheelimage2:Loader;
		private var BottleImage:Loader;
		private var holder:MovieClip;

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

			// Load in the images
			BottleImage = new Loader();
			BottleImage.load(new URLRequest("http://manewc.com/projects/flash/i/Bottle.png"));

			/*
			============================
			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)

			Wheel Particle
				x:Number,
				y:Number,
				radius:Number,
				fixed:Boolean = false,
				mass:Number = 1,
				elasticity:Number = 0.3,
				friction:Number = 0,
				traction:Number = 1
			============================
			*/
			top = new WheelParticle(350,0,37,false,.8);
			top.setDisplay(BottleImage, 0, -37);
			addParticle( top );
		}
	}
}

Share/Save/Bookmark

Tuesday, May 27th, 2008 at 9:33 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.

Leave a reply