In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: boxed2

well test seems to work.
drag and draw boxes
I really like this
grabbed from here http://gamersgarden.com/2008/12/box2d-example-source/       --- as is including all the warnings.
im certain this stuff could be the basis for some nice stuff
Get Adobe Flash player
by h_jayesh 18 Jun 2009
// forked from h_jayesh's forked from: boxed2
// forked from h_jayesh's boxed2
// forked from h_jayesh's forked from: boxed1
// forked from h_jayesh's boxed1
// forked from hjayesh's box2dtest1
//well test seems to work.
//drag and draw boxes
// I really like this
//grabbed from here http://gamersgarden.com/2008/12/box2d-example-source/       --- as is including all the warnings.
//im certain this stuff could be the basis for some nice stuff


package
{
        
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
 
	public class Main extends Sprite
	{
		public var m_dbgSprite;
		public var m_world:b2World;
		public var m_phys_scale:Number = 15.0;
		public var m_timestep:Number = 1.0/15.0;
		public var m_iterations:Number = 5.0;
 
		//initial box coordinates when we first press mouse down
		public var initX:Number = 0.0;
		public var initY:Number = 0.0;
		public var drawing:Boolean = false;
		public function Main ()
		{
			/*
			A Box2D world needs three parameters: a b2AABB, gravity
			and a Booleand deciding whether or not to let bodies sleep
			when they are not being simulated.
			This saves CPU so should always be left on :)
			*/
 

			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set (-1000,-1000);
			worldAABB.upperBound.Set (1000,1000);
			var gravity:b2Vec2 = new b2Vec2(-0.4,0.98);
			m_world = new b2World(worldAABB,gravity,true);
 
			//Setup Debug Draw - So that Box2D draws the shapes for us
			m_dbgSprite = new Sprite();
			addChild (m_dbgSprite);
			SetDebugDraw ();
 
			//Add Our Ground
			AddStaticBox (300/m_phys_scale,440/m_phys_scale,300/m_phys_scale,50/m_phys_scale);
                        AddStaticBox (200/m_phys_scale,340/m_phys_scale,50/m_phys_scale,50/m_phys_scale);
 
			addEventListener (Event.ENTER_FRAME,Update);
			stage.addEventListener (MouseEvent.MOUSE_DOWN,mousePressed);
			stage.addEventListener (MouseEvent.MOUSE_MOVE,mouseMoved);
			stage.addEventListener (MouseEvent.MOUSE_UP,mouseReleased);
		}
		public function mousePressed (e:MouseEvent)
		{
			//Store initial X and Y position
			initX = e.localX;
			initY = e.localY;
			drawing = true;
		}
		public function mouseMoved (e:MouseEvent)
		{
			if (drawing)
			{
				//Simply draw the "ghost" of the box we are about to add
				graphics.clear ();
				graphics.beginFill (0xFF0000,0.5);
				graphics.drawRect (initX,initY,e.localX-initX,e.localY-initY);
			}
		}
		public function mouseReleased (e:MouseEvent)
		{
			graphics.clear ();
			drawing = false;
 
			//Coordinates of bottom-right of box (when drawing from left to right)
			var finalX:Number = e.localX;
			var finalY:Number = e.localY;
 
			//Correct if drawing from right to left
			if (finalX < initX)
			{
				//If so, swap initX and finalX
				var tempX:Number = initX;
				initX = finalX;
				finalX = tempX;
			}
			if (finalY < initY)
			{
				//If so, swap initY and finalY
				var tempY:Number = initY;
				initY = finalY;
				finalY = tempY;
			}
			//Work out the half-width and height of the box
			var boxHalfWidth:Number = Math.abs((finalX-initX)/2);
			var boxHalfHeight:Number = Math.abs((finalY-initY)/2);
			if (boxHalfWidth > 0 && boxHalfHeight > 0)
			{
                    for (var z:Number = 0; z < 5; z++) {
                        AddBox ((finalX-boxHalfWidth)/m_phys_scale+(z*0.02),(finalY-boxHalfHeight)/m_phys_scale+(z*0.2),boxHalfWidth/m_phys_scale+(z*0.02),boxHalfHeight/m_phys_scale+(z*0.2));
}
				
                                
			}
		}
		/*
		NOTE: AddBox takes the _x,_y and halfwidth and halfheight parameters
		in METRES not PIXELS. This means when you call this function, always
		DIVIDE a pixel size by m_phys_scale to get it in meteres.
		*/
		public function AddBox (_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number)
		{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set (_x,_y);
			var boxDef:b2PolygonDef = new b2PolygonDef();
			boxDef.SetAsBox (_halfwidth,_halfheight);
			boxDef.density = 0.5;
			boxDef.friction = 0.4;
			boxDef.restitution = 0.8;
			var body:b2Body = m_world.CreateBody(bodyDef);
			body.CreateShape (boxDef);
			body.SetMassFromShapes ();
		}
		public function AddStaticBox (_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number)
		{
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position.Set (_x,_y);
			var boxDef:b2PolygonDef = new b2PolygonDef();
			boxDef.SetAsBox (_halfwidth,_halfheight);
			boxDef.density = 0.0;
			var body:b2Body = m_world.CreateBody(bodyDef);
			body.CreateShape (boxDef);
			body.SetMassFromShapes ();
		}
		public function Update (e:Event)
		{

                       if    ((Math.random()*10)==5) {   for (var z:Number = 0; z < 5; z++) {
                       AddBox (Math.random()*100/m_phys_scale+(z*0.02),Math.random()*100/m_phys_scale+(z*0.2),Math.random()*100/m_phys_scale+(z*0.02),Math.random()*100/m_phys_scale+(z*0.2));
}
                                 }
			//We need to do this to simulate physics
			m_world.Step (m_timestep,m_iterations);
		}


		public function SetDebugDraw ()
		{
			//Set Debug Draw (hidden here to reserve space in constructor.)
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			dbgDraw.m_sprite = m_dbgSprite;
			dbgDraw.m_drawScale = m_phys_scale;
			dbgDraw.m_fillAlpha = 0.8;
			dbgDraw.m_lineThickness = 2.0;
			dbgDraw.m_drawFlags = 0x0001 | 0x0002;
			m_world.SetDebugDraw (dbgDraw);
		}
	}
}