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

  1. package Box2D.Dynamics
  2. {
  3.    import Box2D.Collision.Shapes.b2MassData;
  4.    import Box2D.Collision.Shapes.b2Shape;
  5.    import Box2D.Collision.Shapes.b2ShapeDef;
  6.    import Box2D.Common.Math.b2Mat22;
  7.    import Box2D.Common.Math.b2Math;
  8.    import Box2D.Common.Math.b2Sweep;
  9.    import Box2D.Common.Math.b2Vec2;
  10.    import Box2D.Common.Math.b2XForm;
  11.    import Box2D.Dynamics.Contacts.b2ContactEdge;
  12.    import Box2D.Dynamics.Joints.b2JointEdge;
  13.    
  14.    public class b2Body
  15.    {
  16.       
  17.       public static var e_fixedRotationFlag:uint = 64;
  18.       
  19.       public static var e_frozenFlag:uint = 2;
  20.       
  21.       public static var e_maxTypes:uint = 3;
  22.       
  23.       public static var e_sleepFlag:uint = 8;
  24.       
  25.       private static var s_massData:b2MassData = new b2MassData();
  26.       
  27.       public static var e_bulletFlag:uint = 32;
  28.       
  29.       public static var e_staticType:uint = 1;
  30.       
  31.       public static var e_islandFlag:uint = 4;
  32.       
  33.       public static var e_allowSleepFlag:uint = 16;
  34.       
  35.       private static var s_xf1:b2XForm = new b2XForm();
  36.       
  37.       public static var e_dynamicType:uint = 2;
  38.        
  39.       
  40.       public var m_next:b2Body;
  41.       
  42.       public var m_xf:b2XForm;
  43.       
  44.       public var m_contactList:b2ContactEdge;
  45.       
  46.       public var m_angularVelocity:Number;
  47.       
  48.       public var m_shapeList:b2Shape;
  49.       
  50.       public var m_force:b2Vec2;
  51.       
  52.       public var m_mass:Number;
  53.       
  54.       public var m_sweep:b2Sweep;
  55.       
  56.       public var m_torque:Number;
  57.       
  58.       public var m_userData:*;
  59.       
  60.       public var m_flags:uint;
  61.       
  62.       public var m_world:b2World;
  63.       
  64.       public var m_prev:b2Body;
  65.       
  66.       public var m_invMass:Number;
  67.       
  68.       public var m_type:int;
  69.       
  70.       public var m_linearDamping:Number;
  71.       
  72.       public var m_shapeCount:int;
  73.       
  74.       public var m_angularDamping:Number;
  75.       
  76.       public var m_invI:Number;
  77.       
  78.       public var m_linearVelocity:b2Vec2;
  79.       
  80.       public var m_sleepTime:Number;
  81.       
  82.       public var m_jointList:b2JointEdge;
  83.       
  84.       public var m_I:Number;
  85.       
  86.       public function b2Body(bd:b2BodyDef, world:b2World)
  87.       {
  88.          m_xf = new b2XForm();
  89.          m_sweep = new b2Sweep();
  90.          m_linearVelocity = new b2Vec2();
  91.          m_force = new b2Vec2();
  92.          super();
  93.          m_flags = 0;
  94.          if(bd.isBullet)
  95.          {
  96.             m_flags |= e_bulletFlag;
  97.          }
  98.          if(bd.fixedRotation)
  99.          {
  100.             m_flags |= e_fixedRotationFlag;
  101.          }
  102.          if(bd.allowSleep)
  103.          {
  104.             m_flags |= e_allowSleepFlag;
  105.          }
  106.          if(bd.isSleeping)
  107.          {
  108.             m_flags |= e_sleepFlag;
  109.          }
  110.          m_world = world;
  111.          m_xf.position.SetV(bd.position);
  112.          m_xf.R.Set(bd.angle);
  113.          m_sweep.localCenter.SetV(bd.massData.center);
  114.          m_sweep.t0 = 1;
  115.          m_sweep.a0 = m_sweep.a = bd.angle;
  116.          var tMat:b2Mat22 = m_xf.R;
  117.          var tVec:b2Vec2 = m_sweep.localCenter;
  118.          m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  119.          m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  120.          m_sweep.c.x += m_xf.position.x;
  121.          m_sweep.c.y += m_xf.position.y;
  122.          m_sweep.c0.SetV(m_sweep.c);
  123.          m_jointList = null;
  124.          m_contactList = null;
  125.          m_prev = null;
  126.          m_next = null;
  127.          m_linearDamping = bd.linearDamping;
  128.          m_angularDamping = bd.angularDamping;
  129.          m_force.Set(0,0);
  130.          m_torque = 0;
  131.          m_linearVelocity.SetZero();
  132.          m_angularVelocity = 0;
  133.          m_sleepTime = 0;
  134.          m_invMass = 0;
  135.          m_I = 0;
  136.          m_invI = 0;
  137.          m_mass = bd.massData.mass;
  138.          if(m_mass > 0)
  139.          {
  140.             m_invMass = 1 / m_mass;
  141.          }
  142.          if((m_flags & b2Body.e_fixedRotationFlag) == 0)
  143.          {
  144.             m_I = bd.massData.I;
  145.          }
  146.          if(m_I > 0)
  147.          {
  148.             m_invI = 1 / m_I;
  149.          }
  150.          if(m_invMass == 0 && m_invI == 0)
  151.          {
  152.             m_type = e_staticType;
  153.          }
  154.          else
  155.          {
  156.             m_type = e_dynamicType;
  157.          }
  158.          m_userData = bd.userData;
  159.          m_shapeList = null;
  160.          m_shapeCount = 0;
  161.       }
  162.       
  163.       public function GetLinearVelocityFromWorldPoint(worldPoint:b2Vec2) : b2Vec2
  164.       {
  165.          return new b2Vec2(m_linearVelocity.x + m_angularVelocity * (worldPoint.y - m_sweep.c.y),m_linearVelocity.x - m_angularVelocity * (worldPoint.x - m_sweep.c.x));
  166.       }
  167.       
  168.       public function SetLinearVelocity(v:b2Vec2) : void
  169.       {
  170.          m_linearVelocity.SetV(v);
  171.       }
  172.       
  173.       public function WakeUp() : void
  174.       {
  175.          m_flags &= ~e_sleepFlag;
  176.          m_sleepTime = 0;
  177.       }
  178.       
  179.       public function GetLocalCenter() : b2Vec2
  180.       {
  181.          return m_sweep.localCenter;
  182.       }
  183.       
  184.       public function ApplyTorque(torque:Number) : void
  185.       {
  186.          if(IsSleeping())
  187.          {
  188.             WakeUp();
  189.          }
  190.          m_torque += torque;
  191.       }
  192.       
  193.       public function IsFrozen() : Boolean
  194.       {
  195.          return (m_flags & e_frozenFlag) == e_frozenFlag;
  196.       }
  197.       
  198.       public function IsDynamic() : Boolean
  199.       {
  200.          return m_type == e_dynamicType;
  201.       }
  202.       
  203.       public function GetLinearVelocity() : b2Vec2
  204.       {
  205.          return m_linearVelocity;
  206.       }
  207.       
  208.       public function SynchronizeTransform() : void
  209.       {
  210.          m_xf.R.Set(m_sweep.a);
  211.          var tMat:b2Mat22 = m_xf.R;
  212.          var tVec:b2Vec2 = m_sweep.localCenter;
  213.          m_xf.position.x = m_sweep.c.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  214.          m_xf.position.y = m_sweep.c.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  215.       }
  216.       
  217.       public function GetInertia() : Number
  218.       {
  219.          return m_I;
  220.       }
  221.       
  222.       public function IsSleeping() : Boolean
  223.       {
  224.          return (m_flags & e_sleepFlag) == e_sleepFlag;
  225.       }
  226.       
  227.       public function SetMassFromShapes() : void
  228.       {
  229.          var s:b2Shape = null;
  230.          if(m_world.m_lock == true)
  231.          {
  232.             return;
  233.          }
  234.          m_mass = 0;
  235.          m_invMass = 0;
  236.          m_I = 0;
  237.          m_invI = 0;
  238.          var centerX:Number = 0;
  239.          var centerY:Number = 0;
  240.          var massData:b2MassData = s_massData;
  241.          s = m_shapeList;
  242.          while(s)
  243.          {
  244.             s.ComputeMass(massData);
  245.             m_mass += massData.mass;
  246.             centerX += massData.mass * massData.center.x;
  247.             centerY += massData.mass * massData.center.y;
  248.             m_I += massData.I;
  249.             s = s.m_next;
  250.          }
  251.          if(m_mass > 0)
  252.          {
  253.             m_invMass = 1 / m_mass;
  254.             centerX *= m_invMass;
  255.             centerY *= m_invMass;
  256.          }
  257.          if(m_I > 0 && (m_flags & e_fixedRotationFlag) == 0)
  258.          {
  259.             m_I -= m_mass * (centerX * centerX + centerY * centerY);
  260.             m_invI = 1 / m_I;
  261.          }
  262.          else
  263.          {
  264.             m_I = 0;
  265.             m_invI = 0;
  266.          }
  267.          m_sweep.localCenter.Set(centerX,centerY);
  268.          var tMat:b2Mat22 = m_xf.R;
  269.          var tVec:b2Vec2 = m_sweep.localCenter;
  270.          m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  271.          m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  272.          m_sweep.c.x += m_xf.position.x;
  273.          m_sweep.c.y += m_xf.position.y;
  274.          m_sweep.c0.SetV(m_sweep.c);
  275.          s = m_shapeList;
  276.          while(s)
  277.          {
  278.             s.UpdateSweepRadius(m_sweep.localCenter);
  279.             s = s.m_next;
  280.          }
  281.          var oldType:int = m_type;
  282.          if(m_invMass == 0 && m_invI == 0)
  283.          {
  284.             m_type = e_staticType;
  285.          }
  286.          else
  287.          {
  288.             m_type = e_dynamicType;
  289.          }
  290.          if(oldType != m_type)
  291.          {
  292.             s = m_shapeList;
  293.             while(s)
  294.             {
  295.                s.RefilterProxy(m_world.m_broadPhase,m_xf);
  296.                s = s.m_next;
  297.             }
  298.          }
  299.       }
  300.       
  301.       public function PutToSleep() : void
  302.       {
  303.          m_flags |= e_sleepFlag;
  304.          m_sleepTime = 0;
  305.          m_linearVelocity.SetZero();
  306.          m_angularVelocity = 0;
  307.          m_force.SetZero();
  308.          m_torque = 0;
  309.       }
  310.       
  311.       public function GetJointList() : b2JointEdge
  312.       {
  313.          return m_jointList;
  314.       }
  315.       
  316.       public function SetXForm(position:b2Vec2, angle:Number) : Boolean
  317.       {
  318.          var s:b2Shape = null;
  319.          var inRange:Boolean = false;
  320.          if(m_world.m_lock == true)
  321.          {
  322.             return true;
  323.          }
  324.          if(IsFrozen())
  325.          {
  326.             return false;
  327.          }
  328.          m_xf.R.Set(angle);
  329.          m_xf.position.SetV(position);
  330.          var tMat:b2Mat22 = m_xf.R;
  331.          var tVec:b2Vec2 = m_sweep.localCenter;
  332.          m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  333.          m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  334.          m_sweep.c.x += m_xf.position.x;
  335.          m_sweep.c.y += m_xf.position.y;
  336.          m_sweep.c0.SetV(m_sweep.c);
  337.          m_sweep.a0 = m_sweep.a = angle;
  338.          var freeze:Boolean = false;
  339.          s = m_shapeList;
  340.          while(s)
  341.          {
  342.             inRange = s.Synchronize(m_world.m_broadPhase,m_xf,m_xf);
  343.             if(inRange == false)
  344.             {
  345.                freeze = true;
  346.                break;
  347.             }
  348.             s = s.m_next;
  349.          }
  350.          if(freeze == true)
  351.          {
  352.             m_flags |= e_frozenFlag;
  353.             m_linearVelocity.SetZero();
  354.             m_angularVelocity = 0;
  355.             s = m_shapeList;
  356.             while(s)
  357.             {
  358.                s.DestroyProxy(m_world.m_broadPhase);
  359.                s = s.m_next;
  360.             }
  361.             return false;
  362.          }
  363.          m_world.m_broadPhase.Commit();
  364.          return true;
  365.       }
  366.       
  367.       public function GetLocalPoint(worldPoint:b2Vec2) : b2Vec2
  368.       {
  369.          return b2Math.b2MulXT(m_xf,worldPoint);
  370.       }
  371.       
  372.       public function ApplyForce(force:b2Vec2, point:b2Vec2) : void
  373.       {
  374.          if(IsSleeping())
  375.          {
  376.             WakeUp();
  377.          }
  378.          m_force.x += force.x;
  379.          m_force.y += force.y;
  380.          m_torque += (point.x - m_sweep.c.x) * force.y - (point.y - m_sweep.c.y) * force.x;
  381.       }
  382.       
  383.       public function SynchronizeShapes() : Boolean
  384.       {
  385.          var s:b2Shape = null;
  386.          var xf1:b2XForm = s_xf1;
  387.          xf1.R.Set(m_sweep.a0);
  388.          var tMat:b2Mat22 = xf1.R;
  389.          var tVec:b2Vec2 = m_sweep.localCenter;
  390.          xf1.position.x = m_sweep.c0.x - (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  391.          xf1.position.y = m_sweep.c0.y - (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  392.          var inRange:Boolean = true;
  393.          s = m_shapeList;
  394.          while(s)
  395.          {
  396.             inRange = s.Synchronize(m_world.m_broadPhase,xf1,m_xf);
  397.             if(inRange == false)
  398.             {
  399.                break;
  400.             }
  401.             s = s.m_next;
  402.          }
  403.          if(inRange == false)
  404.          {
  405.             m_flags |= e_frozenFlag;
  406.             m_linearVelocity.SetZero();
  407.             m_angularVelocity = 0;
  408.             s = m_shapeList;
  409.             while(s)
  410.             {
  411.                s.DestroyProxy(m_world.m_broadPhase);
  412.                s = s.m_next;
  413.             }
  414.             return false;
  415.          }
  416.          return true;
  417.       }
  418.       
  419.       public function GetAngle() : Number
  420.       {
  421.          return m_sweep.a;
  422.       }
  423.       
  424.       public function GetXForm() : b2XForm
  425.       {
  426.          return m_xf;
  427.       }
  428.       
  429.       public function GetLinearVelocityFromLocalPoint(localPoint:b2Vec2) : b2Vec2
  430.       {
  431.          var A:b2Mat22 = m_xf.R;
  432.          var worldPoint:b2Vec2 = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y,A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  433.          worldPoint.x += m_xf.position.x;
  434.          worldPoint.y += m_xf.position.y;
  435.          return new b2Vec2(m_linearVelocity.x + m_angularVelocity * (worldPoint.y - m_sweep.c.y),m_linearVelocity.x - m_angularVelocity * (worldPoint.x - m_sweep.c.x));
  436.       }
  437.       
  438.       public function GetNext() : b2Body
  439.       {
  440.          return m_next;
  441.       }
  442.       
  443.       public function GetMass() : Number
  444.       {
  445.          return m_mass;
  446.       }
  447.       
  448.       public function ApplyImpulse(impulse:b2Vec2, point:b2Vec2) : void
  449.       {
  450.          if(IsSleeping())
  451.          {
  452.             WakeUp();
  453.          }
  454.          m_linearVelocity.x += m_invMass * impulse.x;
  455.          m_linearVelocity.y += m_invMass * impulse.y;
  456.          m_angularVelocity += m_invI * ((point.x - m_sweep.c.x) * impulse.y - (point.y - m_sweep.c.y) * impulse.x);
  457.       }
  458.       
  459.       public function GetAngularVelocity() : Number
  460.       {
  461.          return m_angularVelocity;
  462.       }
  463.       
  464.       public function SetAngularVelocity(omega:Number) : void
  465.       {
  466.          m_angularVelocity = omega;
  467.       }
  468.       
  469.       public function SetMass(massData:b2MassData) : void
  470.       {
  471.          var s:b2Shape = null;
  472.          if(m_world.m_lock == true)
  473.          {
  474.             return;
  475.          }
  476.          m_invMass = 0;
  477.          m_I = 0;
  478.          m_invI = 0;
  479.          m_mass = massData.mass;
  480.          if(m_mass > 0)
  481.          {
  482.             m_invMass = 1 / m_mass;
  483.          }
  484.          if((m_flags & b2Body.e_fixedRotationFlag) == 0)
  485.          {
  486.             m_I = massData.I;
  487.          }
  488.          if(m_I > 0)
  489.          {
  490.             m_invI = 1 / m_I;
  491.          }
  492.          m_sweep.localCenter.SetV(massData.center);
  493.          var tMat:b2Mat22 = m_xf.R;
  494.          var tVec:b2Vec2 = m_sweep.localCenter;
  495.          m_sweep.c.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  496.          m_sweep.c.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  497.          m_sweep.c.x += m_xf.position.x;
  498.          m_sweep.c.y += m_xf.position.y;
  499.          m_sweep.c0.SetV(m_sweep.c);
  500.          s = m_shapeList;
  501.          while(s)
  502.          {
  503.             s.UpdateSweepRadius(m_sweep.localCenter);
  504.             s = s.m_next;
  505.          }
  506.          var oldType:int = m_type;
  507.          if(m_invMass == 0 && m_invI == 0)
  508.          {
  509.             m_type = e_staticType;
  510.          }
  511.          else
  512.          {
  513.             m_type = e_dynamicType;
  514.          }
  515.          if(oldType != m_type)
  516.          {
  517.             s = m_shapeList;
  518.             while(s)
  519.             {
  520.                s.RefilterProxy(m_world.m_broadPhase,m_xf);
  521.                s = s.m_next;
  522.             }
  523.          }
  524.       }
  525.       
  526.       public function IsStatic() : Boolean
  527.       {
  528.          return m_type == e_staticType;
  529.       }
  530.       
  531.       public function GetWorldVector(localVector:b2Vec2) : b2Vec2
  532.       {
  533.          return b2Math.b2MulMV(m_xf.R,localVector);
  534.       }
  535.       
  536.       public function GetShapeList() : b2Shape
  537.       {
  538.          return m_shapeList;
  539.       }
  540.       
  541.       public function Advance(t:Number) : void
  542.       {
  543.          m_sweep.Advance(t);
  544.          m_sweep.c.SetV(m_sweep.c0);
  545.          m_sweep.a = m_sweep.a0;
  546.          SynchronizeTransform();
  547.       }
  548.       
  549.       public function SetBullet(flag:Boolean) : void
  550.       {
  551.          if(flag)
  552.          {
  553.             m_flags |= e_bulletFlag;
  554.          }
  555.          else
  556.          {
  557.             m_flags &= ~e_bulletFlag;
  558.          }
  559.       }
  560.       
  561.       public function CreateShape(def:b2ShapeDef) : b2Shape
  562.       {
  563.          var s:b2Shape = null;
  564.          if(m_world.m_lock == true)
  565.          {
  566.             return null;
  567.          }
  568.          s = b2Shape.Create(def,m_world.m_blockAllocator);
  569.          s.m_next = m_shapeList;
  570.          m_shapeList = s;
  571.          ++m_shapeCount;
  572.          s.m_body = this;
  573.          s.CreateProxy(m_world.m_broadPhase,m_xf);
  574.          s.UpdateSweepRadius(m_sweep.localCenter);
  575.          return s;
  576.       }
  577.       
  578.       public function IsConnected(other:b2Body) : Boolean
  579.       {
  580.          var jn:b2JointEdge = m_jointList;
  581.          while(jn)
  582.          {
  583.             if(jn.other == other)
  584.             {
  585.                return jn.joint.m_collideConnected == false;
  586.             }
  587.             jn = jn.next;
  588.          }
  589.          return false;
  590.       }
  591.       
  592.       public function DestroyShape(s:b2Shape) : void
  593.       {
  594.          if(m_world.m_lock == true)
  595.          {
  596.             return;
  597.          }
  598.          s.DestroyProxy(m_world.m_broadPhase);
  599.          var node:b2Shape = m_shapeList;
  600.          var ppS:b2Shape = null;
  601.          var found:Boolean = false;
  602.          while(node != null)
  603.          {
  604.             if(node == s)
  605.             {
  606.                if(ppS)
  607.                {
  608.                   ppS.m_next = s.m_next;
  609.                }
  610.                else
  611.                {
  612.                   m_shapeList = s.m_next;
  613.                }
  614.                found = true;
  615.                break;
  616.             }
  617.             ppS = node;
  618.             node = node.m_next;
  619.          }
  620.          s.m_body = null;
  621.          s.m_next = null;
  622.          --m_shapeCount;
  623.          b2Shape.Destroy(s,m_world.m_blockAllocator);
  624.       }
  625.       
  626.       public function GetUserData() : *
  627.       {
  628.          return m_userData;
  629.       }
  630.       
  631.       public function IsBullet() : Boolean
  632.       {
  633.          return (m_flags & e_bulletFlag) == e_bulletFlag;
  634.       }
  635.       
  636.       public function GetWorldCenter() : b2Vec2
  637.       {
  638.          return m_sweep.c;
  639.       }
  640.       
  641.       public function AllowSleeping(flag:Boolean) : void
  642.       {
  643.          if(flag)
  644.          {
  645.             m_flags |= e_allowSleepFlag;
  646.          }
  647.          else
  648.          {
  649.             m_flags &= ~e_allowSleepFlag;
  650.             WakeUp();
  651.          }
  652.       }
  653.       
  654.       public function SetUserData(data:*) : void
  655.       {
  656.          m_userData = data;
  657.       }
  658.       
  659.       public function GetLocalVector(worldVector:b2Vec2) : b2Vec2
  660.       {
  661.          return b2Math.b2MulTMV(m_xf.R,worldVector);
  662.       }
  663.       
  664.       public function GetWorldPoint(localPoint:b2Vec2) : b2Vec2
  665.       {
  666.          var A:b2Mat22 = m_xf.R;
  667.          var u:b2Vec2 = new b2Vec2(A.col1.x * localPoint.x + A.col2.x * localPoint.y,A.col1.y * localPoint.x + A.col2.y * localPoint.y);
  668.          u.x += m_xf.position.x;
  669.          u.y += m_xf.position.y;
  670.          return u;
  671.       }
  672.       
  673.       public function GetWorld() : b2World
  674.       {
  675.          return m_world;
  676.       }
  677.       
  678.       public function GetPosition() : b2Vec2
  679.       {
  680.          return m_xf.position;
  681.       }
  682.    }
  683. }
  684.