Advertisement
Guest User

Untitled

a guest
Jun 21st, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. public static bool AABBIsOverlapping(Vector2 mCornerMin1, Vector2 mCornerMax1, Vector2 mCornerMin2,
  2.                                              Vector2 mCornerMax2)
  3.         {
  4.             if (mCornerMax1.X <= mCornerMin2.X || mCornerMin1.X >= mCornerMax2.X)
  5.                 return false;
  6.             if (mCornerMax1.Y <= mCornerMin2.Y || mCornerMin1.Y >= mCornerMax2.Y)
  7.                 return false;
  8.             return true;
  9.         }
  10.  
  11.   public void Update()
  12.         {
  13.             Velocity += World.Gravity;
  14.             Vector2 next = Velocity;
  15.             bool xCollided = false;
  16.             bool yCollided = false;
  17.             List<SSSPBody> toCheck = World.SpatialHash.GetNearbyItems(this);
  18.             for (int i = 0; i < toCheck.Count; i++)
  19.             {
  20.                 SSSPBody body = toCheck[i];
  21.  
  22.                 if (body != this)
  23.                 {
  24.                     Vector2 nextCornerMin = CornerMin + new Vector2(next.X, 0);
  25.                     Vector2 nextCornerMax = CornerMax + new Vector2(next.X, 0);
  26.  
  27.                     if (SSSPUtils.AABBIsOverlapping(nextCornerMin, nextCornerMax, body.CornerMin, body.CornerMax))
  28.                     {
  29.                         xCollided = true;
  30.                         float left = (body.CornerMin.X - nextCornerMax.X);
  31.                         float right = (body.CornerMax.X - nextCornerMin.X);
  32.                         if (Math.Abs(left) < right)
  33.                             Position = new Vector2(Position.X + next.X + left, Position.Y);
  34.                         else
  35.                             Position = new Vector2(Position.X + next.X + right, Position.Y);
  36.                         next = new Vector2(0, next.Y);
  37.                         Velocity = new Vector2(0, next.Y);
  38.                     }
  39.  
  40.                     nextCornerMin = CornerMin + new Vector2(0, next.Y);
  41.                     nextCornerMax = CornerMax + new Vector2(0, next.Y);
  42.  
  43.                     if (SSSPUtils.AABBIsOverlapping(nextCornerMin, nextCornerMax, body.CornerMin, body.CornerMax))
  44.                     {
  45.                         yCollided = true;
  46.                         float top = (body.CornerMin.Y - nextCornerMax.Y);
  47.                         float bottom = (body.CornerMax.Y - nextCornerMin.Y);
  48.                         if (Math.Abs(top) < bottom)
  49.                         {
  50.                             Position = new Vector2(Position.X, Position.Y + next.Y + top);
  51.                             Velocity = new Vector2(next.X, 0);
  52.                         }
  53.                         else
  54.                         {
  55.                             Position = new Vector2(Position.X, Position.Y + next.Y + bottom);
  56.                             Velocity = new Vector2(next.X, 0.01f);
  57.                         }
  58.                         next = new Vector2(next.X, 0);
  59.                        
  60.                     }
  61.                 }
  62.             }
  63.             if (xCollided == false)
  64.                 Position = new Vector2(Position.X + next.X, Position.Y);
  65.             if (yCollided == false)
  66.                 Position = new Vector2(Position.X, Position.Y + next.Y);
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement