Advertisement
Guest User

Untitled

a guest
Jul 10th, 2011
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  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.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement