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 11th, 2011  |  syntax: C#  |  size: 2.49 KB  |  views: 63  |  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.QuadTree.Query(new Rect(left, top, Width*2, Height*2));
  17.             bodiesToCheck.Sort((a, b) => a.Position.Y.CompareTo(b.Position.Y));
  18.  
  19.             for (int i = 0; i < bodiesToCheck.Count; i++)
  20.             {
  21.                 SSSPBody body = bodiesToCheck[i];
  22.  
  23.                 if (body == this || IgnoreCollisionDetectBodies.Contains(body) ||
  24.                     body.CollisionGroups.Any(IgnoreCollisionDetectGroups.Contains)) continue;
  25.  
  26.                 int bodyLeft = body.CornerMin.X;
  27.                 int bodyRight = body.CornerMax.X;
  28.                 int bodyTop = body.CornerMin.Y;
  29.                 int bodyBottom = body.CornerMax.Y;
  30.  
  31.                 if (!SSSPUtils.IsOverlapping(this, body)) continue;
  32.  
  33.                 int encrX = 0, encrY = 0, numPxOverlapX, numPxOverlapY;
  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.                 if (left < bodyLeft) numPxOverlapX = right - bodyLeft;
  42.                 else numPxOverlapX = bodyRight - left;
  43.  
  44.                 if (top < bodyTop) numPxOverlapY = bottom - bodyTop;
  45.                 else numPxOverlapY = bodyBottom - top;
  46.  
  47.                 invokations.Add(new object[] {body, encrX, encrY});
  48.  
  49.                 if ((body.CollisionGroups.Any(IgnoreCollisionResolveGroups.Contains)) &&
  50.                     InvokeOnResolve(body, new SSSPVector2(encrX, encrY))) continue;
  51.  
  52.                 if (numPxOverlapX > numPxOverlapY)
  53.                 {
  54.                     Position += new SSSPVector2(0, encrY);
  55.  
  56.                     if (encrY <= 0) IsCollidingOnTop = true;
  57.                     if (encrY != 0) Velocity = new SSSPVector2(Velocity.X, 0);
  58.                 }
  59.                 else Position += new SSSPVector2(encrX, 0);
  60.             }
  61.         }