flash on 2009-10-22

by mtok
♥0 | Line 229 | Modified 2009-10-22 06:47:14 | MIT License
play

ActionScript3 source code

/**
 * Copyright mtok ( http://wonderfl.net/user/mtok )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/3w3o
 */

package {
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            
        }
    }
}package  
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.box2dflash.collision.AABB;
	import org.box2dflash.collision.shapes.CircleDef;
	import org.box2dflash.collision.shapes.MassData;
	import org.box2dflash.collision.shapes.PolygonDef;
	import org.box2dflash.collision.shapes.Shape
	import org.box2dflash.collision.shapes.ShapeDef;
	import org.box2dflash.common.math.Vec2;
	import org.box2dflash.dynamics.Body;
	import org.box2dflash.dynamics.BodyDef;
	import org.box2dflash.dynamics.DebugDraw;
	import org.box2dflash.dynamics.joints.DistanceJointDef;
	import org.box2dflash.dynamics.joints.MouseJoint;
	import org.box2dflash.dynamics.joints.MouseJointDef;
	import org.box2dflash.dynamics.World;
	/**
	 * ...
	 * @author ...
	 */
	public class Box2DTest extends Sprite
	{
		private var _world:World;
		private var _isDebugDrawing:Boolean = true;
		private var _timeStep:Number;
		private var _isMouseDown:Boolean;
		private var _mouseJoint:MouseJoint;
		private var _mousePVec:Vec2  = new Vec2();
		private var _iterations:int = 10;
		public function Box2DTest() 
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}

		private function addedToStageHandler(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			initialize();
		}

		private function initialize():void {
			_timeStep = 1 / stage.frameRate;
			_world = buildWorld();
			buildScene(_world);

			setUpDebugDraw(_world);

			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function enterFrameHandler(e:Event):void 
		{
			mouseDrag();
			_world.step(_timeStep, _iterations);
		}
		
		private function mouseDrag():void
		{
			var body:Body;
			if (_isMouseDown && !_mouseJoint) {
				body = getBodyAtMouse();
				if (body) {
					var md:MouseJointDef = new MouseJointDef();
					md.body1 = _world.groundBody;
					md.body2 = body;
					md.target.x = mouseX;
					md.target.y = mouseY;
					md.maxForce = 300 * body.mass;
					md.timeStep = _timeStep;
					_mouseJoint = _world.createJoint(md) as MouseJoint;
					body.wakeUp();
				}
			}
			if (!_isMouseDown) {
				if (_mouseJoint) {
					_world.destroyJoint(_mouseJoint);
					_mouseJoint = null;
				}
			}
			if (_mouseJoint) {
				_mouseJoint.m_target.x = mouseX;
				_mouseJoint.m_target.y = mouseY;
			}
		}
		
		private function getBodyAtMouse(isStaticIncluded:Boolean = false):Body
		{
			_mousePVec.x = mouseX;
			_mousePVec.y = mouseY;
			
			var aabb:AABB = new AABB();
			aabb.lowerBound.x = mouseX - 0.5;
			aabb.lowerBound.y = mouseY - 0.5;
			aabb.upperBound.x = mouseX + 0.5;
			aabb.upperBound.y = mouseY + 0.5;
			
			var maxCount:int = 10;
			var shapes:Array = new Array();
			var count:int = _world.query(aabb, shapes, maxCount);
			var body:Body = null;
			var shape:Shape;
			for (var i:int = 0; i < count; i++) {
				shape = shapes[i] as Shape;
				if (shape.body.isStatic == false || isStaticIncluded) {
					var inside:Boolean = shape.testPoint(shape.body.getXForm(), _mousePVec);
					if (inside) {
						body = shape.body;
						break;
					}
				}
			}
			return body;
		}
		
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			_isMouseDown = false;
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			_isMouseDown = true;
		}
		
		private function setUpDebugDraw(world:World):void
		{
			if (_isDebugDrawing) {
				var debugDraw:DebugDraw = new DebugDraw();
				addChild(debugDraw.m_sprite = new Sprite());
				debugDraw.m_drawScale = 1;
				debugDraw.m_fillAlpha = 0.25;
				debugDraw.m_lineThickness = 1;
				debugDraw.m_drawFlags = /*DebugDraw.e_aabbBit | DebugDraw.e_centerOfMassBit | DebugDraw.e_coreShapeBit | DebugDraw.e_obbBit | DebugDraw.e_pairBit |*/ DebugDraw.e_shapeBit| DebugDraw.e_jointBit ;
				world.debugDraw = debugDraw;
			}
		}
		
		private function buildScene(world:World):void
		{
			buildWalls(world);
			buildShapes(world);
			buildDistanceJoint(world);
		}
		
		private function buildDistanceJoint(world:World):void
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			var boxBodyDef:BodyDef = new BodyDef();
			boxBodyDef.position.x = sw * 0.8;
			boxBodyDef.position.y = sh * 0.5;
			
			var box:Body = world.createBody(boxBodyDef);
			
			var boxShape:PolygonDef = new PolygonDef();
			boxShape.setAsBox(40, 40);
			boxShape.density = 0.7;
			boxShape.friction = 0.3;
			boxShape.restitution = 0.4;
			
			box.createShape(boxShape);
			
			var massData:MassData = new MassData();
			massData.mass = 100;
			
			box.setMass(massData);
			
			var jointDef:DistanceJointDef = new DistanceJointDef();
			var ancher:Vec2 = new Vec2();
			ancher.x = sw * 0.5;
			ancher.y = 0;
			
			jointDef.initialize(box, world.groundBody, box.worldCenter, ancher);
			
			world.createJoint(jointDef);
			box.linearVelocity.x = 100;
			box.linearVelocity.y = 0;
		}
		
		private function buildShapes(world:World):void
		{
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			var bodyDef:BodyDef;
			var body:Body;
			var shapeDef:CircleDef;
			for (var i:int = 0; i < 50; i++) {
				bodyDef = new BodyDef();
				bodyDef.position.x = sw * Math.random();
				bodyDef.position.y = sh * Math.random();
				
				body = world.createBody(bodyDef);
			
				shapeDef = new CircleDef();
				shapeDef.radius = Math.random() * 15 + 3;
				shapeDef.density = 1;
				shapeDef.friction = 0.3;
				shapeDef.restitution = 0.7;
				body.createShape(shapeDef);
				body.setMassFromShapes();
			}
		}
		
		private function buildWorld():World
		{
			var gravity:Vec2 = new Vec2(0, 10);
			var worldAABB:AABB = new AABB();
			var sleep:Boolean = true;
			worldAABB.lowerBound.x = 0;
			worldAABB.lowerBound.y = 0;
			worldAABB.upperBound.x = 465;
			worldAABB.upperBound.y = 465;
			var world:World = new World(worldAABB, gravity, sleep);
			return world;
		}
		
		private function buildWalls(world:World):void
		{
			var wallShapeDef:PolygonDef = new PolygonDef();
			var wallBodyDef:BodyDef = new BodyDef();
			var wall:Body;
			
			var sw:Number = stage.stageWidth;
			var sh:Number = stage.stageHeight;
			
			//右と左の壁に使うシェイプを定義
			wallShapeDef.setAsBox(100, (sh + 40) * 0.5);

			//左
			wallBodyDef.position.x = -95;
			wallBodyDef.position.y = sh * 0.5;
			wall = world.createBody(wallBodyDef);
			wall.createShape(wallShapeDef);
			wall.setMassFromShapes();

			//右
			wallBodyDef.position.x = sw + 95;
			wallBodyDef.position.y = sh * 0.5;
			wall = world.createBody(wallBodyDef);
			wall.createShape(wallShapeDef);
			wall.setMassFromShapes();
			
			//上と下の壁に使うシェイプを定義
			wallShapeDef.setAsBox((sw+40) * 0.5, 100);
			
			//下
			wallBodyDef.position.x = (sw+40) * 0.5;
			wallBodyDef.position.y = -95;
			wall = world.createBody(wallBodyDef);
			wall.createShape(wallShapeDef);
			wall.setMassFromShapes();
			
			//下
			wallBodyDef.position.x = (sw+40) * 0.5;
			wallBodyDef.position.y = sh + 95;
			wall = world.createBody(wallBodyDef);
			wall.createShape(wallShapeDef);
			wall.setMassFromShapes();
		}
	}
}