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

[Box2D] 2009-4-22

[Box2D]
Get Adobe Flash player
by XELF 23 Apr 2009
// [Box2D]
package {
	import flash.display.Sprite;
	import flash.events.Event;

	[SWF(width = 465, height = 465, backgroundColor = 0x20a090, frameRate = 60)]
	public class Intro extends Sprite {
		public function Intro() {
			addEventListener(Event.ADDED_TO_STAGE, initialize);
		}
		public function initialize(event:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, initialize);
			var main:Main = new Main(stage);
			addChild(main);
		}
	}
}

import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;

import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;

class Box2DMain extends Sprite {
	public var layer:Stage;
	
	public var m_sprite:Sprite;
	public var m_world:b2World;
	public var m_physScale:Number = 30.0;
	public var m_iterations:int = 100;
	public var m_timeStep:Number = 1.0 / 60.0;

	public var m_mouseJoint:b2MouseJoint;

	static public var mouseXWorldPhys:Number;
	static public var mouseYWorldPhys:Number;
	static public var mouseXWorld:Number;
	static public var mouseYWorld:Number;

	private var mousePVec:b2Vec2 = new b2Vec2();
	private var mouseDown:Boolean = false;

	public function Box2DMain(stg:Stage) {
		layer = stg;
		initialize();
	}
	
	public function initialize():void {
		removeEventListener(Event.ADDED_TO_STAGE, initialize);

		//コンテナ
		m_sprite = new Sprite();
		addChild(m_sprite);
		
		addEventListener(Event.ENTER_FRAME, update);
		
		addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		addEventListener(MouseEvent.CLICK, onMouseUp);
		addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		addEventListener(Event.MOUSE_LEAVE, onMouseLeave);

		//WORLD
		var worldAABB:b2AABB = new b2AABB();
		worldAABB.lowerBound.Set(-1000.0, -1000.0);
		worldAABB.upperBound.Set(+1000.0, +1000.0);
		var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
		var doSleep:Boolean = true;
		m_world = new b2World(worldAABB, gravity, doSleep);

		//デバック用
		var dbgDraw:b2DebugDraw= new b2DebugDraw();
		var dbgSprite:Sprite= new Sprite();
		m_sprite.addChild(dbgSprite);
		dbgDraw.m_sprite= dbgSprite;
		dbgDraw.m_drawScale= 30.0;
		dbgDraw.m_fillAlpha= 0.3;
		dbgDraw.m_lineThickness= 1.0;
		dbgDraw.m_alpha=1.0;
		dbgDraw.m_xformScale=1.0;
		dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit;
		m_world.SetDebugDraw(dbgDraw);

		createWall();
		createObject();
	}
	
	public function createObject():void {
	}
	
