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

  1. package Box2D.Collision
  2. {
  3.    import Box2D.Collision.Shapes.b2CircleShape;
  4.    import Box2D.Collision.Shapes.b2PolygonShape;
  5.    import Box2D.Common.Math.b2Mat22;
  6.    import Box2D.Common.Math.b2Math;
  7.    import Box2D.Common.Math.b2Vec2;
  8.    import Box2D.Common.Math.b2XForm;
  9.    import Box2D.Common.b2Settings;
  10.    
  11.    public class b2Collision
  12.    {
  13.       
  14.       public static const b2_nullFeature:uint = 255;
  15.       
  16.       private static var b2CollidePolyTempVec:b2Vec2 = new b2Vec2();
  17.        
  18.       
  19.       public function b2Collision()
  20.       {
  21.          super();
  22.       }
  23.       
  24.       public static function EdgeSeparation(poly1:b2PolygonShape, xf1:b2XForm, edge1:int, poly2:b2PolygonShape, xf2:b2XForm) : Number
  25.       {
  26.          var tMat:b2Mat22 = null;
  27.          var tVec:b2Vec2 = null;
  28.          var dot:Number = NaN;
  29.          var count1:int = poly1.m_vertexCount;
  30.          var vertices1:Array = poly1.m_vertices;
  31.          var normals1:Array = poly1.m_normals;
  32.          var count2:int = poly2.m_vertexCount;
  33.          var vertices2:Array = poly2.m_vertices;
  34.          tMat = xf1.R;
  35.          tVec = normals1[edge1];
  36.          var normal1WorldX:Number = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  37.          var normal1WorldY:Number = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  38.          tMat = xf2.R;
  39.          var normal1X:Number = tMat.col1.x * normal1WorldX + tMat.col1.y * normal1WorldY;
  40.          var normal1Y:Number = tMat.col2.x * normal1WorldX + tMat.col2.y * normal1WorldY;
  41.          var index:int = 0;
  42.          var minDot:Number = Number.MAX_VALUE;
  43.          for(var i:int = 0; i < count2; i++)
  44.          {
  45.             tVec = vertices2[i];
  46.             dot = tVec.x * normal1X + tVec.y * normal1Y;
  47.             if(dot < minDot)
  48.             {
  49.                minDot = dot;
  50.                index = i;
  51.             }
  52.          }
  53.          tVec = vertices1[edge1];
  54.          tMat = xf1.R;
  55.          var v1X:Number = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  56.          var v1Y:Number = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  57.          tVec = vertices2[index];
  58.          tMat = xf2.R;
  59.          var v2X:Number = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  60.          var v2Y:Number = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  61.          v2X -= v1X;
  62.          v2Y -= v1Y;
  63.          return v2X * normal1WorldX + v2Y * normal1WorldY;
  64.       }
  65.       
  66.       public static function b2TestOverlap(a:b2AABB, b:b2AABB) : Boolean
  67.       {
  68.          var t1:b2Vec2 = b.lowerBound;
  69.          var t2:b2Vec2 = a.upperBound;
  70.          var d1X:Number = t1.x - t2.x;
  71.          var d1Y:Number = t1.y - t2.y;
  72.          t1 = a.lowerBound;
  73.          t2 = b.upperBound;
  74.          var d2X:Number = t1.x - t2.x;
  75.          var d2Y:Number = t1.y - t2.y;
  76.          if(d1X > 0 || d1Y > 0)
  77.          {
  78.             return false;
  79.          }
  80.          if(d2X > 0 || d2Y > 0)
  81.          {
  82.             return false;
  83.          }
  84.          return true;
  85.       }
  86.       
  87.       public static function FindIncidentEdge(c:Array, poly1:b2PolygonShape, xf1:b2XForm, edge1:int, poly2:b2PolygonShape, xf2:b2XForm) : void
  88.       {
  89.          var tMat:b2Mat22 = null;
  90.          var tVec:b2Vec2 = null;
  91.          var tClip:ClipVertex = null;
  92.          var dot:Number = NaN;
  93.          var count1:int = poly1.m_vertexCount;
  94.          var normals1:Array = poly1.m_normals;
  95.          var count2:int = poly2.m_vertexCount;
  96.          var vertices2:Array = poly2.m_vertices;
  97.          var normals2:Array = poly2.m_normals;
  98.          tMat = xf1.R;
  99.          tVec = normals1[edge1];
  100.          var normal1X:Number = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  101.          var normal1Y:Number = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  102.          tMat = xf2.R;
  103.          var tX:Number = tMat.col1.x * normal1X + tMat.col1.y * normal1Y;
  104.          normal1Y = tMat.col2.x * normal1X + tMat.col2.y * normal1Y;
  105.          normal1X = tX;
  106.          var index:int = 0;
  107.          var minDot:Number = Number.MAX_VALUE;
  108.          for(var i:int = 0; i < count2; i++)
  109.          {
  110.             tVec = normals2[i];
  111.             dot = normal1X * tVec.x + normal1Y * tVec.y;
  112.             if(dot < minDot)
  113.             {
  114.                minDot = dot;
  115.                index = i;
  116.             }
  117.          }
  118.          var i1:int = index;
  119.          var i2:int = i1 + 1 < count2 ? i1 + 1 : 0;
  120.          tClip = c[0];
  121.          tVec = vertices2[i1];
  122.          tMat = xf2.R;
  123.          tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  124.          tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  125.          tClip.id.features.referenceEdge = edge1;
  126.          tClip.id.features.incidentEdge = i1;
  127.          tClip.id.features.incidentVertex = 0;
  128.          tClip = c[1];
  129.          tVec = vertices2[i2];
  130.          tMat = xf2.R;
  131.          tClip.v.x = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  132.          tClip.v.y = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  133.          tClip.id.features.referenceEdge = edge1;
  134.          tClip.id.features.incidentEdge = i2;
  135.          tClip.id.features.incidentVertex = 1;
  136.       }
  137.       
  138.       public static function b2CollidePolygons(manifold:b2Manifold, polyA:b2PolygonShape, xfA:b2XForm, polyB:b2PolygonShape, xfB:b2XForm) : void
  139.       {
  140.          var cv:ClipVertex = null;
  141.          var poly1:b2PolygonShape = null;
  142.          var poly2:b2PolygonShape = null;
  143.          var edge1:int = 0;
  144.          var flip:uint = 0;
  145.          var np:int = 0;
  146.          var v12:b2Vec2 = null;
  147.          var separation:Number = NaN;
  148.          var cp:b2ManifoldPoint = null;
  149.          manifold.pointCount = 0;
  150.          var edgeA:int = 0;
  151.          var edgeAO:Array = [edgeA];
  152.          var separationA:Number = FindMaxSeparation(edgeAO,polyA,xfA,polyB,xfB);
  153.          edgeA = int(edgeAO[0]);
  154.          if(separationA > 0)
  155.          {
  156.             return;
  157.          }
  158.          var edgeB:int = 0;
  159.          var edgeBO:Array = [edgeB];
  160.          var separationB:Number = FindMaxSeparation(edgeBO,polyB,xfB,polyA,xfA);
  161.          edgeB = int(edgeBO[0]);
  162.          if(separationB > 0)
  163.          {
  164.             return;
  165.          }
  166.          var xf1:b2XForm = new b2XForm();
  167.          var xf2:b2XForm = new b2XForm();
  168.          var k_relativeTol:Number = 0.98;
  169.          var k_absoluteTol:Number = 0.001;
  170.          if(separationB > k_relativeTol * separationA + k_absoluteTol)
  171.          {
  172.             poly1 = polyB;
  173.             poly2 = polyA;
  174.             xf1.Set(xfB);
  175.             xf2.Set(xfA);
  176.             edge1 = edgeB;
  177.             flip = 1;
  178.          }
  179.          else
  180.          {
  181.             poly1 = polyA;
  182.             poly2 = polyB;
  183.             xf1.Set(xfA);
  184.             xf2.Set(xfB);
  185.             edge1 = edgeA;
  186.             flip = 0;
  187.          }
  188.          var incidentEdge:Array = [new ClipVertex(),new ClipVertex()];
  189.          FindIncidentEdge(incidentEdge,poly1,xf1,edge1,poly2,xf2);
  190.          var count1:int = poly1.m_vertexCount;
  191.          var vertices1:Array = poly1.m_vertices;
  192.          var tVec:b2Vec2 = vertices1[edge1];
  193.          var v11:b2Vec2 = tVec.Copy();
  194.          if(edge1 + 1 < count1)
  195.          {
  196.             tVec = vertices1[int(edge1 + 1)];
  197.             v12 = tVec.Copy();
  198.          }
  199.          else
  200.          {
  201.             tVec = vertices1[0];
  202.             v12 = tVec.Copy();
  203.          }
  204.          var dv:b2Vec2 = b2Math.SubtractVV(v12,v11);
  205.          var sideNormal:b2Vec2 = b2Math.b2MulMV(xf1.R,b2Math.SubtractVV(v12,v11));
  206.          sideNormal.Normalize();
  207.          var frontNormal:b2Vec2 = b2Math.b2CrossVF(sideNormal,1);
  208.          v11 = b2Math.b2MulX(xf1,v11);
  209.          v12 = b2Math.b2MulX(xf1,v12);
  210.          var frontOffset:Number = b2Math.b2Dot(frontNormal,v11);
  211.          var sideOffset1:Number = -b2Math.b2Dot(sideNormal,v11);
  212.          var sideOffset2:Number = b2Math.b2Dot(sideNormal,v12);
  213.          var clipPoints1:Array = [new ClipVertex(),new ClipVertex()];
  214.          var clipPoints2:Array = [new ClipVertex(),new ClipVertex()];
  215.          np = ClipSegmentToLine(clipPoints1,incidentEdge,sideNormal.Negative(),sideOffset1);
  216.          if(np < 2)
  217.          {
  218.             return;
  219.          }
  220.          np = ClipSegmentToLine(clipPoints2,clipPoints1,sideNormal,sideOffset2);
  221.          if(np < 2)
  222.          {
  223.             return;
  224.          }
  225.          manifold.normal = !!flip ? frontNormal.Negative() : frontNormal.Copy();
  226.          var pointCount:int = 0;
  227.          for(var i:int = 0; i < b2Settings.b2_maxManifoldPoints; i++)
  228.          {
  229.             cv = clipPoints2[i];
  230.             separation = b2Math.b2Dot(frontNormal,cv.v) - frontOffset;
  231.             if(separation <= 0)
  232.             {
  233.                cp = manifold.points[pointCount];
  234.                cp.separation = separation;
  235.                cp.localPoint1 = b2Math.b2MulXT(xfA,cv.v);
  236.                cp.localPoint2 = b2Math.b2MulXT(xfB,cv.v);
  237.                cp.id.key = cv.id._key;
  238.                cp.id.features.flip = flip;
  239.                pointCount++;
  240.             }
  241.          }
  242.          manifold.pointCount = pointCount;
  243.       }
  244.       
  245.       public static function FindMaxSeparation(edgeIndex:Array, poly1:b2PolygonShape, xf1:b2XForm, poly2:b2PolygonShape, xf2:b2XForm) : Number
  246.       {
  247.          var tVec:b2Vec2 = null;
  248.          var tMat:b2Mat22 = null;
  249.          var bestEdge:int = 0;
  250.          var bestSeparation:Number = NaN;
  251.          var increment:int = 0;
  252.          var dot:Number = NaN;
  253.          var count1:int = poly1.m_vertexCount;
  254.          var normals1:Array = poly1.m_normals;
  255.          tMat = xf2.R;
  256.          tVec = poly2.m_centroid;
  257.          var dX:Number = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  258.          var dY:Number = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  259.          tMat = xf1.R;
  260.          tVec = poly1.m_centroid;
  261.          dX -= xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  262.          dY -= xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  263.          var dLocal1X:Number = dX * xf1.R.col1.x + dY * xf1.R.col1.y;
  264.          var dLocal1Y:Number = dX * xf1.R.col2.x + dY * xf1.R.col2.y;
  265.          var edge:int = 0;
  266.          var maxDot:Number = -Number.MAX_VALUE;
  267.          for(var i:int = 0; i < count1; i++)
  268.          {
  269.             tVec = normals1[i];
  270.             dot = tVec.x * dLocal1X + tVec.y * dLocal1Y;
  271.             if(dot > maxDot)
  272.             {
  273.                maxDot = dot;
  274.                edge = i;
  275.             }
  276.          }
  277.          var s:Number = EdgeSeparation(poly1,xf1,edge,poly2,xf2);
  278.          if(s > 0)
  279.          {
  280.             return s;
  281.          }
  282.          var prevEdge:int = edge - 1 >= 0 ? edge - 1 : count1 - 1;
  283.          var sPrev:Number = EdgeSeparation(poly1,xf1,prevEdge,poly2,xf2);
  284.          if(sPrev > 0)
  285.          {
  286.             return sPrev;
  287.          }
  288.          var nextEdge:int = edge + 1 < count1 ? edge + 1 : 0;
  289.          var sNext:Number = EdgeSeparation(poly1,xf1,nextEdge,poly2,xf2);
  290.          if(sNext > 0)
  291.          {
  292.             return sNext;
  293.          }
  294.          if(sPrev > s && sPrev > sNext)
  295.          {
  296.             increment = -1;
  297.             bestEdge = prevEdge;
  298.             bestSeparation = sPrev;
  299.          }
  300.          else
  301.          {
  302.             if(sNext <= s)
  303.             {
  304.                edgeIndex[0] = edge;
  305.                return s;
  306.             }
  307.             increment = 1;
  308.             bestEdge = nextEdge;
  309.             bestSeparation = sNext;
  310.          }
  311.          while(true)
  312.          {
  313.             if(increment == -1)
  314.             {
  315.                edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
  316.             }
  317.             else
  318.             {
  319.                edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;
  320.             }
  321.             s = EdgeSeparation(poly1,xf1,edge,poly2,xf2);
  322.             if(s > 0)
  323.             {
  324.                break;
  325.             }
  326.             if(s <= bestSeparation)
  327.             {
  328.                edgeIndex[0] = bestEdge;
  329.                return bestSeparation;
  330.             }
  331.             bestEdge = edge;
  332.             bestSeparation = s;
  333.          }
  334.          return s;
  335.       }
  336.       
  337.       public static function ClipSegmentToLine(vOut:Array, vIn:Array, normal:b2Vec2, offset:Number) : int
  338.       {
  339.          var cv:ClipVertex = null;
  340.          var numOut:int = 0;
  341.          var vIn0:b2Vec2 = null;
  342.          var vIn1:b2Vec2 = null;
  343.          var distance0:Number = NaN;
  344.          var interp:Number = NaN;
  345.          var tVec:b2Vec2 = null;
  346.          var cv2:ClipVertex = null;
  347.          numOut = 0;
  348.          cv = vIn[0];
  349.          vIn0 = cv.v;
  350.          cv = vIn[1];
  351.          vIn1 = cv.v;
  352.          distance0 = b2Math.b2Dot(normal,vIn0) - offset;
  353.          var distance1:Number = b2Math.b2Dot(normal,vIn1) - offset;
  354.          if(distance0 <= 0)
  355.          {
  356.             var _loc14_:*;
  357.             vOut[_loc14_ = numOut++] = vIn[0];
  358.          }
  359.          if(distance1 <= 0)
  360.          {
  361.             vOut[_loc14_ = numOut++] = vIn[1];
  362.          }
  363.          if(distance0 * distance1 < 0)
  364.          {
  365.             interp = distance0 / (distance0 - distance1);
  366.             cv = vOut[numOut];
  367.             tVec = cv.v;
  368.             tVec.x = vIn0.x + interp * (vIn1.x - vIn0.x);
  369.             tVec.y = vIn0.y + interp * (vIn1.y - vIn0.y);
  370.             cv = vOut[numOut];
  371.             if(distance0 > 0)
  372.             {
  373.                cv2 = vIn[0];
  374.                cv.id = cv2.id;
  375.             }
  376.             else
  377.             {
  378.                cv2 = vIn[1];
  379.                cv.id = cv2.id;
  380.             }
  381.             numOut++;
  382.          }
  383.          return numOut;
  384.       }
  385.       
  386.       public static function b2CollideCircles(manifold:b2Manifold, circle1:b2CircleShape, xf1:b2XForm, circle2:b2CircleShape, xf2:b2XForm) : void
  387.       {
  388.          var tMat:b2Mat22 = null;
  389.          var tVec:b2Vec2 = null;
  390.          var separation:Number = NaN;
  391.          var dist:Number = NaN;
  392.          var a:Number = NaN;
  393.          manifold.pointCount = 0;
  394.          tMat = xf1.R;
  395.          tVec = circle1.m_localPosition;
  396.          var p1X:Number = xf1.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  397.          var p1Y:Number = xf1.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  398.          tMat = xf2.R;
  399.          tVec = circle2.m_localPosition;
  400.          var p2X:Number = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  401.          var p2Y:Number = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  402.          var dX:Number = p2X - p1X;
  403.          var dY:Number = p2Y - p1Y;
  404.          var distSqr:Number = dX * dX + dY * dY;
  405.          var r1:Number = circle1.m_radius;
  406.          var r2:Number = circle2.m_radius;
  407.          var radiusSum:Number = r1 + r2;
  408.          if(distSqr > radiusSum * radiusSum)
  409.          {
  410.             return;
  411.          }
  412.          if(distSqr < Number.MIN_VALUE)
  413.          {
  414.             separation = -radiusSum;
  415.             manifold.normal.Set(0,1);
  416.          }
  417.          else
  418.          {
  419.             dist = Math.sqrt(distSqr);
  420.             separation = dist - radiusSum;
  421.             a = 1 / dist;
  422.             manifold.normal.x = a * dX;
  423.             manifold.normal.y = a * dY;
  424.          }
  425.          manifold.pointCount = 1;
  426.          var tPoint:b2ManifoldPoint = manifold.points[0];
  427.          tPoint.id.key = 0;
  428.          tPoint.separation = separation;
  429.          p1X += r1 * manifold.normal.x;
  430.          p1Y += r1 * manifold.normal.y;
  431.          p2X -= r2 * manifold.normal.x;
  432.          p2Y -= r2 * manifold.normal.y;
  433.          var pX:Number = 0.5 * (p1X + p2X);
  434.          var pY:Number = 0.5 * (p1Y + p2Y);
  435.          var tX:Number = pX - xf1.position.x;
  436.          var tY:Number = pY - xf1.position.y;
  437.          tPoint.localPoint1.x = tX * xf1.R.col1.x + tY * xf1.R.col1.y;
  438.          tPoint.localPoint1.y = tX * xf1.R.col2.x + tY * xf1.R.col2.y;
  439.          tX = pX - xf2.position.x;
  440.          tY = pY - xf2.position.y;
  441.          tPoint.localPoint2.x = tX * xf2.R.col1.x + tY * xf2.R.col1.y;
  442.          tPoint.localPoint2.y = tX * xf2.R.col2.x + tY * xf2.R.col2.y;
  443.       }
  444.       
  445.       public static function b2CollidePolygonAndCircle(manifold:b2Manifold, polygon:b2PolygonShape, xf1:b2XForm, circle:b2CircleShape, xf2:b2XForm) : void
  446.       {
  447.          var tPoint:b2ManifoldPoint = null;
  448.          var dX:Number = NaN;
  449.          var dY:Number = NaN;
  450.          var positionX:Number = NaN;
  451.          var positionY:Number = NaN;
  452.          var tVec:b2Vec2 = null;
  453.          var tMat:b2Mat22 = null;
  454.          var dist:Number = NaN;
  455.          var pX:Number = NaN;
  456.          var pY:Number = NaN;
  457.          var s:Number = NaN;
  458.          manifold.pointCount = 0;
  459.          tMat = xf2.R;
  460.          tVec = circle.m_localPosition;
  461.          var cX:Number = xf2.position.x + (tMat.col1.x * tVec.x + tMat.col2.x * tVec.y);
  462.          var cY:Number = xf2.position.y + (tMat.col1.y * tVec.x + tMat.col2.y * tVec.y);
  463.          dX = cX - xf1.position.x;
  464.          dY = cY - xf1.position.y;
  465.          tMat = xf1.R;
  466.          var cLocalX:Number = dX * tMat.col1.x + dY * tMat.col1.y;
  467.          var cLocalY:Number = dX * tMat.col2.x + dY * tMat.col2.y;
  468.          var normalIndex:int = 0;
  469.          var separation:Number = -Number.MAX_VALUE;
  470.          var radius:Number = circle.m_radius;
  471.          var vertexCount:int = polygon.m_vertexCount;
  472.          var vertices:Array = polygon.m_vertices;
  473.          var normals:Array = polygon.m_normals;
  474.          for(var i:int = 0; i < vertexCount; i++)
  475.          {
  476.             tVec = vertices[i];
  477.             dX = cLocalX - tVec.x;
  478.             dY = cLocalY - tVec.y;
  479.             tVec = normals[i];
  480.             s = tVec.x * dX + tVec.y * dY;
  481.             if(s > radius)
  482.             {
  483.                return;
  484.             }
  485.             if(s > separation)
  486.             {
  487.                separation = s;
  488.                normalIndex = i;
  489.             }
  490.          }
  491.          if(separation < Number.MIN_VALUE)
  492.          {
  493.             manifold.pointCount = 1;
  494.             tVec = normals[normalIndex];
  495.             tMat = xf1.R;
  496.             manifold.normal.x = tMat.col1.x * tVec.x + tMat.col2.x * tVec.y;
  497.             manifold.normal.y = tMat.col1.y * tVec.x + tMat.col2.y * tVec.y;
  498.             tPoint = manifold.points[0];
  499.             tPoint.id.features.incidentEdge = normalIndex;
  500.             tPoint.id.features.incidentVertex = b2_nullFeature;
  501.             tPoint.id.features.referenceEdge = 0;
  502.             tPoint.id.features.flip = 0;
  503.             positionX = cX - radius * manifold.normal.x;
  504.             positionY = cY - radius * manifold.normal.y;
  505.             dX = positionX - xf1.position.x;
  506.             dY = positionY - xf1.position.y;
  507.             tMat = xf1.R;
  508.             tPoint.localPoint1.x = dX * tMat.col1.x + dY * tMat.col1.y;
  509.             tPoint.localPoint1.y = dX * tMat.col2.x + dY * tMat.col2.y;
  510.             dX = positionX - xf2.position.x;
  511.             dY = positionY - xf2.position.y;
  512.             tMat = xf2.R;
  513.             tPoint.localPoint2.x = dX * tMat.col1.x + dY * tMat.col1.y;
  514.             tPoint.localPoint2.y = dX * tMat.col2.x + dY * tMat.col2.y;
  515.             tPoint.separation = separation - radius;
  516.             return;
  517.          }
  518.          var vertIndex1:int = normalIndex;
  519.          var vertIndex2:int = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0;
  520.          tVec = vertices[vertIndex1];
  521.          var tVec2:b2Vec2 = vertices[vertIndex2];
  522.          var eX:Number = tVec2.x - tVec.x;
  523.          var eY:Number = tVec2.y - tVec.y;
  524.          var length:Number = Math.sqrt(eX * eX + eY * eY);
  525.          eX /= length;
  526.          eY /= length;
  527.          dX = cLocalX - tVec.x;
  528.          dY = cLocalY - tVec.y;
  529.          var u:Number = dX * eX + dY * eY;
  530.          tPoint = manifold.points[0];
  531.          if(u <= 0)
  532.          {
  533.             pX = tVec.x;
  534.             pY = tVec.y;
  535.             tPoint.id.features.incidentEdge = b2_nullFeature;
  536.             tPoint.id.features.incidentVertex = vertIndex1;
  537.          }
  538.          else if(u >= length)
  539.          {
  540.             pX = tVec2.x;
  541.             pY = tVec2.y;
  542.             tPoint.id.features.incidentEdge = b2_nullFeature;
  543.             tPoint.id.features.incidentVertex = vertIndex2;
  544.          }
  545.          else
  546.          {
  547.             pX = eX * u + tVec.x;
  548.             pY = eY * u + tVec.y;
  549.             tPoint.id.features.incidentEdge = normalIndex;
  550.             tPoint.id.features.incidentVertex = 0;
  551.          }
  552.          dX = cLocalX - pX;
  553.          dY = cLocalY - pY;
  554.          dist = Math.sqrt(dX * dX + dY * dY);
  555.          dX /= dist;
  556.          dY /= dist;
  557.          if(dist > radius)
  558.          {
  559.             return;
  560.          }
  561.          manifold.pointCount = 1;
  562.          tMat = xf1.R;
  563.          manifold.normal.x = tMat.col1.x * dX + tMat.col2.x * dY;
  564.          manifold.normal.y = tMat.col1.y * dX + tMat.col2.y * dY;
  565.          positionX = cX - radius * manifold.normal.x;
  566.          positionY = cY - radius * manifold.normal.y;
  567.          dX = positionX - xf1.position.x;
  568.          dY = positionY - xf1.position.y;
  569.          tMat = xf1.R;
  570.          tPoint.localPoint1.x = dX * tMat.col1.x + dY * tMat.col1.y;
  571.          tPoint.localPoint1.y = dX * tMat.col2.x + dY * tMat.col2.y;
  572.          dX = positionX - xf2.position.x;
  573.          dY = positionY - xf2.position.y;
  574.          tMat = xf2.R;
  575.          tPoint.localPoint2.x = dX * tMat.col1.x + dY * tMat.col1.y;
  576.          tPoint.localPoint2.y = dX * tMat.col2.x + dY * tMat.col2.y;
  577.          tPoint.separation = dist - radius;
  578.          tPoint.id.features.referenceEdge = 0;
  579.          tPoint.id.features.flip = 0;
  580.       }
  581.    }
  582. }
  583.