物理模拟引擎box2D-1

几个常用的概念
rigid body
shape 
constraint 
contact constraint
joint 
joint limit 
joint motor
world 
Hello Box2D
Creating a World

var worldAABB:b2AABB = new b2AABB();  
worldAABB.lowerBound.Set(-100.0, -100.0);  
worldAABB.upperBound.Set(100.0, 100.0);




Caution
The world AABB should always be bigger then the region where your bodies are located. It is better to make the world AABB too big than too small. If a body reaches the boundary of the world AABB it will be frozen and will stop simulating.
and b2 allow object to sleep.

var gravity:b2Vec2 = new b2Vec2 (0.0, -10.0);  
var doSleep:Boolean = true; 


now  we create world object.

var world:b2World = new b2World(worldAABB, gravity, doSleep);



we can following the steps as
Creating a World--Creating a Ground Box--Creating a Dynamic Body--
Simulating the World (of Box2D). the following is source code and the attacted is flash.

package{   
       
    import flash.display.Sprite;   
    import flash.events.Event;   
    // Classes used in this example   
    import Box2D.Dynamics.*;   
    import Box2D.Collision.*;   
    import Box2D.Collision.Shapes.*;   
    import Box2D.Common.Math.*;   
  
    [SWF(width="640", height="360", backgroundColor="#000000", frameRate="30")]    
           
    public class HelloWorld extends Sprite{   
           
        [Embed(source='assets/PhysBox.swf')]      
        public var PhysBox:Class;      
        //public var FGmc:MovieClip = new FG as MovieClip;    
           
        [Embed(source='assets/PhysCircle.swf')]      
        public var PhysCircle:Class;    
           
        [Embed(source='assets/PhysGround.swf')]      
        public var PhysGround:Class;    
           
        public function HelloWorld(){   
               
               
               
            // Add event for main loop   
            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;   
            boxDef.density = 0; // static bodies require zero density   
            /*circleDef = new b2CircleDef();  
            circleDef.radius = 10;  
            circleDef.restitution = 0.2*/  
            // Add sprite to body userData   
            bodyDef.userData = new PhysGround();   
            //bodyDef.userData = new PhysCircle();   
            bodyDef.userData.width = 30 * 2 * 30;    
            bodyDef.userData.height = 30 * 2 * 3;    
            addChild(bodyDef.userData);   
            body = m_world.CreateBody(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.CreateBody(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.CreateBody(bodyDef);   
                    body.CreateShape(circleDef);   
                }   
                body.SetMassFromShapes();   
                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);   
                }   
            }   
               
        }   
           
        public var m_world:b2World;   
        public var m_iterations:int = 10;   
        public var m_timeStep:Number = 1.0/30.0;   
           
    }   
  
} 

你可能感兴趣的:(Flash)