Advertisement
Guest User

Untitled

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