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

  1. package Box2D.Dynamics.Contacts
  2. {
  3.    import Box2D.Collision.Shapes.b2PolygonShape;
  4.    import Box2D.Collision.Shapes.b2Shape;
  5.    import Box2D.Collision.b2Collision;
  6.    import Box2D.Collision.b2ContactPoint;
  7.    import Box2D.Collision.b2Manifold;
  8.    import Box2D.Collision.b2ManifoldPoint;
  9.    import Box2D.Common.Math.b2Vec2;
  10.    import Box2D.Dynamics.b2Body;
  11.    import Box2D.Dynamics.b2ContactListener;
  12.    
  13.    public class b2PolygonContact extends b2Contact
  14.    {
  15.       
  16.       private static const s_evalCP:b2ContactPoint = new b2ContactPoint();
  17.        
  18.       
  19.       private var m_manifolds:Array;
  20.       
  21.       private var m0:b2Manifold;
  22.       
  23.       public var m_manifold:b2Manifold;
  24.       
  25.       public function b2PolygonContact(shape1:b2Shape, shape2:b2Shape)
  26.       {
  27.          m0 = new b2Manifold();
  28.          m_manifolds = [new b2Manifold()];
  29.          super(shape1,shape2);
  30.          m_manifold = m_manifolds[0];
  31.          m_manifold.pointCount = 0;
  32.       }
  33.       
  34.       public static function Destroy(contact:b2Contact, allocator:*) : void
  35.       {
  36.       }
  37.       
  38.       public static function Create(shape1:b2Shape, shape2:b2Shape, allocator:*) : b2Contact
  39.       {
  40.          return new b2PolygonContact(shape1,shape2);
  41.       }
  42.       
  43.       override public function Evaluate(listener:b2ContactListener) : void
  44.       {
  45.          var v1:b2Vec2 = null;
  46.          var v2:b2Vec2 = null;
  47.          var mp0:b2ManifoldPoint = null;
  48.          var cp:b2ContactPoint = null;
  49.          var i:int = 0;
  50.          var mp:b2ManifoldPoint = null;
  51.          var found:Boolean = false;
  52.          var idKey:uint = 0;
  53.          var j:int = 0;
  54.          var b1:b2Body = m_shape1.m_body;
  55.          var b2:b2Body = m_shape2.m_body;
  56.          m0.Set(m_manifold);
  57.          b2Collision.b2CollidePolygons(m_manifold,m_shape1 as b2PolygonShape,b1.m_xf,m_shape2 as b2PolygonShape,b2.m_xf);
  58.          var persisted:Array = [false,false];
  59.          cp = s_evalCP;
  60.          cp.shape1 = m_shape1;
  61.          cp.shape2 = m_shape2;
  62.          cp.friction = m_friction;
  63.          cp.restitution = m_restitution;
  64.          if(m_manifold.pointCount > 0)
  65.          {
  66.             for(i = 0; i < m_manifold.pointCount; i++)
  67.             {
  68.                mp = m_manifold.points[i];
  69.                mp.normalImpulse = 0;
  70.                mp.tangentImpulse = 0;
  71.                found = false;
  72.                idKey = mp.id._key;
  73.                for(j = 0; j < m0.pointCount; j++)
  74.                {
  75.                   if(persisted[j] != true)
  76.                   {
  77.                      mp0 = m0.points[j];
  78.                      if(mp0.id._key == idKey)
  79.                      {
  80.                         persisted[j] = true;
  81.                         mp.normalImpulse = mp0.normalImpulse;
  82.                         mp.tangentImpulse = mp0.tangentImpulse;
  83.                         found = true;
  84.                         if(listener != null)
  85.                         {
  86.                            cp.position = b1.GetWorldPoint(mp.localPoint1);
  87.                            v1 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
  88.                            v2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
  89.                            cp.velocity.Set(v2.x - v1.x,v2.y - v1.y);
  90.                            cp.normal.SetV(m_manifold.normal);
  91.                            cp.separation = mp.separation;
  92.                            cp.id.key = idKey;
  93.                            listener.Persist(cp);
  94.                         }
  95.                         break;
  96.                      }
  97.                   }
  98.                }
  99.                if(found == false && listener != null)
  100.                {
  101.                   cp.position = b1.GetWorldPoint(mp.localPoint1);
  102.                   v1 = b1.GetLinearVelocityFromLocalPoint(mp.localPoint1);
  103.                   v2 = b2.GetLinearVelocityFromLocalPoint(mp.localPoint2);
  104.                   cp.velocity.Set(v2.x - v1.x,v2.y - v1.y);
  105.                   cp.normal.SetV(m_manifold.normal);
  106.                   cp.separation = mp.separation;
  107.                   cp.id.key = idKey;
  108.                   listener.Add(cp);
  109.                }
  110.             }
  111.             m_manifoldCount = 1;
  112.          }
  113.          else
  114.          {
  115.             m_manifoldCount = 0;
  116.          }
  117.          if(listener == null)
  118.          {
  119.             return;
  120.          }
  121.          for(i = 0; i < m0.pointCount; i++)
  122.          {
  123.             if(!persisted[i])
  124.             {
  125.                mp0 = m0.points[i];
  126.                cp.position = b1.GetWorldPoint(mp0.localPoint1);
  127.                v1 = b1.GetLinearVelocityFromLocalPoint(mp0.localPoint1);
  128.                v2 = b2.GetLinearVelocityFromLocalPoint(mp0.localPoint2);
  129.                cp.velocity.Set(v2.x - v1.x,v2.y - v1.y);
  130.                cp.normal.SetV(m0.normal);
  131.                cp.separation = mp0.separation;
  132.                cp.id.key = mp0.id._key;
  133.                listener.Remove(cp);
  134.             }
  135.          }
  136.       }
  137.       
  138.       override public function GetManifolds() : Array
  139.       {
  140.          return m_manifolds;
  141.       }
  142.    }
  143. }
  144.