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

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