Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2011
1,656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1.    public void Update()
  2.         {
  3.             IsCollidingOnTop = false;
  4.             IsCollidingOnBottom = false;
  5.             IsCollidingOnLeft = false;
  6.             IsCollidingOnRight = false;
  7.  
  8.             List<object[]> invokations = new List<object[]>();
  9.  
  10.             if (IsAffectedByGravity) Velocity += World.Gravity;
  11.             SSSPVector2 nextPosition = Position + Velocity;
  12.  
  13.             List<SSSPBody> bodiesToCheck =
  14.                 World.QuadTree.Query(new Rect(Position.X, Position.Y, Width + (Width * 3), Height + (Height * 3)));
  15.  
  16.             for (int i = 0; i < bodiesToCheck.Count; i++)
  17.             {
  18.                 SSSPBody body = bodiesToCheck[i];
  19.  
  20.                 if (body == this || IgnoreCollisionDetectBodies.Contains(body) ||
  21.                     body.CollisionGroups.Any(IgnoreCollisionDetectGroups.Contains)) continue;
  22.  
  23.                 int nextH = 0, nextV = 0;
  24.                 bool overlap = false;
  25.  
  26.                 if (true)
  27.                 {
  28.                     if (SSSPUtils.IsOverlapping(this, body, nextPosition.X - Position.X))
  29.                     {
  30.                         overlap = true;
  31.  
  32.                         int left = (body.CornerMin.X - (nextPosition.X + Width / 2));
  33.                         int right = (body.CornerMax.X - (nextPosition.X - Width / 2));
  34.                         nextH = Math.Abs(left) < right ? left : right;
  35.  
  36.                         if (!(body.CollisionGroups.Any(IgnoreCollisionResolveGroups.Contains)) &&
  37.                             InvokeOnResolve(body, new SSSPVector2(nextH, 0)))
  38.                         {
  39.                             nextPosition.X += nextH;
  40.  
  41.                             if (Math.Abs(left) < right)
  42.                             {
  43.                                 if (Velocity.X > 0) Velocity = new SSSPVector2(0, Velocity.Y);
  44.                                 IsCollidingOnLeft = true;
  45.                             }
  46.                             else
  47.                             {
  48.                                 if (Velocity.X < 0) Velocity = new SSSPVector2(0, Velocity.Y);
  49.                                 IsCollidingOnRight = true;
  50.                             }
  51.                         }
  52.                     }
  53.  
  54.                     if (SSSPUtils.IsOverlapping(this, body, 0, nextPosition.Y - Position.Y))
  55.                     {
  56.                         overlap = true;
  57.  
  58.                         int top = (body.CornerMin.Y - (nextPosition.Y + Height / 2));
  59.                         int bottom = (body.CornerMax.Y - (nextPosition.Y - Height / 2));
  60.                         nextV = Math.Abs(top) < bottom ? top : bottom;
  61.  
  62.                         if (!(body.CollisionGroups.Any(IgnoreCollisionResolveGroups.Contains)) &&
  63.                             InvokeOnResolve(body, new SSSPVector2(0, nextV)))
  64.                         {
  65.                             nextPosition.Y += nextV;
  66.  
  67.                             if (Math.Abs(top) < bottom)
  68.                             {
  69.                                 if (Velocity.Y > 0) Velocity = new SSSPVector2(Velocity.X, 0);
  70.                                 IsCollidingOnTop = true;
  71.                             }
  72.                             else
  73.                             {
  74.                                 if (Velocity.Y < 0) Velocity = new SSSPVector2(Velocity.X, 0);
  75.                                 IsCollidingOnBottom = true;
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.  
  81.                 if (overlap) invokations.Add(new object[] { body, nextH, nextV });
  82.             }
  83.  
  84.  
  85.             foreach (object[] objects in invokations)
  86.             {
  87.                 InvokeOnOverlap((SSSPBody)objects[0], (int)objects[1], (int)objects[2]);
  88.                 SSSPBody b = (SSSPBody)objects[0];
  89.                 b.InvokeOnOverlap(this, (int)objects[1] * -1, (int)objects[2] * -1);
  90.             }
  91.  
  92.             Position = nextPosition;
  93.             InvokeBoundsChanged(new EventArgs());
  94.         }
  95.  
  96.  
  97.  
  98.  
  99. ---
  100.  
  101.  
  102.  
  103.  public static bool IsOverlapping(SSSPBody mBody1, SSSPBody mBody2, int mOffsetX = 0, int mOffsetY = 0)
  104.         {
  105.             return mBody1.CornerMax.X + mOffsetX > mBody2.CornerMin.X &&
  106.                    mBody1.CornerMin.X + mOffsetX < mBody2.CornerMax.X &&
  107.                    (mBody1.CornerMax.Y + mOffsetY > mBody2.CornerMin.Y &&
  108.                     mBody1.CornerMin.Y + mOffsetY < mBody2.CornerMax.Y);
  109.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement