I’ve created a quick demo with APE that allows the user to draw rectangle particles with the mouse. New wheel particles are created every 2 seconds. Particles are removed if they fall outside the stage area so that the demo will not leak memory.






Main.as:

	package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.Timer;
    import flash.events.TimerEvent;

	import org.cove.ape.*;

	// Draw APE Rectangle Particles
	// built with Actionscript Physics Engine v0.45
	// http://www.cove.org/ape/

	public class Main extends Sprite
	{
		private var myRectangle:RectangleParticle
		private var wheel:WheelParticle;
        private var defaultGroup:Group;

		private var beginX:Number; // mouse drag top left x position
		private var beginY:Number; // mouse drag top left y position
       	private var endX:Number; // mouse drag bottom right x position
       	private var endY:Number; // mouse drag bottom right y position  

		private const bound:Number = 600;

		public function Main()
		{
			init();
		}

		private function init():void
		{
			stage.frameRate = 65;  

            // 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);
			APEngine.damping = 0.98;

            // set the default diplay container
            APEngine.container = this;
            APEngine.addMasslessForce(new Vector(0,8));
            defaultGroup = new Group();
            defaultGroup.collideInternal = true;  

			//RectangleParticle(x, y, width, height, rotation, fixed, mass, elasticity, friction)
			var floor:RectangleParticle = new RectangleParticle(300,490, 600, 20, 0.0, true, 10, 0.4, 0.55);
			floor.setStyle(0, 0, 0, Math.random() * 0x111111);
            defaultGroup.addParticle(floor);  

            APEngine.addGroup(defaultGroup);

			addEventListener(Event.ENTER_FRAME, run);

			stage.addEventListener(MouseEvent.MOUSE_DOWN, beginDrawRect);
			stage.addEventListener(MouseEvent.MOUSE_UP, endDrawRect);

			setTimer();  // start creating wheel particles
		}

		private function beginDrawRect(m:MouseEvent):void
       	{
			beginX = mouseX;
			beginY = mouseY;
       	}

		private function endDrawRect(m:MouseEvent):void
       	{
           	endX = mouseX;
		   	endY = mouseY;
			// if we have 2 positive numbers, draw:
			if (endX - beginX > 1 && endY - beginY > 1)
			{
				drawRectangle();
			}
		}

		private function drawRectangle():void
		{
			//RectangleParticle(x, y, width, height, rotation, fixed, mass, elasticity, friction)
			// the more area a rectangle has the more mass we give it:
			myRectangle = new RectangleParticle(beginX + ( (endX - beginX)/2 ), beginY + ((endY - beginY)/2), endX - beginX, endY - beginY, 0 , false, (endX - beginX) * (endY - beginY) , 0.4, 0.1);
           	myRectangle.setStyle(0, 0, 0, Math.random() * 0xffffff);
		   	defaultGroup.addParticle(myRectangle);
		}

		private function createParticle(e:TimerEvent):void
        {
			//WheelParticle(x, y, radius, fixed, mass, elasticity, friction, traction)
			var rand:Number = Math.random();
			// the more area a wheel has the more mass we give it:
            wheel = new WheelParticle(rand * stage.stageWidth, -20, rand * 55, false, rand * 55, 0.4, 0.1);
            wheel.setStyle(0, 0, 0, Math.round(Math.random() * 0xffffff));
			defaultGroup.addParticle(wheel);
			checkForBounds();
        }

		// Check location of particles & remove if out of bounds so that we don't leak memory:
		function checkForBounds()
		{
			/* particles:Array  [read-only] The Array of all AbstractParticle
			   instances added to the AbstractCollection */
			for (var i:Number = defaultGroup.particles.length -1; i > 0 ; --i){

				/* Check to see if particles is past the 'bound' number, if is then remove it from system
				   py & px give the X & Y location of the partical, view AbstractParticle in APE api */
				if ( defaultGroup.particles[i].py > bound )
				{
					defaultGroup.removeParticle( defaultGroup.particles[i] ); // Removes an AbstractParticle from the AbstractCollection.
				}
			}
		}

		private function setTimer():void
		{
			var myTimer:Timer= new Timer(2000, 0);
            myTimer.addEventListener("timer", createParticle);
            myTimer.start();
        }

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

Comments

Leave a Reply