home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / pup_idol.swf / scripts / Box2D / Dynamics / b2World.as < prev    next >
Encoding:
Text File  |  2008-08-07  |  13.6 KB  |  428 lines

  1. package Box2D.Dynamics
  2. {
  3.    import Box2D.Collision.Shapes.b2Shape;
  4.    import Box2D.Collision.b2AABB;
  5.    import Box2D.Collision.b2BroadPhase;
  6.    import Box2D.Common.Math.b2Math;
  7.    import Box2D.Common.Math.b2Vec2;
  8.    import Box2D.Dynamics.Contacts.b2Contact;
  9.    import Box2D.Dynamics.Contacts.b2ContactNode;
  10.    import Box2D.Dynamics.Joints.b2Joint;
  11.    import Box2D.Dynamics.Joints.b2JointDef;
  12.    import Box2D.Dynamics.Joints.b2JointNode;
  13.    
  14.    public class b2World
  15.    {
  16.       
  17.       public static var s_enableWarmStarting:int = 1;
  18.       
  19.       public static var s_enablePositionCorrection:int = 1;
  20.        
  21.       
  22.       public var m_bodyCount:int;
  23.       
  24.       public var m_gravity:b2Vec2;
  25.       
  26.       public var m_listener:b2WorldListener;
  27.       
  28.       public var m_filter:b2CollisionFilter;
  29.       
  30.       private var step:b2TimeStep;
  31.       
  32.       public var m_positionIterationCount:int;
  33.       
  34.       public var m_contactList:b2Contact;
  35.       
  36.       public var m_blockAllocator:*;
  37.       
  38.       public var m_groundBody:b2Body;
  39.       
  40.       public var m_contactCount:int;
  41.       
  42.       public var m_broadPhase:b2BroadPhase;
  43.       
  44.       public var m_allowSleep:Boolean;
  45.       
  46.       public var m_stackAllocator:*;
  47.       
  48.       public var m_jointCount:int;
  49.       
  50.       public var m_bodyList:b2Body;
  51.       
  52.       public var m_bodyDestroyList:b2Body;
  53.       
  54.       public var m_jointList:b2Joint;
  55.       
  56.       public var m_contactManager:b2ContactManager;
  57.       
  58.       public function b2World(param1:b2AABB, param2:b2Vec2, param3:Boolean)
  59.       {
  60.          step = new b2TimeStep();
  61.          m_contactManager = new b2ContactManager();
  62.          super();
  63.          m_listener = null;
  64.          m_filter = b2CollisionFilter.b2_defaultFilter;
  65.          m_bodyList = null;
  66.          m_contactList = null;
  67.          m_jointList = null;
  68.          m_bodyCount = 0;
  69.          m_contactCount = 0;
  70.          m_jointCount = 0;
  71.          m_bodyDestroyList = null;
  72.          m_allowSleep = param3;
  73.          m_gravity = param2;
  74.          m_contactManager.m_world = this;
  75.          m_broadPhase = new b2BroadPhase(param1,m_contactManager);
  76.          var _loc4_:b2BodyDef = new b2BodyDef();
  77.          m_groundBody = CreateBody(_loc4_);
  78.       }
  79.       
  80.       public function GetContactList() : b2Contact
  81.       {
  82.          return m_contactList;
  83.       }
  84.       
  85.       public function CreateJoint(param1:b2JointDef) : b2Joint
  86.       {
  87.          var _loc3_:b2Body = null;
  88.          var _loc4_:b2Shape = null;
  89.          var _loc2_:b2Joint = b2Joint.Create(param1,m_blockAllocator);
  90.          _loc2_.m_prev = null;
  91.          _loc2_.m_next = m_jointList;
  92.          if(m_jointList)
  93.          {
  94.             m_jointList.m_prev = _loc2_;
  95.          }
  96.          m_jointList = _loc2_;
  97.          ++m_jointCount;
  98.          _loc2_.m_node1.joint = _loc2_;
  99.          _loc2_.m_node1.other = _loc2_.m_body2;
  100.          _loc2_.m_node1.prev = null;
  101.          _loc2_.m_node1.next = _loc2_.m_body1.m_jointList;
  102.          if(_loc2_.m_body1.m_jointList)
  103.          {
  104.             _loc2_.m_body1.m_jointList.prev = _loc2_.m_node1;
  105.          }
  106.          _loc2_.m_body1.m_jointList = _loc2_.m_node1;
  107.          _loc2_.m_node2.joint = _loc2_;
  108.          _loc2_.m_node2.other = _loc2_.m_body1;
  109.          _loc2_.m_node2.prev = null;
  110.          _loc2_.m_node2.next = _loc2_.m_body2.m_jointList;
  111.          if(_loc2_.m_body2.m_jointList)
  112.          {
  113.             _loc2_.m_body2.m_jointList.prev = _loc2_.m_node2;
  114.          }
  115.          _loc2_.m_body2.m_jointList = _loc2_.m_node2;
  116.          if(param1.collideConnected == false)
  117.          {
  118.             _loc3_ = param1.body1.m_shapeCount < param1.body2.m_shapeCount ? param1.body1 : param1.body2;
  119.             _loc4_ = _loc3_.m_shapeList;
  120.             while(_loc4_)
  121.             {
  122.                _loc4_.ResetProxy(m_broadPhase);
  123.                _loc4_ = _loc4_.m_next;
  124.             }
  125.          }
  126.          return _loc2_;
  127.       }
  128.       
  129.       public function DestroyJoint(param1:b2Joint) : void
  130.       {
  131.          var _loc5_:b2Body = null;
  132.          var _loc6_:b2Shape = null;
  133.          var _loc2_:Boolean = param1.m_collideConnected;
  134.          if(param1.m_prev)
  135.          {
  136.             param1.m_prev.m_next = param1.m_next;
  137.          }
  138.          if(param1.m_next)
  139.          {
  140.             param1.m_next.m_prev = param1.m_prev;
  141.          }
  142.          if(param1 == m_jointList)
  143.          {
  144.             m_jointList = param1.m_next;
  145.          }
  146.          var _loc3_:b2Body = param1.m_body1;
  147.          var _loc4_:b2Body = param1.m_body2;
  148.          _loc3_.WakeUp();
  149.          _loc4_.WakeUp();
  150.          if(param1.m_node1.prev)
  151.          {
  152.             param1.m_node1.prev.next = param1.m_node1.next;
  153.          }
  154.          if(param1.m_node1.next)
  155.          {
  156.             param1.m_node1.next.prev = param1.m_node1.prev;
  157.          }
  158.          if(param1.m_node1 == _loc3_.m_jointList)
  159.          {
  160.             _loc3_.m_jointList = param1.m_node1.next;
  161.          }
  162.          param1.m_node1.prev = null;
  163.          param1.m_node1.next = null;
  164.          if(param1.m_node2.prev)
  165.          {
  166.             param1.m_node2.prev.next = param1.m_node2.next;
  167.          }
  168.          if(param1.m_node2.next)
  169.          {
  170.             param1.m_node2.next.prev = param1.m_node2.prev;
  171.          }
  172.          if(param1.m_node2 == _loc4_.m_jointList)
  173.          {
  174.             _loc4_.m_jointList = param1.m_node2.next;
  175.          }
  176.          param1.m_node2.prev = null;
  177.          param1.m_node2.next = null;
  178.          b2Joint.Destroy(param1,m_blockAllocator);
  179.          --m_jointCount;
  180.          if(_loc2_ == false)
  181.          {
  182.             _loc6_ = (_loc5_ = _loc3_.m_shapeCount < _loc4_.m_shapeCount ? _loc3_ : _loc4_).m_shapeList;
  183.             while(_loc6_)
  184.             {
  185.                _loc6_.ResetProxy(m_broadPhase);
  186.                _loc6_ = _loc6_.m_next;
  187.             }
  188.          }
  189.       }
  190.       
  191.       public function SetFilter(param1:b2CollisionFilter) : void
  192.       {
  193.          m_filter = param1;
  194.       }
  195.       
  196.       public function DestroyBody(param1:b2Body) : void
  197.       {
  198.          if(param1.m_flags & b2Body.e_destroyFlag)
  199.          {
  200.             return;
  201.          }
  202.          if(param1.m_prev)
  203.          {
  204.             param1.m_prev.m_next = param1.m_next;
  205.          }
  206.          if(param1.m_next)
  207.          {
  208.             param1.m_next.m_prev = param1.m_prev;
  209.          }
  210.          if(param1 == m_bodyList)
  211.          {
  212.             m_bodyList = param1.m_next;
  213.          }
  214.          param1.m_flags |= b2Body.e_destroyFlag;
  215.          --m_bodyCount;
  216.          param1.m_prev = null;
  217.          param1.m_next = m_bodyDestroyList;
  218.          m_bodyDestroyList = param1;
  219.       }
  220.       
  221.       public function SetListener(param1:b2WorldListener) : void
  222.       {
  223.          m_listener = param1;
  224.       }
  225.       
  226.       public function CreateBody(param1:b2BodyDef) : b2Body
  227.       {
  228.          var _loc2_:b2Body = new b2Body(param1,this);
  229.          _loc2_.m_prev = null;
  230.          _loc2_.m_next = m_bodyList;
  231.          if(m_bodyList)
  232.          {
  233.             m_bodyList.m_prev = _loc2_;
  234.          }
  235.          m_bodyList = _loc2_;
  236.          ++m_bodyCount;
  237.          return _loc2_;
  238.       }
  239.       
  240.       public function GetGroundBody() : b2Body
  241.       {
  242.          return m_groundBody;
  243.       }
  244.       
  245.       public function Query(param1:b2AABB, param2:Array, param3:int) : int
  246.       {
  247.          var _loc4_:Array = new Array();
  248.          var _loc5_:int = m_broadPhase.QueryAABB(param1,_loc4_,param3);
  249.          var _loc6_:int = 0;
  250.          while(_loc6_ < _loc5_)
  251.          {
  252.             param2[_loc6_] = _loc4_[_loc6_] as b2Shape;
  253.             _loc6_++;
  254.          }
  255.          return _loc5_;
  256.       }
  257.       
  258.       public function CleanBodyList() : void
  259.       {
  260.          var _loc2_:b2Body = null;
  261.          var _loc3_:b2JointNode = null;
  262.          var _loc4_:b2JointNode = null;
  263.          m_contactManager.m_destroyImmediate = true;
  264.          var _loc1_:b2Body = m_bodyDestroyList;
  265.          while(_loc1_)
  266.          {
  267.             _loc2_ = _loc1_;
  268.             _loc1_ = _loc1_.m_next;
  269.             _loc3_ = _loc2_.m_jointList;
  270.             while(_loc3_)
  271.             {
  272.                _loc4_ = _loc3_;
  273.                _loc3_ = _loc3_.next;
  274.                if(m_listener)
  275.                {
  276.                   m_listener.NotifyJointDestroyed(_loc4_.joint);
  277.                }
  278.                DestroyJoint(_loc4_.joint);
  279.             }
  280.             _loc2_.Destroy();
  281.          }
  282.          m_bodyDestroyList = null;
  283.          m_contactManager.m_destroyImmediate = false;
  284.       }
  285.       
  286.       public function Step(param1:Number, param2:int) : void
  287.       {
  288.          var _loc3_:b2Body = null;
  289.          var _loc4_:b2Body = null;
  290.          var _loc12_:int = 0;
  291.          var _loc13_:int = 0;
  292.          var _loc14_:b2ContactNode = null;
  293.          var _loc15_:b2JointNode = null;
  294.          var _loc16_:uint = 0;
  295.          step.dt = param1;
  296.          step.iterations = param2;
  297.          if(param1 > 0)
  298.          {
  299.             step.inv_dt = 1 / param1;
  300.          }
  301.          else
  302.          {
  303.             step.inv_dt = 0;
  304.          }
  305.          m_positionIterationCount = 0;
  306.          m_contactManager.CleanContactList();
  307.          CleanBodyList();
  308.          m_contactManager.Collide();
  309.          var _loc5_:b2Island = new b2Island(m_bodyCount,m_contactCount,m_jointCount,m_stackAllocator);
  310.          _loc3_ = m_bodyList;
  311.          while(_loc3_ != null)
  312.          {
  313.             _loc3_.m_flags &= ~b2Body.e_islandFlag;
  314.             _loc3_ = _loc3_.m_next;
  315.          }
  316.          var _loc6_:b2Contact = m_contactList;
  317.          while(_loc6_ != null)
  318.          {
  319.             _loc6_.m_flags &= ~b2Contact.e_islandFlag;
  320.             _loc6_ = _loc6_.m_next;
  321.          }
  322.          var _loc7_:b2Joint = m_jointList;
  323.          while(_loc7_ != null)
  324.          {
  325.             _loc7_.m_islandFlag = false;
  326.             _loc7_ = _loc7_.m_next;
  327.          }
  328.          var _loc8_:int = m_bodyCount;
  329.          var _loc9_:Array = new Array(m_bodyCount);
  330.          var _loc10_:int = 0;
  331.          while(_loc10_ < m_bodyCount)
  332.          {
  333.             _loc9_[_loc10_] = null;
  334.             _loc10_++;
  335.          }
  336.          var _loc11_:b2Body = m_bodyList;
  337.          while(_loc11_ != null)
  338.          {
  339.             if(!(_loc11_.m_flags & (b2Body.e_staticFlag | b2Body.e_islandFlag | b2Body.e_sleepFlag | b2Body.e_frozenFlag)))
  340.             {
  341.                _loc5_.Clear();
  342.                _loc12_ = 0;
  343.                var _loc17_:*;
  344.                _loc9_[_loc17_ = _loc12_++] = _loc11_;
  345.                _loc11_.m_flags |= b2Body.e_islandFlag;
  346.                while(_loc12_ > 0)
  347.                {
  348.                   _loc3_ = _loc9_[--_loc12_];
  349.                   _loc5_.AddBody(_loc3_);
  350.                   _loc3_.m_flags &= ~b2Body.e_sleepFlag;
  351.                   if(!(_loc3_.m_flags & b2Body.e_staticFlag))
  352.                   {
  353.                      _loc14_ = _loc3_.m_contactList;
  354.                      while(_loc14_ != null)
  355.                      {
  356.                         if(!(_loc14_.contact.m_flags & b2Contact.e_islandFlag))
  357.                         {
  358.                            _loc5_.AddContact(_loc14_.contact);
  359.                            _loc14_.contact.m_flags |= b2Contact.e_islandFlag;
  360.                            if(!((_loc4_ = _loc14_.other).m_flags & b2Body.e_islandFlag))
  361.                            {
  362.                               var _loc18_:*;
  363.                               _loc9_[_loc18_ = _loc12_++] = _loc4_;
  364.                               _loc4_.m_flags |= b2Body.e_islandFlag;
  365.                            }
  366.                         }
  367.                         _loc14_ = _loc14_.next;
  368.                      }
  369.                      _loc15_ = _loc3_.m_jointList;
  370.                      while(_loc15_ != null)
  371.                      {
  372.                         if(_loc15_.joint.m_islandFlag != true)
  373.                         {
  374.                            _loc5_.AddJoint(_loc15_.joint);
  375.                            _loc15_.joint.m_islandFlag = true;
  376.                            if(!((_loc4_ = _loc15_.other).m_flags & b2Body.e_islandFlag))
  377.                            {
  378.                               _loc9_[_loc18_ = _loc12_++] = _loc4_;
  379.                               _loc4_.m_flags |= b2Body.e_islandFlag;
  380.                            }
  381.                         }
  382.                         _loc15_ = _loc15_.next;
  383.                      }
  384.                   }
  385.                }
  386.                _loc5_.Solve(step,m_gravity);
  387.                m_positionIterationCount = b2Math.b2Max(m_positionIterationCount,b2Island.m_positionIterationCount);
  388.                if(m_allowSleep)
  389.                {
  390.                   _loc5_.UpdateSleep(param1);
  391.                }
  392.                _loc13_ = 0;
  393.                while(_loc13_ < _loc5_.m_bodyCount)
  394.                {
  395.                   _loc3_ = _loc5_.m_bodies[_loc13_];
  396.                   if(_loc3_.m_flags & b2Body.e_staticFlag)
  397.                   {
  398.                      _loc3_.m_flags &= ~b2Body.e_islandFlag;
  399.                   }
  400.                   if(_loc3_.IsFrozen() && Boolean(m_listener))
  401.                   {
  402.                      if((_loc16_ = m_listener.NotifyBoundaryViolated(_loc3_)) == b2WorldListener.b2_destroyBody)
  403.                      {
  404.                         DestroyBody(_loc3_);
  405.                         _loc3_ = null;
  406.                         _loc5_.m_bodies[_loc13_] = null;
  407.                      }
  408.                   }
  409.                   _loc13_++;
  410.                }
  411.             }
  412.             _loc11_ = _loc11_.m_next;
  413.          }
  414.          m_broadPhase.Commit();
  415.       }
  416.       
  417.       public function GetJointList() : b2Joint
  418.       {
  419.          return m_jointList;
  420.       }
  421.       
  422.       public function GetBodyList() : b2Body
  423.       {
  424.          return m_bodyList;
  425.       }
  426.    }
  427. }
  428.