Box2DのサンプルをFlexで動かしてみた。
Box2Dを久しぶりに触ってみました。
っと言っても、うまくいったためしがなかったので、チャレンジです。
実際の動作はこちら
http://moeten.info/flex/20080424_box2DTest1/bin-release/main.html
ソースはこちら
サンプルソースをFlex用に手直ししてます。
今回はImageコンポーネントにaddchildのがミソです。
Papervision3Dもそうだけど、this.addchildだとエラーが起きる場合はImageコンポーネントにaddchildすればほとんどOK。
なぜそうなのかはわかりません\(-o-)/
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" viewSourceURL="srcview/index.html"> <mx:Script> <![CDATA[ import flash.display.Sprite; import flash.events.Event; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; public var m_world:b2World; public var m_iterations:int = 10; public var m_timeStep:Number = 1.0/30.0; [Embed(source='PhysGround.swf')] private static var PhysGround:Class; [Embed(source='PhysBox.swf')] private static var PhysBox:Class; [Embed(source='PhysCircle.swf')] private static var PhysCircle:Class; private function init():void{ // Add event for main loop this.addEventListener(Event.ENTER_FRAME, Update, false, 0, true); // Creat world AABB var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-100.0, -100.0); worldAABB.upperBound.Set(100.0, 100.0); // Define the gravity vector var gravity:b2Vec2 = new b2Vec2(0.0, 10.0); // Allow bodies to sleep var doSleep:Boolean = true; // Construct a world object m_world = new b2World(worldAABB, gravity, doSleep); // set debug draw /*var dbgDraw:b2DebugDraw = new b2DebugDraw(); var dbgSprite:Sprite = new Sprite(); addChild(dbgSprite); dbgDraw.m_sprite = dbgSprite; dbgDraw.m_drawScale = 30.0; dbgDraw.m_fillAlpha = 0.0; dbgDraw.m_lineThickness = 1.0; dbgDraw.m_drawFlags = 0xFFFFFFFF; m_world.SetDebugDraw(dbgDraw);*/ // Vars used to create bodies var body:b2Body; var bodyDef:b2BodyDef; var boxDef:b2PolygonDef; var circleDef:b2CircleDef; // Add ground body bodyDef = new b2BodyDef(); //bodyDef.position.Set(15, 19); bodyDef.position.Set(10, 12); //bodyDef.angle = 0.1; boxDef = new b2PolygonDef(); boxDef.SetAsBox(30, 3); boxDef.friction = 0.3; /*circleDef = new b2CircleDef(); circleDef.radius = 10; circleDef.restitution = 0.2*/ // Add sprite to body userData bodyDef.userData = new PhysGround(); bodyDef.userData.width = 30 * 2 * 30; bodyDef.userData.height = 30 * 2 * 3; myImage.addChild(bodyDef.userData); body = m_world.CreateStaticBody(bodyDef); body.CreateShape(boxDef); //body.CreateShape(circleDef); body.SetMassFromShapes(); // Add some objects for (var i:int = 1; i < 10; i++){ bodyDef = new b2BodyDef(); bodyDef.position.x = Math.random() * 15 + 5; bodyDef.position.y = Math.random() * 10; var rX:Number = Math.random() + 0.5; var rY:Number = Math.random() + 0.5; // Box if (Math.random() < 0.5){ boxDef = new b2PolygonDef(); boxDef.SetAsBox(rX, rY); boxDef.density = 1.0; boxDef.friction = 0.5; boxDef.restitution = 0.2; bodyDef.userData = new PhysBox(); bodyDef.userData.width = rX * 2 * 30; bodyDef.userData.height = rY * 2 * 30; body = m_world.CreateDynamicBody(bodyDef); body.CreateShape(boxDef); } // Circle else { circleDef = new b2CircleDef(); circleDef.radius = rX; circleDef.density = 1.0; circleDef.friction = 0.5; circleDef.restitution = 0.2 bodyDef.userData = new PhysCircle(); bodyDef.userData.width = rX * 2 * 30; bodyDef.userData.height = rX * 2 * 30; body = m_world.CreateDynamicBody(bodyDef); body.CreateShape(circleDef); } body.SetMassFromShapes(); myImage.addChild(bodyDef.userData); } } public function Update(e:Event):void{ m_world.Step(m_timeStep, m_iterations); // Go through body list and update sprite positions/rotations for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){ if (bb.m_userData is Sprite){ bb.m_userData.x = bb.GetPosition().x * 30; bb.m_userData.y = bb.GetPosition().y * 30; bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI); } } } ]]> </mx:Script> <mx:Image id="myImage" x="0" y="0" width="634" height="596"/> </mx:Application>