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の何か
Get Adobe Flash player
by hokori 10 May 2009
//のれんのようなBox2Dの何か
package  
{
	import Box2D.Dynamics.b2World;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef
	import Box2D.Dynamics.Joints.b2Joint;
	import Box2D.Dynamics.Joints.b2DistanceJoint;
	import Box2D.Dynamics.Joints.b2DistanceJointDef;
	import Box2D.Dynamics.Joints.b2JointEdge;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Collision.b2AABB;
	import Box2D.Collision.Shapes.b2CircleDef;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Common.Math.b2Vec2;

	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.Graphics;

	public class Sample04 extends Sprite
	{
		private var m_disp_grp:Sprite;
		private var m_color:int;
		
		public function Sample04() 
		{	
			//Box2D初期設定
			this.B2Base();
			
			m_disp_grp = new Sprite();
			this.addChild(m_disp_grp);
			
			m_color = Math.random() * 0x00FFFF;
			
			this.createNoren(100, 0, 1);
			this.createNoren(250, -50, 1);
			this.createNoren(350, -50, -1);

			this.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
		}
		
		//距離ジョイントで繋げたものを揺らす
		private function createNoren(x:int, y:int, set_to:int):void {

			var bodies:Array = new Array();

			var density:int = 0;
			for (var i:int = 0; i < 5; i++) {
				
				if(i != 0){
					var len_max:int = Math.random() * 30 + 10;
					var angle:Number =  (360 / (len_max * 0.5))  / 180 * Math.PI
					var disp:Sprite = new Sprite();
					disp.graphics.lineStyle(1, m_color);
					for (var l:int = 0; l < len_max * 0.5; l++){
						disp.graphics.moveTo(0, 0);
						disp.graphics.lineTo(Math.cos(angle * l) * len_max, Math.sin(angle * l) * len_max);
					}
				}			
				var body:b2Body = this.makeB2BodyCircle(20 , x, y, density, 1, 1, disp , m_disp_grp);
				bodies[i] = body;
				
				y += (50 + Math.random() * 100) * set_to;
				density = 1;
			}
			
			//距離ジョイントで繋ぐ
			for (var j:int = 0; j < 4; j++){
				var d_joint_def:b2DistanceJointDef = new b2DistanceJointDef();
				d_joint_def.collideConnected = true;
				d_joint_def.Initialize(bodies[j], bodies[j + 1], bodies[j].GetPosition(), bodies[j + 1].GetPosition());
				this.m_b2_world.CreateJoint(d_joint_def);
			}
			
			//ちょっと力を加えて動かす
			bodies[4].ApplyForce(new b2Vec2(1000, 0), bodies[4].GetPosition());
		}

		private function update(e:Event):void{

			var self:Sample04 = this;
			
			this.m_b2_world.Step(this.m_b2_timeStep, this.m_b2_iterations);

			var disp_gra:Graphics = this.m_disp_grp.graphics;
			disp_gra.clear();
			disp_gra.lineStyle(1, m_color);
			
			for (var bb:b2Body = this.m_b2_world.m_bodyList; bb; bb = bb.m_next){

				if (bb.m_userData is Sprite){
					bb.m_userData.x = bb.GetPosition().x * m_b2_physcale;
					bb.m_userData.y = bb.GetPosition().y * m_b2_physcale;
					bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
				}
				
				//ジョイントの線
				if (bb.m_jointList is b2JointEdge) {
					for (var joint:b2Joint = bb.m_jointList.joint; joint; joint=joint.m_next){
						var anc1:b2Vec2 = joint.GetAnchor1();
						var anc2:b2Vec2 = joint.GetAnchor2();
						disp_gra.moveTo(anc1.x * this.m_b2_physcale, anc1.y * this.m_b2_physcale);
						disp_gra.lineTo(anc2.x * this.m_b2_physcale, anc2.y * this.m_b2_physcale);
					}
				}
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		//Box2D用まとめ
		//wonderfl上でクラスを複数置く方法がわからない...。
		//-----------------------------------------------------------------------------------
		protected var m_b2_world:b2World;
		protected var m_b2_physcale:Number = 10;
		protected var m_b2_iterations:int = 10; 
		protected var m_b2_timeStep:Number = 24.0; 

		public function B2Base(fps:int = 24, physcale:Number = 10, gravity:Number = 10):void{
			
			this.m_b2_timeStep = 1.0 / fps;
			this.m_b2_physcale = physcale;
			this.createB2World(gravity);			
		}

		private function createB2World(gravity:Number):void {
			
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-1000, -1000);
			worldAABB.upperBound.Set(1000, 1000);
			
			var vec_gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
			
			var doSleep:Boolean = true;
			
			this.m_b2_world = new b2World(worldAABB, vec_gravity, doSleep);
		}

		protected function makeB2BodyBox(w:int, h:int, x:Number, y:Number, angle:Number, 
									  density:Number, friction:Number, restitution:Number,
									  viewdata:Sprite = null, viewgroup:Sprite = null) :b2Body {
			
			var b2_boxdef:b2PolygonDef = this.createB2BoxDef(w, h, density, friction, restitution);
			var b2_bodydef:b2BodyDef = this.createB2BodyDef(x, y, angle, viewdata, viewgroup);
			
			var b2_body:b2Body = this.m_b2_world.CreateBody(b2_bodydef);
			b2_body.CreateShape(b2_boxdef);
			b2_body.SetMassFromShapes();
			
			return b2_body;
		}	

		protected function makeB2BodyCircle(radius:int, x:Number, y:Number, 
									  density:Number, friction:Number, restitution:Number,
									  viewdata:Sprite = null, viewgroup:Sprite = null) :b2Body {
			
			var b2_boxdef:b2CircleDef = this.createB2CircleDef(radius, density, friction, restitution);
			var b2_bodydef:b2BodyDef = this.createB2BodyDef(x, y, 0, viewdata, viewgroup);
			
			var b2_body:b2Body = this.m_b2_world.CreateBody(b2_bodydef);
			b2_body.CreateShape(b2_boxdef);
			b2_body.SetMassFromShapes();
			
			return b2_body;
		}

		private function createB2BoxDef(width:int, height:int, 
										density:Number, friction:Number, restitution:Number):b2PolygonDef {
			
			var boxdef:b2PolygonDef = new b2PolygonDef();

			boxdef.SetAsBox(width / 2 / this.m_b2_physcale, height/ 2 / this.m_b2_physcale);
			boxdef.density = density; 
			boxdef.friction = friction;
			boxdef.restitution = restitution;
			
			return boxdef;
		}
	
		private function createB2CircleDef(radius:int, density:Number, friction:Number, restitution:Number):b2CircleDef {
			
			var cdef:b2CircleDef = new b2CircleDef();
			
			cdef.radius = radius / 2 / this.m_b2_physcale;
			cdef.density = density;
			cdef.friction = friction; 
			cdef.restitution = restitution;
			
			return cdef;
		}

		private function createB2BodyDef(x:Number, y:Number, angle:Number, viewdata:Sprite = null, viewgroup:Sprite = null):b2BodyDef {
			
			var bodydef:b2BodyDef = new b2BodyDef();

			bodydef.position.Set(x / this.m_b2_physcale, y / this.m_b2_physcale);
			bodydef.angle = (angle / 180) * Math.PI; 
			
			bodydef.allowSleep = true; 
			
			if(viewdata != null){
				bodydef.userData = viewdata;			
				bodydef.userData.width = viewdata.width;
				bodydef.userData.height = viewdata.height;
			
				if(viewgroup != null){
					viewgroup.addChild(bodydef.userData);
				}
			}
			
			return bodydef;			
		}

		protected function createB2Vec(x:Number, y:Number):b2Vec2 {
			
			var vec:b2Vec2 = new b2Vec2(x / this.m_b2_physcale, y / this.m_b2_physcale); 
			return vec;
		}
	}
}