Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 10th, 2011  |  syntax: C#  |  size: 2.27 KB  |  views: 72  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public void Update()
  2.         {
  3.             IsCollidingOnTop = false;
  4.             IsCollidingOnBottom = false;
  5.             IsCollidingOnLeft = false;
  6.             IsCollidingOnRight = false;
  7.  
  8.             if (IsAffectedByGravity) Velocity += World.Gravity;
  9.             Position += Velocity;
  10.  
  11.             int left = CornerMin.X;
  12.             int right = CornerMax.X;
  13.             int top = CornerMin.Y;
  14.             int bottom = CornerMax.Y;
  15.  
  16.             List<SSSPBody> bodiesToCheck = World.Bodies;
  17.  
  18.             for (int i = 0; i < bodiesToCheck.Count; i++)
  19.             {
  20.                 SSSPBody body = bodiesToCheck[i];
  21.  
  22.                 if (body == this || IgnoreCollisionDetectBodies.Contains(body) ||
  23.                     body.CollisionGroups.Any(IgnoreCollisionDetectGroups.Contains)) continue;
  24.  
  25.                 int bodyLeft = body.CornerMin.X;
  26.                 int bodyRight = body.CornerMax.X;
  27.                 int bodyTop = body.CornerMin.Y;
  28.                 int bodyBottom = body.CornerMax.Y;
  29.  
  30.                 if(SSSPUtils.IsOverlapping(this, body))
  31.                 {
  32.                     int encrX = 0;
  33.                     int encrY = 0;
  34.  
  35.                     if (bottom < bodyBottom && bottom >= bodyTop) encrY = bodyTop - bottom;
  36.                     else if (top > bodyTop && top <= bodyBottom) encrY = bodyBottom - top;
  37.  
  38.                     if (left < bodyLeft && right >= bodyLeft) encrX = bodyLeft - right;
  39.                     else if (right > bodyRight && left <= bodyRight) encrX = bodyRight - left;
  40.  
  41.                     int numPxOverlapX = 0;
  42.                     int numPxOverlapY = 0;
  43.  
  44.                     if (left < bodyLeft) numPxOverlapX = right - bodyLeft;
  45.                     else numPxOverlapX = bodyRight - left;
  46.  
  47.                     if (top < bodyTop) numPxOverlapY = bottom - bodyTop;
  48.                     else numPxOverlapY = bodyBottom - top;
  49.  
  50.                     if(numPxOverlapX > numPxOverlapY)
  51.                     {
  52.                         Position += new SSSPVector2(0, encrY);
  53.                         if (encrY <= 0) IsCollidingOnTop = true;
  54.                     }
  55.                     else
  56.                     {
  57.                         Position += new SSSPVector2(encrX, 0);
  58.                     }
  59.                 }
  60.             }
  61.         }
clone this paste RAW Paste Data