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

  1. package Box2D.Dynamics
  2. {
  3.    import Box2D.Collision.b2Manifold;
  4.    import Box2D.Collision.b2ManifoldPoint;
  5.    import Box2D.Common.Math.b2Mat22;
  6.    import Box2D.Common.Math.b2Math;
  7.    import Box2D.Common.Math.b2Vec2;
  8.    import Box2D.Common.b2Settings;
  9.    import Box2D.Dynamics.Contacts.b2Contact;
  10.    import Box2D.Dynamics.Contacts.b2ContactConstraint;
  11.    import Box2D.Dynamics.Contacts.b2ContactConstraintPoint;
  12.    import Box2D.Dynamics.Contacts.b2ContactResult;
  13.    import Box2D.Dynamics.Contacts.b2ContactSolver;
  14.    import Box2D.Dynamics.Joints.b2Joint;
  15.    
  16.    public class b2Island
  17.    {
  18.       
  19.       private static var s_reportCR:b2ContactResult = new b2ContactResult();
  20.        
  21.       
  22.       public var m_listener:b2ContactListener;
  23.       
  24.       public var m_positionIterationCount:int;
  25.       
  26.       public var m_bodyCapacity:int;
  27.       
  28.       public var m_bodies:Array;
  29.       
  30.       public var m_joints:Array;
  31.       
  32.       public var m_jointCapacity:int;
  33.       
  34.       public var m_contactCount:int;
  35.       
  36.       public var m_contacts:Array;
  37.       
  38.       public var m_contactCapacity:int;
  39.       
  40.       public var m_jointCount:int;
  41.       
  42.       public var m_allocator:*;
  43.       
  44.       public var m_bodyCount:int;
  45.       
  46.       public function b2Island(bodyCapacity:int, contactCapacity:int, jointCapacity:int, allocator:*, listener:b2ContactListener)
  47.       {
  48.          var i:int = 0;
  49.          super();
  50.          m_bodyCapacity = bodyCapacity;
  51.          m_contactCapacity = contactCapacity;
  52.          m_jointCapacity = jointCapacity;
  53.          m_bodyCount = 0;
  54.          m_contactCount = 0;
  55.          m_jointCount = 0;
  56.          m_allocator = allocator;
  57.          m_listener = listener;
  58.          m_bodies = new Array(bodyCapacity);
  59.          for(i = 0; i < bodyCapacity; i++)
  60.          {
  61.             m_bodies[i] = null;
  62.          }
  63.          m_contacts = new Array(contactCapacity);
  64.          for(i = 0; i < contactCapacity; i++)
  65.          {
  66.             m_contacts[i] = null;
  67.          }
  68.          m_joints = new Array(jointCapacity);
  69.          for(i = 0; i < jointCapacity; i++)
  70.          {
  71.             m_joints[i] = null;
  72.          }
  73.          m_positionIterationCount = 0;
  74.       }
  75.       
  76.       public function AddBody(body:b2Body) : void
  77.       {
  78.          var _loc2_:* = m_bodyCount++;
  79.          m_bodies[_loc2_] = body;
  80.       }
  81.       
  82.       public function AddJoint(joint:b2Joint) : void
  83.       {
  84.          var _loc2_:* = m_jointCount++;
  85.          m_joints[_loc2_] = joint;
  86.       }
  87.       
  88.       public function Report(constraints:Array) : void
  89.       {
  90.          var tMat:b2Mat22 = null;
  91.          var tVec:b2Vec2 = null;
  92.          var c:b2Contact = null;
  93.          var cc:b2ContactConstraint = null;
  94.          var cr:b2ContactResult = null;
  95.          var b1:b2Body = null;
  96.          var manifoldCount:int = 0;
  97.          var manifolds:Array = null;
  98.          var j:int = 0;
  99.          var manifold:b2Manifold = null;
  100.          var k:int = 0;
  101.          var point:b2ManifoldPoint = null;
  102.          var ccp:b2ContactConstraintPoint = null;
  103.          if(m_listener == null)
  104.          {
  105.             return;
  106.          }
  107.          for(var i:int = 0; i < m_contactCount; i++)
  108.          {
  109.             c = m_contacts[i];
  110.             cc = constraints[i];
  111.             cr = s_reportCR;
  112.             cr.shape1 = c.m_shape1;
  113.             cr.shape2 = c.m_shape2;
  114.             b1 = cr.shape1.m_body;
  115.             manifoldCount = c.m_manifoldCount;
  116.             manifolds = c.GetManifolds();
  117.             for(j = 0; j < manifoldCount; j++)
  118.             {
  119.                manifold = manifolds[j];
  120.                cr.normal.SetV(manifold.normal);
  121.                for(k = 0; k < manifold.pointCount; k++)
  122.                {
  123.                   point = manifold.points[k];
  124.                   ccp = cc.points[k];
  125.                   cr.position = b1.GetWorldPoint(point.localPoint1);
  126.                   cr.normalImpulse = ccp.normalImpulse;
  127.                   cr.tangentImpulse = ccp.tangentImpulse;
  128.                   cr.id.key = point.id.key;
  129.                   m_listener.Result(cr);
  130.                }
  131.             }
  132.          }
  133.       }
  134.       
  135.       public function AddContact(contact:b2Contact) : void
  136.       {
  137.          var _loc2_:* = m_contactCount++;
  138.          m_contacts[_loc2_] = contact;
  139.       }
  140.       
  141.       public function Solve(step:b2TimeStep, gravity:b2Vec2, correctPositions:Boolean, allowSleep:Boolean) : void
  142.       {
  143.          var i:int = 0;
  144.          var b:b2Body = null;
  145.          var joint:b2Joint = null;
  146.          var j:int = 0;
  147.          var contactsOkay:Boolean = false;
  148.          var jointsOkay:Boolean = false;
  149.          var jointOkay:Boolean = false;
  150.          var minSleepTime:Number = NaN;
  151.          var linTolSqr:Number = NaN;
  152.          var angTolSqr:Number = NaN;
  153.          for(i = 0; i < m_bodyCount; i++)
  154.          {
  155.             b = m_bodies[i];
  156.             if(!b.IsStatic())
  157.             {
  158.                b.m_linearVelocity.x += step.dt * (gravity.x + b.m_invMass * b.m_force.x);
  159.                b.m_linearVelocity.y += step.dt * (gravity.y + b.m_invMass * b.m_force.y);
  160.                b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
  161.                b.m_force.SetZero();
  162.                b.m_torque = 0;
  163.                b.m_linearVelocity.Multiply(b2Math.b2Clamp(1 - step.dt * b.m_linearDamping,0,1));
  164.                b.m_angularVelocity *= b2Math.b2Clamp(1 - step.dt * b.m_angularDamping,0,1);
  165.                if(b.m_linearVelocity.LengthSquared() > b2Settings.b2_maxLinearVelocitySquared)
  166.                {
  167.                   b.m_linearVelocity.Normalize();
  168.                   b.m_linearVelocity.x *= b2Settings.b2_maxLinearVelocity;
  169.                   b.m_linearVelocity.y *= b2Settings.b2_maxLinearVelocity;
  170.                }
  171.                if(b.m_angularVelocity * b.m_angularVelocity > b2Settings.b2_maxAngularVelocitySquared)
  172.                {
  173.                   if(b.m_angularVelocity < 0)
  174.                   {
  175.                      b.m_angularVelocity = -b2Settings.b2_maxAngularVelocity;
  176.                   }
  177.                   else
  178.                   {
  179.                      b.m_angularVelocity = b2Settings.b2_maxAngularVelocity;
  180.                   }
  181.                }
  182.             }
  183.          }
  184.          var contactSolver:b2ContactSolver = new b2ContactSolver(step,m_contacts,m_contactCount,m_allocator);
  185.          contactSolver.InitVelocityConstraints(step);
  186.          for(i = 0; i < m_jointCount; i++)
  187.          {
  188.             joint = m_joints[i];
  189.             joint.InitVelocityConstraints(step);
  190.          }
  191.          for(i = 0; i < step.maxIterations; i++)
  192.          {
  193.             contactSolver.SolveVelocityConstraints();
  194.             for(j = 0; j < m_jointCount; j++)
  195.             {
  196.                joint = m_joints[j];
  197.                joint.SolveVelocityConstraints(step);
  198.             }
  199.          }
  200.          contactSolver.FinalizeVelocityConstraints();
  201.          for(i = 0; i < m_bodyCount; i++)
  202.          {
  203.             b = m_bodies[i];
  204.             if(!b.IsStatic())
  205.             {
  206.                b.m_sweep.c0.SetV(b.m_sweep.c);
  207.                b.m_sweep.a0 = b.m_sweep.a;
  208.                b.m_sweep.c.x += step.dt * b.m_linearVelocity.x;
  209.                b.m_sweep.c.y += step.dt * b.m_linearVelocity.y;
  210.                b.m_sweep.a += step.dt * b.m_angularVelocity;
  211.                b.SynchronizeTransform();
  212.             }
  213.          }
  214.          if(correctPositions)
  215.          {
  216.             for(i = 0; i < m_jointCount; i++)
  217.             {
  218.                joint = m_joints[i];
  219.                joint.InitPositionConstraints();
  220.             }
  221.             for(m_positionIterationCount = 0; m_positionIterationCount < step.maxIterations; ++m_positionIterationCount)
  222.             {
  223.                contactsOkay = contactSolver.SolvePositionConstraints(b2Settings.b2_contactBaumgarte);
  224.                jointsOkay = true;
  225.                for(i = 0; i < m_jointCount; i++)
  226.                {
  227.                   joint = m_joints[i];
  228.                   jointOkay = joint.SolvePositionConstraints();
  229.                   jointsOkay &&= jointOkay;
  230.                }
  231.                if(contactsOkay && jointsOkay)
  232.                {
  233.                   break;
  234.                }
  235.             }
  236.          }
  237.          Report(contactSolver.m_constraints);
  238.          if(allowSleep)
  239.          {
  240.             minSleepTime = Number.MAX_VALUE;
  241.             linTolSqr = b2Settings.b2_linearSleepTolerance * b2Settings.b2_linearSleepTolerance;
  242.             angTolSqr = b2Settings.b2_angularSleepTolerance * b2Settings.b2_angularSleepTolerance;
  243.             for(i = 0; i < m_bodyCount; i++)
  244.             {
  245.                b = m_bodies[i];
  246.                if(b.m_invMass != 0)
  247.                {
  248.                   if((b.m_flags & b2Body.e_allowSleepFlag) == 0)
  249.                   {
  250.                      b.m_sleepTime = 0;
  251.                      minSleepTime = 0;
  252.                   }
  253.                   if((b.m_flags & b2Body.e_allowSleepFlag) == 0 || b.m_angularVelocity * b.m_angularVelocity > angTolSqr || b2Math.b2Dot(b.m_linearVelocity,b.m_linearVelocity) > linTolSqr)
  254.                   {
  255.                      b.m_sleepTime = 0;
  256.                      minSleepTime = 0;
  257.                   }
  258.                   else
  259.                   {
  260.                      b.m_sleepTime += step.dt;
  261.                      minSleepTime = b2Math.b2Min(minSleepTime,b.m_sleepTime);
  262.                   }
  263.                }
  264.             }
  265.             if(minSleepTime >= b2Settings.b2_timeToSleep)
  266.             {
  267.                for(i = 0; i < m_bodyCount; i++)
  268.                {
  269.                   b = m_bodies[i];
  270.                   b.m_flags |= b2Body.e_sleepFlag;
  271.                   b.m_linearVelocity.SetZero();
  272.                   b.m_angularVelocity = 0;
  273.                }
  274.             }
  275.          }
  276.       }
  277.       
  278.       public function Clear() : void
  279.       {
  280.          m_bodyCount = 0;
  281.          m_contactCount = 0;
  282.          m_jointCount = 0;
  283.       }
  284.       
  285.       public function SolveTOI(subStep:b2TimeStep) : void
  286.       {
  287.          var i:int = 0;
  288.          var b:b2Body = null;
  289.          var contactsOkay:Boolean = false;
  290.          var contactSolver:b2ContactSolver = new b2ContactSolver(subStep,m_contacts,m_contactCount,m_allocator);
  291.          for(i = 0; i < subStep.maxIterations; i++)
  292.          {
  293.             contactSolver.SolveVelocityConstraints();
  294.          }
  295.          for(i = 0; i < m_bodyCount; i++)
  296.          {
  297.             b = m_bodies[i];
  298.             if(!b.IsStatic())
  299.             {
  300.                b.m_sweep.c0.SetV(b.m_sweep.c);
  301.                b.m_sweep.a0 = b.m_sweep.a;
  302.                b.m_sweep.c.x += subStep.dt * b.m_linearVelocity.x;
  303.                b.m_sweep.c.y += subStep.dt * b.m_linearVelocity.y;
  304.                b.m_sweep.a += subStep.dt * b.m_angularVelocity;
  305.                b.SynchronizeTransform();
  306.             }
  307.          }
  308.          var k_toiBaumgarte:Number = 0.75;
  309.          for(i = 0; i < subStep.maxIterations; i++)
  310.          {
  311.             contactsOkay = contactSolver.SolvePositionConstraints(k_toiBaumgarte);
  312.             if(contactsOkay)
  313.             {
  314.                break;
  315.             }
  316.          }
  317.          Report(contactSolver.m_constraints);
  318.       }
  319.    }
  320. }
  321.