SHARE
TWEET

Untitled

a guest Oct 19th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static ShapeCollision TestCircleVsCircle
  2.             (Circle circleA, Circle circleB, ShapeCollision into = null, bool flip = false)
  3.         {
  4.             Circle circle1 = flip ? circleB : circleA;
  5.             Circle circle2 = flip ? circleA : circleB;
  6.  
  7.             //add both radii together to get the colliding distance
  8.             float totalRadius = circle1.TransformedRadius + circle2.TransformedRadius;
  9.             //find the distance between teh two circles
  10.             float distanceSquared = (circle1.Position- circle2.Position).LengthSquared();
  11.  
  12.             if(distanceSquared < totalRadius * totalRadius)
  13.             {
  14.                 if (into == null)
  15.                 {
  16.                     into = new ShapeCollision();
  17.                 }
  18.                 else
  19.                 {
  20.                     into.Reset();
  21.                 }
  22.  
  23.                 float difference = totalRadius - (float)Math.Sqrt(distanceSquared);
  24.  
  25.                 into.Tested = circle1;
  26.                 into.TestedAgainst = circle2;
  27.  
  28.                 Vector2 unitVector = circle1.Position - circle2.Position;
  29.                 unitVector.Normalize();
  30.  
  31.                 into.UnitVectorX = unitVector.X;
  32.                 into.UnitVectorY = unitVector.Y;
  33.  
  34.                 into.SeperationX = unitVector.X * difference;
  35.                 into.SeperationY = unitVector.Y * difference;
  36.  
  37.                 return into;
  38.             }
  39.  
  40.             return null;
  41.         }
RAW Paste Data
Top