home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / Box2D / Dynamics / b2World.as < prev   
Encoding:
Text File  |  2008-09-03  |  33.3 KB  |  989 lines

  1. package Box2D.Dynamics
  2. {
  3.    import Box2D.Collision.*;
  4.    import Box2D.Collision.Shapes.*;
  5.    import Box2D.Common.*;
  6.    import Box2D.Common.Math.*;
  7.    import Box2D.Dynamics.Contacts.*;
  8.    import Box2D.Dynamics.Joints.*;
  9.    
  10.    public class b2World
  11.    {
  12.       
  13.       private static var s_jointColor:b2Color = new b2Color(0.5,0.8,0.8);
  14.       
  15.       public static var m_continuousPhysics:Boolean;
  16.       
  17.       public static var m_warmStarting:Boolean;
  18.       
  19.       private static var s_coreColor:b2Color = new b2Color(0.9,0.6,0.6);
  20.       
  21.       public static var m_positionCorrection:Boolean;
  22.       
  23.       private static var s_xf:b2XForm = new b2XForm();
  24.        
  25.       
  26.       public var m_inv_dt0:Number;
  27.       
  28.       public var m_boundaryListener:b2BoundaryListener;
  29.       
  30.       public var m_contactList:b2Contact;
  31.       
  32.       public var m_blockAllocator:*;
  33.       
  34.       public var m_contactListener:b2ContactListener;
  35.       
  36.       public var m_allowSleep:Boolean;
  37.       
  38.       public var m_broadPhase:b2BroadPhase;
  39.       
  40.       public var m_destructionListener:b2DestructionListener;
  41.       
  42.       public var m_jointCount:int;
  43.       
  44.       public var m_bodyCount:int;
  45.       
  46.       public var m_lock:Boolean;
  47.       
  48.       public var m_positionIterationCount:int;
  49.       
  50.       public var m_groundBody:b2Body;
  51.       
  52.       public var m_contactCount:int;
  53.       
  54.       public var m_debugDraw:b2DebugDraw;
  55.       
  56.       public var m_contactFilter:b2ContactFilter;
  57.       
  58.       public var m_bodyList:b2Body;
  59.       
  60.       public var m_stackAllocator:*;
  61.       
  62.       public var m_jointList:b2Joint;
  63.       
  64.       public var m_gravity:b2Vec2;
  65.       
  66.       public var m_contactManager:b2ContactManager;
  67.       
  68.       public function b2World(worldAABB:b2AABB, gravity:b2Vec2, doSleep:Boolean)
  69.       {
  70.          m_contactManager = new b2ContactManager();
  71.          super();
  72.          m_destructionListener = null;
  73.          m_boundaryListener = null;
  74.          m_contactFilter = b2ContactFilter.b2_defaultFilter;
  75.          m_contactListener = null;
  76.          m_debugDraw = null;
  77.          m_bodyList = null;
  78.          m_contactList = null;
  79.          m_jointList = null;
  80.          m_bodyCount = 0;
  81.          m_contactCount = 0;
  82.          m_jointCount = 0;
  83.          m_positionCorrection = true;
  84.          m_warmStarting = true;
  85.          m_continuousPhysics = true;
  86.          m_allowSleep = doSleep;
  87.          m_gravity = gravity;
  88.          m_lock = false;
  89.          m_inv_dt0 = 0;
  90.          m_contactManager.m_world = this;
  91.          m_broadPhase = new b2BroadPhase(worldAABB,m_contactManager);
  92.          var bd:b2BodyDef = new b2BodyDef();
  93.          m_groundBody = CreateBody(bd);
  94.       }
  95.       
  96.       public function DrawJoint(joint:b2Joint) : void
  97.       {
  98.          var pulley:b2PulleyJoint = null;
  99.          var s1:b2Vec2 = null;
  100.          var s2:b2Vec2 = null;
  101.          var b1:b2Body = joint.m_body1;
  102.          var b2:b2Body = joint.m_body2;
  103.          var xf1:b2XForm = b1.m_xf;
  104.          var xf2:b2XForm = b2.m_xf;
  105.          var x1:b2Vec2 = xf1.position;
  106.          var x2:b2Vec2 = xf2.position;
  107.          var p1:b2Vec2 = joint.GetAnchor1();
  108.          var p2:b2Vec2 = joint.GetAnchor2();
  109.          var color:b2Color = s_jointColor;
  110.          switch(joint.m_type)
  111.          {
  112.             case b2Joint.e_distanceJoint:
  113.                m_debugDraw.DrawSegment(p1,p2,color);
  114.                break;
  115.             case b2Joint.e_pulleyJoint:
  116.                pulley = joint as b2PulleyJoint;
  117.                s1 = pulley.GetGroundAnchor1();
  118.                s2 = pulley.GetGroundAnchor2();
  119.                m_debugDraw.DrawSegment(s1,p1,color);
  120.                m_debugDraw.DrawSegment(s2,p2,color);
  121.                m_debugDraw.DrawSegment(s1,s2,color);
  122.                break;
  123.             case b2Joint.e_mouseJoint:
  124.                m_debugDraw.DrawSegment(p1,p2,color);
  125.                break;
  126.             default:
  127.                if(b1 != m_groundBody)
  128.                {
  129.                   m_debugDraw.DrawSegment(x1,p1,color);
  130.                }
  131.                m_debugDraw.DrawSegment(p1,p2,color);
  132.                if(b2 != m_groundBody)
  133.                {
  134.                   m_debugDraw.DrawSegment(x2,p2,color);
  135.                }
  136.          }
  137.       }
  138.       
  139.       public function Refilter(shape:b2Shape) : void
  140.       {
  141.          shape.RefilterProxy(m_broadPhase,shape.m_body.m_xf);
  142.       }
  143.       
  144.       public function SetDebugDraw(debugDraw:b2DebugDraw) : void
  145.       {
  146.          m_debugDraw = debugDraw;
  147.       }
  148.       
  149.       public function SetContinuousPhysics(flag:Boolean) : void
  150.       {
  151.          m_continuousPhysics = flag;
  152.       }
  153.       
  154.       public function GetProxyCount() : int
  155.       {
  156.          return m_broadPhase.m_proxyCount;
  157.       }
  158.       
  159.       public function DrawDebugData() : void
  160.       {
  161.          var i:int = 0;
  162.          var b:b2Body = null;
  163.          var s:b2Shape = null;
  164.          var j:b2Joint = null;
  165.          var bp:b2BroadPhase = null;
  166.          var xf:b2XForm = null;
  167.          var core:* = false;
  168.          var index:uint = 0;
  169.          var pair:b2Pair = null;
  170.          var p1:b2Proxy = null;
  171.          var p2:b2Proxy = null;
  172.          var worldLower:b2Vec2 = null;
  173.          var worldUpper:b2Vec2 = null;
  174.          var p:b2Proxy = null;
  175.          var poly:b2PolygonShape = null;
  176.          var obb:b2OBB = null;
  177.          var h:b2Vec2 = null;
  178.          var tMat:b2Mat22 = null;
  179.          var tVec:b2Vec2 = null;
  180.          var tX:Number = NaN;
  181.          if(m_debugDraw == null)
  182.          {
  183.             return;
  184.          }
  185.          m_debugDraw.m_sprite.graphics.clear();
  186.          var flags:uint = m_debugDraw.GetFlags();
  187.          var invQ:b2Vec2 = new b2Vec2();
  188.          var x1:b2Vec2 = new b2Vec2();
  189.          var x2:b2Vec2 = new b2Vec2();
  190.          var color:b2Color = new b2Color(0,0,0);
  191.          var b1:b2AABB = new b2AABB();
  192.          var b2:b2AABB = new b2AABB();
  193.          var vs:Array = [new b2Vec2(),new b2Vec2(),new b2Vec2(),new b2Vec2()];
  194.          if(flags & b2DebugDraw.e_shapeBit)
  195.          {
  196.             core = (flags & b2DebugDraw.e_coreShapeBit) == b2DebugDraw.e_coreShapeBit;
  197.             b = m_bodyList;
  198.             while(b)
  199.             {
  200.                xf = b.m_xf;
  201.                s = b.GetShapeList();
  202.                while(s)
  203.                {
  204.                   if(b.IsStatic())
  205.                   {
  206.                      DrawShape(s,xf,new b2Color(0.5,0.9,0.5),core);
  207.                   }
  208.                   else if(b.IsSleeping())
  209.                   {
  210.                      DrawShape(s,xf,new b2Color(0.5,0.5,0.9),core);
  211.                   }
  212.                   else
  213.                   {
  214.                      DrawShape(s,xf,new b2Color(0.9,0.9,0.9),core);
  215.                   }
  216.                   s = s.m_next;
  217.                }
  218.                b = b.m_next;
  219.             }
  220.          }
  221.          if(flags & b2DebugDraw.e_jointBit)
  222.          {
  223.             j = m_jointList;
  224.             while(j)
  225.             {
  226.                DrawJoint(j);
  227.                j = j.m_next;
  228.             }
  229.          }
  230.          if(flags & b2DebugDraw.e_pairBit)
  231.          {
  232.             bp = m_broadPhase;
  233.             invQ.Set(1 / bp.m_quantizationFactor.x,1 / bp.m_quantizationFactor.y);
  234.             color.Set(0.9,0.9,0.3);
  235.             for(i = 0; i < b2Pair.b2_tableCapacity; i++)
  236.             {
  237.                index = uint(bp.m_pairManager.m_hashTable[i]);
  238.                while(index != b2Pair.b2_nullPair)
  239.                {
  240.                   pair = bp.m_pairManager.m_pairs[index];
  241.                   p1 = bp.m_proxyPool[pair.proxyId1];
  242.                   p2 = bp.m_proxyPool[pair.proxyId2];
  243.                   b1.lowerBound.x = bp.m_worldAABB.lowerBound.x + invQ.x * bp.m_bounds[0][p1.lowerBounds[0]].value;
  244.                   b1.lowerBound.y = bp.m_worldAABB.lowerBound.y + invQ.y * bp.m_bounds[1][p1.lowerBounds[1]].value;
  245.                   b1.upperBound.x = bp.m_worldAABB.lowerBound.x + invQ.x * bp.m_bounds[0][p1.upperBounds[0]].value;
  246.                   b1.upperBound.y = bp.m_worldAABB.lowerBound.y + invQ.y * bp.m_bounds[1][p1.upperBounds[1]].value;
  247.                   b2.lowerBound.x = bp.m_worldAABB.lowerBound.x + invQ.x * bp.m_bounds[0][p2.lowerBounds[0]].value;
  248.                   b2.lowerBound.y = bp.m_worldAABB.lowerBound.y + invQ.y * bp.m_bounds[1][p2.lowerBounds[1]].value;
  249.                   b2.upperBound.x = bp.m_worldAABB.lowerBound.x + invQ.x * bp.m_bounds[0][p2.upperBounds[0]].value;
  250.                   b2.upperBound.y = bp.m_worldAABB.lowerBound.y + invQ.y * bp.m_bounds[1][p2.upperBounds[1]].value;
  251.                   x1.x = 0.5 * (b1.lowerBound.x + b1.upperBound.x);
  252.                   x1.y = 0.5 * (b1.lowerBound.y + b1.upperBound.y);
  253.                   x2.x = 0.5 * (b2.lowerBound.x + b2.upperBound.x);
  254.                   x2.y = 0.5 * (b2.lowerBound.y + b2.upperBound.y);
  255.                   m_debugDraw.DrawSegment(x1,x2,color);
  256.                   index = pair.next;
  257.                }
  258.             }
  259.          }
  260.          if(flags & b2DebugDraw.e_aabbBit)
  261.          {
  262.             bp = m_broadPhase;
  263.             worldLower = bp.m_worldAABB.lowerBound;
  264.             worldUpper = bp.m_worldAABB.upperBound;
  265.             invQ.Set(1 / bp.m_quantizationFactor.x,1 / bp.m_quantizationFactor.y);
  266.             color.Set(0.9,0.3,0.9);
  267.             for(i = 0; i < b2Settings.b2_maxProxies; i++)
  268.             {
  269.                p = bp.m_proxyPool[i];
  270.                if(p.IsValid() != false)
  271.                {
  272.                   b1.lowerBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.lowerBounds[0]].value;
  273.                   b1.lowerBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.lowerBounds[1]].value;
  274.                   b1.upperBound.x = worldLower.x + invQ.x * bp.m_bounds[0][p.upperBounds[0]].value;
  275.                   b1.upperBound.y = worldLower.y + invQ.y * bp.m_bounds[1][p.upperBounds[1]].value;
  276.                   vs[0].Set(b1.lowerBound.x,b1.lowerBound.y);
  277.                   vs[1].Set(b1.upperBound.x,b1.lowerBound.y);
  278.                   vs[2].Set(b1.upperBound.x,b1.upperBound.y);
  279.                   vs[3].Set(b1.lowerBound.x,b1.upperBound.y);
  280.                   m_debugDraw.DrawPolygon(vs,4,color);
  281.                }
  282.             }
  283.             vs[0].Set(worldLower.x,worldLower.y);
  284.             vs[1].Set(worldUpper.x,worldLower.y);
  285.             vs[2].Set(worldUpper.x,worldUpper.y);
  286.             vs[3].Set(worldLower.x,worldUpper.y);
  287.             m_debugDraw.DrawPolygon(vs,4,new b2Color(0.3,0.9,0.9));
  288.          }
  289.          if(flags & b2DebugDraw.e_obbBit)
  290.          {
  291.             color.Set(0.5,0.3,0.5);
  292.             b = m_bodyList;
  293.             while(b)
  294.             {
  295.                xf = b.m_xf;
  296.                s = b.GetShapeList();
  297.                while(s)
  298.                {
  299.                   if(s.m_type == b2Shape.e_polygonShape)
  300.                   {
  301.                      poly = s as b2PolygonShape;
  302.                      obb = poly.GetOBB();
  303.                      h = obb.extents;
  304.                      vs[0].Set(-h.x,-h.y);
  305.                      vs[1].Set(h.x,-h.y);
  306.                      vs[2].Set(h.x,h.y);
  307.                      vs[3].Set(-h.x,h.y);
  308.                      for(i = 0; i < 4; i++)
  309.                      {
  310.                         tMat = obb.R;
  311.                         tVec = vs[i];
  312.                         tX = obb.center.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  313.                         vs[i].y = obb.center.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  314.                         vs[i].x = tX;
  315.                         tMat = xf.R;
  316.                         tX = xf.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  317.                         vs[i].y = xf.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  318.                         vs[i].x = tX;
  319.                      }
  320.                      m_debugDraw.DrawPolygon(vs,4,color);
  321.                   }
  322.                   s = s.m_next;
  323.                }
  324.                b = b.m_next;
  325.             }
  326.          }
  327.          if(flags & b2DebugDraw.e_centerOfMassBit)
  328.          {
  329.             b = m_bodyList;
  330.             while(b)
  331.             {
  332.                xf = s_xf;
  333.                xf.R = b.m_xf.R;
  334.                xf.position = b.GetWorldCenter();
  335.                m_debugDraw.DrawXForm(xf);
  336.                b = b.m_next;
  337.             }
  338.          }
  339.       }
  340.       
  341.       public function DestroyBody(b:b2Body) : void
  342.       {
  343.          var jn0:b2JointEdge = null;
  344.          var s0:b2Shape = null;
  345.          if(m_lock == true)
  346.          {
  347.             return;
  348.          }
  349.          var jn:b2JointEdge = b.m_jointList;
  350.          while(jn)
  351.          {
  352.             jn0 = jn;
  353.             jn = jn.next;
  354.             if(m_destructionListener)
  355.             {
  356.                m_destructionListener.SayGoodbyeJoint(jn0.joint);
  357.             }
  358.             DestroyJoint(jn0.joint);
  359.          }
  360.          var s:b2Shape = b.m_shapeList;
  361.          while(s)
  362.          {
  363.             s0 = s;
  364.             s = s.m_next;
  365.             if(m_destructionListener)
  366.             {
  367.                m_destructionListener.SayGoodbyeShape(s0);
  368.             }
  369.             s0.DestroyProxy(m_broadPhase);
  370.             b2Shape.Destroy(s0,m_blockAllocator);
  371.          }
  372.          if(b.m_prev)
  373.          {
  374.             b.m_prev.m_next = b.m_next;
  375.          }
  376.          if(b.m_next)
  377.          {
  378.             b.m_next.m_prev = b.m_prev;
  379.          }
  380.          if(b == m_bodyList)
  381.          {
  382.             m_bodyList = b.m_next;
  383.          }
  384.          --m_bodyCount;
  385.       }
  386.       
  387.       public function SetContactFilter(filter:b2ContactFilter) : void
  388.       {
  389.          m_contactFilter = filter;
  390.       }
  391.       
  392.       public function GetGroundBody() : b2Body
  393.       {
  394.          return m_groundBody;
  395.       }
  396.       
  397.       public function DrawShape(shape:b2Shape, xf:b2XForm, color:b2Color, core:Boolean) : void
  398.       {
  399.          var circle:b2CircleShape = null;
  400.          var center:b2Vec2 = null;
  401.          var radius:Number = NaN;
  402.          var axis:b2Vec2 = null;
  403.          var i:int = 0;
  404.          var poly:b2PolygonShape = null;
  405.          var vertexCount:int = 0;
  406.          var localVertices:Array = null;
  407.          var vertices:Array = null;
  408.          var localCoreVertices:Array = null;
  409.          var coreColor:b2Color = s_coreColor;
  410.          switch(shape.m_type)
  411.          {
  412.             case b2Shape.e_circleShape:
  413.                circle = shape as b2CircleShape;
  414.                center = b2Math.b2MulX(xf,circle.m_localPosition);
  415.                radius = circle.m_radius;
  416.                axis = xf.R.col1;
  417.                m_debugDraw.DrawSolidCircle(center,radius,axis,color);
  418.                if(core)
  419.                {
  420.                   m_debugDraw.DrawCircle(center,radius - b2Settings.b2_toiSlop,coreColor);
  421.                }
  422.                break;
  423.             case b2Shape.e_polygonShape:
  424.                poly = shape as b2PolygonShape;
  425.                vertexCount = poly.GetVertexCount();
  426.                localVertices = poly.GetVertices();
  427.                vertices = new Array(b2Settings.b2_maxPolygonVertices);
  428.                for(i = 0; i < vertexCount; i++)
  429.                {
  430.                   vertices[i] = b2Math.b2MulX(xf,localVertices[i]);
  431.                }
  432.                m_debugDraw.DrawSolidPolygon(vertices,vertexCount,color);
  433.                if(core)
  434.                {
  435.                   localCoreVertices = poly.GetCoreVertices();
  436.                   for(i = 0; i < vertexCount; i++)
  437.                   {
  438.                      vertices[i] = b2Math.b2MulX(xf,localCoreVertices[i]);
  439.                   }
  440.                   m_debugDraw.DrawPolygon(vertices,vertexCount,coreColor);
  441.                }
  442.          }
  443.       }
  444.       
  445.       public function GetContactCount() : int
  446.       {
  447.          return m_contactCount;
  448.       }
  449.       
  450.       public function Solve(step:b2TimeStep) : void
  451.       {
  452.          var b:b2Body = null;
  453.          var stackCount:int = 0;
  454.          var i:int = 0;
  455.          var other:b2Body = null;
  456.          var cn:b2ContactEdge = null;
  457.          var jn:b2JointEdge = null;
  458.          var inRange:Boolean = false;
  459.          m_positionIterationCount = 0;
  460.          var island:b2Island = new b2Island(m_bodyCount,m_contactCount,m_jointCount,m_stackAllocator,m_contactListener);
  461.          b = m_bodyList;
  462.          while(b)
  463.          {
  464.             b.m_flags &= ~b2Body.e_islandFlag;
  465.             b = b.m_next;
  466.          }
  467.          var c:b2Contact = m_contactList;
  468.          while(c)
  469.          {
  470.             c.m_flags &= ~b2Contact.e_islandFlag;
  471.             c = c.m_next;
  472.          }
  473.          var j:b2Joint = m_jointList;
  474.          while(j)
  475.          {
  476.             j.m_islandFlag = false;
  477.             j = j.m_next;
  478.          }
  479.          var stackSize:int = m_bodyCount;
  480.          var stack:Array = new Array(stackSize);
  481.          var seed:b2Body = m_bodyList;
  482.          while(seed)
  483.          {
  484.             if(!(seed.m_flags & (b2Body.e_islandFlag | b2Body.e_sleepFlag | b2Body.e_frozenFlag)))
  485.             {
  486.                if(!seed.IsStatic())
  487.                {
  488.                   island.Clear();
  489.                   stackCount = 0;
  490.                   var _loc15_:*;
  491.                   stack[_loc15_ = stackCount++] = seed;
  492.                   seed.m_flags |= b2Body.e_islandFlag;
  493.                   while(stackCount > 0)
  494.                   {
  495.                      b = stack[--stackCount];
  496.                      island.AddBody(b);
  497.                      b.m_flags &= ~b2Body.e_sleepFlag;
  498.                      if(!b.IsStatic())
  499.                      {
  500.                         cn = b.m_contactList;
  501.                         while(cn)
  502.                         {
  503.                            if(!(cn.contact.m_flags & (b2Contact.e_islandFlag | b2Contact.e_nonSolidFlag)))
  504.                            {
  505.                               if(cn.contact.m_manifoldCount != 0)
  506.                               {
  507.                                  island.AddContact(cn.contact);
  508.                                  cn.contact.m_flags |= b2Contact.e_islandFlag;
  509.                                  other = cn.other;
  510.                                  if(!(other.m_flags & b2Body.e_islandFlag))
  511.                                  {
  512.                                     var _loc16_:*;
  513.                                     stack[_loc16_ = stackCount++] = other;
  514.                                     other.m_flags |= b2Body.e_islandFlag;
  515.                                  }
  516.                               }
  517.                            }
  518.                            cn = cn.next;
  519.                         }
  520.                         jn = b.m_jointList;
  521.                         while(jn)
  522.                         {
  523.                            if(jn.joint.m_islandFlag != true)
  524.                            {
  525.                               island.AddJoint(jn.joint);
  526.                               jn.joint.m_islandFlag = true;
  527.                               other = jn.other;
  528.                               if(!(other.m_flags & b2Body.e_islandFlag))
  529.                               {
  530.                                  stack[_loc16_ = stackCount++] = other;
  531.                                  other.m_flags |= b2Body.e_islandFlag;
  532.                               }
  533.                            }
  534.                            jn = jn.next;
  535.                         }
  536.                      }
  537.                   }
  538.                   island.Solve(step,m_gravity,m_positionCorrection,m_allowSleep);
  539.                   if(island.m_positionIterationCount > m_positionIterationCount)
  540.                   {
  541.                      m_positionIterationCount = island.m_positionIterationCount;
  542.                   }
  543.                   for(i = 0; i < island.m_bodyCount; i++)
  544.                   {
  545.                      b = island.m_bodies[i];
  546.                      if(b.IsStatic())
  547.                      {
  548.                         b.m_flags &= ~b2Body.e_islandFlag;
  549.                      }
  550.                   }
  551.                }
  552.             }
  553.             seed = seed.m_next;
  554.          }
  555.          b = m_bodyList;
  556.          while(b)
  557.          {
  558.             if(!(b.m_flags & (b2Body.e_sleepFlag | b2Body.e_frozenFlag)))
  559.             {
  560.                if(!b.IsStatic())
  561.                {
  562.                   inRange = b.SynchronizeShapes();
  563.                   if(inRange == false && m_boundaryListener != null)
  564.                   {
  565.                      m_boundaryListener.Violation(b);
  566.                   }
  567.                }
  568.             }
  569.             b = b.m_next;
  570.          }
  571.          m_broadPhase.Commit();
  572.       }
  573.       
  574.       public function Query(aabb:b2AABB, shapes:Array, maxCount:int) : int
  575.       {
  576.          var results:Array = new Array(maxCount);
  577.          var count:int = m_broadPhase.QueryAABB(aabb,results,maxCount);
  578.          for(var i:int = 0; i < count; i++)
  579.          {
  580.             shapes[i] = results[i];
  581.          }
  582.          return count;
  583.       }
  584.       
  585.       public function SetGravity(gravity:b2Vec2) : void
  586.       {
  587.          m_gravity = gravity;
  588.       }
  589.       
  590.       public function SolveTOI(step:b2TimeStep) : void
  591.       {
  592.          var b:b2Body = null;
  593.          var s1:b2Shape = null;
  594.          var s2:b2Shape = null;
  595.          var b1:b2Body = null;
  596.          var b2:b2Body = null;
  597.          var cn:b2ContactEdge = null;
  598.          var c:b2Contact = null;
  599.          var minContact:b2Contact = null;
  600.          var minTOI:Number = NaN;
  601.          var seed:b2Body = null;
  602.          var stackCount:int = 0;
  603.          var subStep:b2TimeStep = null;
  604.          var i:int = 0;
  605.          var toi:Number = NaN;
  606.          var t0:Number = NaN;
  607.          var other:b2Body = null;
  608.          var inRange:Boolean = false;
  609.          var island:b2Island = new b2Island(m_bodyCount,b2Settings.b2_maxTOIContactsPerIsland,0,m_stackAllocator,m_contactListener);
  610.          var stackSize:int = m_bodyCount;
  611.          var stack:Array = new Array(stackSize);
  612.          b = m_bodyList;
  613.          while(b)
  614.          {
  615.             b.m_flags &= ~b2Body.e_islandFlag;
  616.             b.m_sweep.t0 = 0;
  617.             b = b.m_next;
  618.          }
  619.          c = m_contactList;
  620.          while(c)
  621.          {
  622.             c.m_flags &= ~(b2Contact.e_toiFlag | b2Contact.e_islandFlag);
  623.             c = c.m_next;
  624.          }
  625.          while(true)
  626.          {
  627.             minContact = null;
  628.             minTOI = 1;
  629.             c = m_contactList;
  630.             for(; c; c = c.m_next)
  631.             {
  632.                if(!(c.m_flags & (b2Contact.e_slowFlag | b2Contact.e_nonSolidFlag)))
  633.                {
  634.                   toi = 1;
  635.                   if(c.m_flags & b2Contact.e_toiFlag)
  636.                   {
  637.                      toi = c.m_toi;
  638.                   }
  639.                   else
  640.                   {
  641.                      s1 = c.m_shape1;
  642.                      s2 = c.m_shape2;
  643.                      b1 = s1.m_body;
  644.                      b2 = s2.m_body;
  645.                      if((b1.IsStatic() || b1.IsSleeping()) && (b2.IsStatic() || b2.IsSleeping()))
  646.                      {
  647.                         continue;
  648.                      }
  649.                      t0 = b1.m_sweep.t0;
  650.                      if(b1.m_sweep.t0 < b2.m_sweep.t0)
  651.                      {
  652.                         t0 = b2.m_sweep.t0;
  653.                         b1.m_sweep.Advance(t0);
  654.                      }
  655.                      else if(b2.m_sweep.t0 < b1.m_sweep.t0)
  656.                      {
  657.                         t0 = b1.m_sweep.t0;
  658.                         b2.m_sweep.Advance(t0);
  659.                      }
  660.                      toi = b2TimeOfImpact.TimeOfImpact(c.m_shape1,b1.m_sweep,c.m_shape2,b2.m_sweep);
  661.                      if(toi > 0 && toi < 1)
  662.                      {
  663.                         toi = (1 - toi) * t0 + toi;
  664.                         if(toi > 1)
  665.                         {
  666.                            toi = 1;
  667.                         }
  668.                      }
  669.                      c.m_toi = toi;
  670.                      c.m_flags |= b2Contact.e_toiFlag;
  671.                   }
  672.                   if(Number.MIN_VALUE < toi && toi < minTOI)
  673.                   {
  674.                      minContact = c;
  675.                      minTOI = toi;
  676.                   }
  677.                }
  678.             }
  679.             if(minContact == null || 1 - 100 * Number.MIN_VALUE < minTOI)
  680.             {
  681.                break;
  682.             }
  683.             s1 = minContact.m_shape1;
  684.             s2 = minContact.m_shape2;
  685.             b1 = s1.m_body;
  686.             b2 = s2.m_body;
  687.             b1.Advance(minTOI);
  688.             b2.Advance(minTOI);
  689.             minContact.Update(m_contactListener);
  690.             minContact.m_flags &= ~b2Contact.e_toiFlag;
  691.             if(minContact.m_manifoldCount != 0)
  692.             {
  693.                seed = b1;
  694.                if(seed.IsStatic())
  695.                {
  696.                   seed = b2;
  697.                }
  698.                island.Clear();
  699.                stackCount = 0;
  700.                var _loc22_:*;
  701.                stack[_loc22_ = stackCount++] = seed;
  702.                seed.m_flags |= b2Body.e_islandFlag;
  703.                while(stackCount > 0)
  704.                {
  705.                   b = stack[--stackCount];
  706.                   island.AddBody(b);
  707.                   b.m_flags &= ~b2Body.e_sleepFlag;
  708.                   if(!b.IsStatic())
  709.                   {
  710.                      cn = b.m_contactList;
  711.                      while(cn)
  712.                      {
  713.                         if(island.m_contactCount != island.m_contactCapacity)
  714.                         {
  715.                            if(!(cn.contact.m_flags & (b2Contact.e_islandFlag | b2Contact.e_slowFlag | b2Contact.e_nonSolidFlag)))
  716.                            {
  717.                               if(cn.contact.m_manifoldCount != 0)
  718.                               {
  719.                                  island.AddContact(cn.contact);
  720.                                  cn.contact.m_flags |= b2Contact.e_islandFlag;
  721.                                  other = cn.other;
  722.                                  if(!(other.m_flags & b2Body.e_islandFlag))
  723.                                  {
  724.                                     if(other.IsStatic() == false)
  725.                                     {
  726.                                        other.Advance(minTOI);
  727.                                        other.WakeUp();
  728.                                     }
  729.                                     var _loc23_:*;
  730.                                     stack[_loc23_ = stackCount++] = other;
  731.                                     other.m_flags |= b2Body.e_islandFlag;
  732.                                  }
  733.                               }
  734.                            }
  735.                         }
  736.                         cn = cn.next;
  737.                      }
  738.                   }
  739.                }
  740.                subStep = new b2TimeStep();
  741.                subStep.dt = (1 - minTOI) * step.dt;
  742.                subStep.inv_dt = 1 / subStep.dt;
  743.                subStep.maxIterations = step.maxIterations;
  744.                island.SolveTOI(subStep);
  745.                for(i = 0; i < island.m_bodyCount; i++)
  746.                {
  747.                   b = island.m_bodies[i];
  748.                   b.m_flags &= ~b2Body.e_islandFlag;
  749.                   if(!(b.m_flags & (b2Body.e_sleepFlag | b2Body.e_frozenFlag)))
  750.                   {
  751.                      if(!b.IsStatic())
  752.                      {
  753.                         inRange = b.SynchronizeShapes();
  754.                         if(inRange == false && m_boundaryListener != null)
  755.                         {
  756.                            m_boundaryListener.Violation(b);
  757.                         }
  758.                         cn = b.m_contactList;
  759.                         while(cn)
  760.                         {
  761.                            cn.contact.m_flags &= ~b2Contact.e_toiFlag;
  762.                            cn = cn.next;
  763.                         }
  764.                      }
  765.                   }
  766.                }
  767.                for(i = 0; i < island.m_contactCount; i++)
  768.                {
  769.                   c = island.m_contacts[i];
  770.                   c.m_flags &= ~(b2Contact.e_toiFlag | b2Contact.e_islandFlag);
  771.                }
  772.                m_broadPhase.Commit();
  773.             }
  774.          }
  775.       }
  776.       
  777.       public function GetJointList() : b2Joint
  778.       {
  779.          return m_jointList;
  780.       }
  781.       
  782.       public function GetBodyList() : b2Body
  783.       {
  784.          return m_bodyList;
  785.       }
  786.       
  787.       public function GetPairCount() : int
  788.       {
  789.          return m_broadPhase.m_pairManager.m_pairCount;
  790.       }
  791.       
  792.       public function Validate() : void
  793.       {
  794.          m_broadPhase.Validate();
  795.       }
  796.       
  797.       public function SetWarmStarting(flag:Boolean) : void
  798.       {
  799.          m_warmStarting = flag;
  800.       }
  801.       
  802.       public function SetPositionCorrection(flag:Boolean) : void
  803.       {
  804.          m_positionCorrection = flag;
  805.       }
  806.       
  807.       public function CreateJoint(def:b2JointDef) : b2Joint
  808.       {
  809.          var b:b2Body = null;
  810.          var s:b2Shape = null;
  811.          var j:b2Joint = b2Joint.Create(def,m_blockAllocator);
  812.          j.m_prev = null;
  813.          j.m_next = m_jointList;
  814.          if(m_jointList)
  815.          {
  816.             m_jointList.m_prev = j;
  817.          }
  818.          m_jointList = j;
  819.          ++m_jointCount;
  820.          j.m_node1.joint = j;
  821.          j.m_node1.other = j.m_body2;
  822.          j.m_node1.prev = null;
  823.          j.m_node1.next = j.m_body1.m_jointList;
  824.          if(j.m_body1.m_jointList)
  825.          {
  826.             j.m_body1.m_jointList.prev = j.m_node1;
  827.          }
  828.          j.m_body1.m_jointList = j.m_node1;
  829.          j.m_node2.joint = j;
  830.          j.m_node2.other = j.m_body1;
  831.          j.m_node2.prev = null;
  832.          j.m_node2.next = j.m_body2.m_jointList;
  833.          if(j.m_body2.m_jointList)
  834.          {
  835.             j.m_body2.m_jointList.prev = j.m_node2;
  836.          }
  837.          j.m_body2.m_jointList = j.m_node2;
  838.          if(def.collideConnected == false)
  839.          {
  840.             b = def.body1.m_shapeCount < def.body2.m_shapeCount ? def.body1 : def.body2;
  841.             s = b.m_shapeList;
  842.             while(s)
  843.             {
  844.                s.RefilterProxy(m_broadPhase,b.m_xf);
  845.                s = s.m_next;
  846.             }
  847.          }
  848.          return j;
  849.       }
  850.       
  851.       public function DestroyJoint(j:b2Joint) : void
  852.       {
  853.          var b:b2Body = null;
  854.          var s:b2Shape = null;
  855.          var collideConnected:Boolean = j.m_collideConnected;
  856.          if(j.m_prev)
  857.          {
  858.             j.m_prev.m_next = j.m_next;
  859.          }
  860.          if(j.m_next)
  861.          {
  862.             j.m_next.m_prev = j.m_prev;
  863.          }
  864.          if(j == m_jointList)
  865.          {
  866.             m_jointList = j.m_next;
  867.          }
  868.          var body1:b2Body = j.m_body1;
  869.          var body2:b2Body = j.m_body2;
  870.          body1.WakeUp();
  871.          body2.WakeUp();
  872.          if(j.m_node1.prev)
  873.          {
  874.             j.m_node1.prev.next = j.m_node1.next;
  875.          }
  876.          if(j.m_node1.next)
  877.          {
  878.             j.m_node1.next.prev = j.m_node1.prev;
  879.          }
  880.          if(j.m_node1 == body1.m_jointList)
  881.          {
  882.             body1.m_jointList = j.m_node1.next;
  883.          }
  884.          j.m_node1.prev = null;
  885.          j.m_node1.next = null;
  886.          if(j.m_node2.prev)
  887.          {
  888.             j.m_node2.prev.next = j.m_node2.next;
  889.          }
  890.          if(j.m_node2.next)
  891.          {
  892.             j.m_node2.next.prev = j.m_node2.prev;
  893.          }
  894.          if(j.m_node2 == body2.m_jointList)
  895.          {
  896.             body2.m_jointList = j.m_node2.next;
  897.          }
  898.          j.m_node2.prev = null;
  899.          j.m_node2.next = null;
  900.          b2Joint.Destroy(j,m_blockAllocator);
  901.          --m_jointCount;
  902.          if(collideConnected == false)
  903.          {
  904.             b = body1.m_shapeCount < body2.m_shapeCount ? body1 : body2;
  905.             s = b.m_shapeList;
  906.             while(s)
  907.             {
  908.                s.RefilterProxy(m_broadPhase,b.m_xf);
  909.                s = s.m_next;
  910.             }
  911.          }
  912.       }
  913.       
  914.       public function SetContactListener(listener:b2ContactListener) : void
  915.       {
  916.          m_contactListener = listener;
  917.       }
  918.       
  919.       public function CreateBody(def:b2BodyDef) : b2Body
  920.       {
  921.          if(m_lock == true)
  922.          {
  923.             return null;
  924.          }
  925.          var b:b2Body = new b2Body(def,this);
  926.          b.m_prev = null;
  927.          b.m_next = m_bodyList;
  928.          if(m_bodyList)
  929.          {
  930.             m_bodyList.m_prev = b;
  931.          }
  932.          m_bodyList = b;
  933.          ++m_bodyCount;
  934.          return b;
  935.       }
  936.       
  937.       public function SetBoundaryListener(listener:b2BoundaryListener) : void
  938.       {
  939.          m_boundaryListener = listener;
  940.       }
  941.       
  942.       public function SetDestructionListener(listener:b2DestructionListener) : void
  943.       {
  944.          m_destructionListener = listener;
  945.       }
  946.       
  947.       public function Step(dt:Number, iterations:int) : void
  948.       {
  949.          m_lock = true;
  950.          var step:b2TimeStep = new b2TimeStep();
  951.          step.dt = dt;
  952.          step.maxIterations = iterations;
  953.          if(dt > 0)
  954.          {
  955.             step.inv_dt = 1 / dt;
  956.          }
  957.          else
  958.          {
  959.             step.inv_dt = 0;
  960.          }
  961.          step.dtRatio = m_inv_dt0 * dt;
  962.          step.positionCorrection = m_positionCorrection;
  963.          step.warmStarting = m_warmStarting;
  964.          m_contactManager.Collide();
  965.          if(step.dt > 0)
  966.          {
  967.             Solve(step);
  968.          }
  969.          if(m_continuousPhysics && step.dt > 0)
  970.          {
  971.             SolveTOI(step);
  972.          }
  973.          DrawDebugData();
  974.          m_inv_dt0 = step.inv_dt;
  975.          m_lock = false;
  976.       }
  977.       
  978.       public function GetBodyCount() : int
  979.       {
  980.          return m_bodyCount;
  981.       }
  982.       
  983.       public function GetJointCount() : int
  984.       {
  985.          return m_jointCount;
  986.       }
  987.    }
  988. }
  989.