	private function createWall():void {
		var wallObject:b2PolygonDef = new b2PolygonDef();
		wallObject.density = 0.0;
		var wallDef:b2BodyDef = new b2BodyDef();
		var wallBody:b2Body;
		
		//Left
		wallDef.position.Set(10/2 / m_physScale, layer.stageWidth/2 / m_physScale);
		wallObject.SetAsBox(10/2 / m_physScale, layer.stageWidth/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Right
		wallDef.position.Set((layer.stageWidth-10/2) / m_physScale, layer.stageWidth/2 / m_physScale);
		wallObject.SetAsBox(10/2 / m_physScale, layer.stageWidth/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Top
		wallDef.position.Set(layer.stageWidth/2 / m_physScale, 10/2 / m_physScale);
		wallObject.SetAsBox(layer.stageWidth/2 / m_physScale, 10/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();

		//Bottom
		wallDef.position.Set(layer.stageWidth/2 / m_physScale, (layer.stageWidth-10/2) / m_physScale);
		wallObject.SetAsBox(layer.stageWidth/2 / m_physScale, 10/2 / m_physScale);
		wallBody = m_world.CreateBody(wallDef);
		wallBody.CreateShape(wallObject);
		wallBody.SetMassFromShapes();
	}
			
	private function onMouseDown(event:MouseEvent):void {
		mouseDown = true;
	}
	
	private function onMouseUp(event:MouseEvent):void {
		mouseDown = false;
	}
	
	private function onMouseMove(event:MouseEvent):void {
		if (mouseDown != event.buttonDown){
			mouseDown = event.buttonDown;
		}
	}
	
	public function onMouseLeave(e:Event):void {
		mouseDown = false;
		MouseDrag();
	}
	
	private function update(event:Event):void {
		m_world.Step(m_timeStep, m_iterations);
		m_sprite.graphics.clear();
		
		UpdateMouseWorld();
		MouseDrag();
	}
	
	public function UpdateMouseWorld():void {
		mouseXWorldPhys = (m_sprite.mouseX)/m_physScale; 
		mouseYWorldPhys = (m_sprite.mouseY)/m_physScale; 
		
		mouseXWorld = (m_sprite.mouseX); 
		mouseYWorld = (m_sprite.mouseY); 
	}
	
	public function MouseDrag():void {
		// mouse press
		if (mouseDown && !m_mouseJoint) {
			var body:b2Body = GetBodyAtMouse();
			if (body) {
				var md:b2MouseJointDef = new b2MouseJointDef();
				md.body1 = m_world.GetGroundBody();
				md.body2 = body;
				md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
				md.maxForce = 300.0 * body.GetMass();
				md.timeStep = m_timeStep;
				m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
				body.WakeUp();
			}
		}
		
		// mouse release
		if (!mouseDown) {
			if (m_mouseJoint) {
				m_world.DestroyJoint(m_mouseJoint);
				m_mouseJoint = null;
			}
		}
		
		// mouse move
		if (m_mouseJoint) {
			var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
			m_mouseJoint.SetTarget(p2);
		}
	}
	
	public function GetBodyAtMouse(includeStatic:Boolean = false):b2Body {
		// Make a small box.
		mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
		var aabb:b2AABB = new b2AABB();
		aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
		aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);

		// Query the world for overlapping shapes.
		var k_maxCount:int = 10;
		var shapes:Array = new Array();
		var count:int = m_world.Query(aabb, shapes, k_maxCount);
		var body:b2Body = null;
		for (var i:int = 0; i < count; ++i) {
			if (shapes[i].GetBody().IsStatic() == false || includeStatic) {
				var tShape:b2Shape = shapes[i] as b2Shape;
				var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
				if (inside) {
					body = tShape.GetBody();
					break;
				}
			}
		}
		return body;
	}
}

class Main extends Box2DMain {
	public function Main(stage:Stage) {
		super(stage);
	}

 	public function CreateBall(x:Number, y:Number, radius:Number, density:Number,
            groupIndex:int = -1):b2Body {
            var d:b2BodyDef = new b2BodyDef();
            d.position.Set(x / m_physScale, y / m_physScale);
            var body:b2Body = m_world.CreateBody(d);
            var shape:b2CircleDef = new b2CircleDef();
            shape.radius = radius / m_physScale;
            shape.density = density;
            shape.filter.groupIndex = groupIndex;
            body.CreateShape(shape);
            body.SetMassFromShapes();
            return body;
        }
        
	public function CreateBox(x:Number, y:Number, width:Number, height:Number, density:Number,
            groupIndex:int = -1):b2Body {
            var d:b2BodyDef = new b2BodyDef();
            d.position.Set(x / m_physScale, y / m_physScale);
            var body:b2Body = m_world.CreateBody(d);
            var shape:b2PolygonDef = new b2PolygonDef();
            shape.SetAsBox(width / m_physScale, height / m_physScale);
            shape.density = density;
            shape.filter.groupIndex = groupIndex;
            body.CreateShape(shape);
            body.SetMassFromShapes();
            return body;
        }
        
	public function CreateMotor(body1:b2Body, body2:b2Body, x:Number, y:Number,
                maximumTorque:Number, speed:Number):b2Joint {
		var d:b2RevoluteJointDef = new b2RevoluteJointDef();
                d.maxMotorTorque = maximumTorque;
                d.motorSpeed = speed;
                d.enableMotor = true;
		d.Initialize(body1, body2, new b2Vec2(x / m_physScale, y / m_physScale));
		return m_world.CreateJoint(d);
	}

	public function CreateRevoluteJoint(body1:b2Body, body2:b2Body, x:Number, y:Number):b2Joint {
		var d:b2RevoluteJointDef = new b2RevoluteJointDef();
		d.Initialize(body1, body2, new b2Vec2(x / m_physScale, y / m_physScale));
		return m_world.CreateJoint(d);
	}

	override public function createObject():void {
            for (var i:uint = 0; i < 2; i++){
                
                var g:int = -8 - i;

                var ball :b2Body = CreateBall(100, 100, 30, 1, g);
                var hip  :b2Body = CreateBox(110, 100, 40, 40, 5, g);
                
                var ul1l :b2Body = CreateBox(095, 150, 20, 50, 10, g);
                var ul2l :b2Body = CreateBox(080, 150, 05, 50, 10, g);
                var lll  :b2Body = CreateBox(100, 250, 13, 50, 10, g);
                var bl   :b2Body = CreateBox(075, 100, 05, 10, 01, g);
                var fl   :b2Body = CreateBox(110, 300, 35, 10, 50, g);
                
                var ul1r :b2Body = CreateBox(095, 150, 20, 50, 10, g);
                var ul2r :b2Body = CreateBox(080, 150, 05, 50, 10, g);
                var llr  :b2Body = CreateBox(100, 250, 13, 50, 10, g);
                var br   :b2Body = CreateBox(075, 100, 05, 10, 01, g);
                var fr   :b2Body = CreateBox(110, 300, 35, 10, 50, g);

                var motor:b2Joint = CreateMotor        (ball,hip , 100, 100, 1000, 5);
                var hipl :b2Joint = CreateRevoluteJoint(hip, ul1l , 100, 105);
                var hipr :b2Joint = CreateRevoluteJoint(hip, ul1r , 100, 105);

                var jl   :b2Joint = CreateRevoluteJoint(hip , bl  , 060, 100);
                var kn1l :b2Joint = CreateRevoluteJoint(ul1l, lll , 110, 200);
                var kn2l :b2Joint = CreateRevoluteJoint(ul2l, lll , 060, 200);
                var hip2l:b2Joint = CreateRevoluteJoint(ball, ul2l, 060, 100);
                var an1l :b2Joint = CreateRevoluteJoint(lll , fl  , 100, 295);
                var an2l :b2Joint = CreateRevoluteJoint(lll , fl  , 120, 305);
                
                var jr   :b2Joint = CreateRevoluteJoint(hip , br  , 140, 100);
                var kn1r :b2Joint = CreateRevoluteJoint(ul1r, llr , 110, 200);
                var kn2r :b2Joint = CreateRevoluteJoint(ul2r, llr , 060, 200);
                var hip2r:b2Joint = CreateRevoluteJoint(ball, ul2r, 140, 100);
                var an1r :b2Joint = CreateRevoluteJoint(llr , fr  , 100, 295);
                var an2r :b2Joint = CreateRevoluteJoint(llr , fr  , 120, 305);
            }
	}
}