Advertisement
Guest User

Untitled

a guest
Aug 5th, 2011
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 KB | None | 0 0
  1. public void Update()
  2.         {
  3.             bool crushedLeft = false, crushedRight = false, crushedTop = false, crushedBottom = false; // Boolean values to check if the body is "crushed"
  4.             IsCollidingOnBottom = false; // Boolean value to check if the body is on the ground
  5.  
  6.             List<object[]> invokations = new List<object[]>(); // List of "detection events" invokations
  7.  
  8.             PositionPrevious = Position; // Sets the previous position to the current position
  9.             if (IsAffectedByGravity) Velocity += World.Gravity; // Adds gravity to velocity
  10.             Position += Velocity; // Adds velocity to position
  11.  
  12.             List<SSSPBody> bodiesToCheck = World.Grid.GetBodiesIn(new Rectangle(Left, Top, Width, Height)); // Gets nearby bodies from spatial hash
  13.  
  14.             if (Velocity.Y < 0) bodiesToCheck.Sort((b, a) => a.Position.Y.CompareTo(b.Position.Y)); // Sorts bodies by Y axis to avoid wall-jumping bugs
  15.             else bodiesToCheck.Sort((a, b) => a.Position.Y.CompareTo(b.Position.Y));
  16.  
  17.             for (int i = 0; i < bodiesToCheck.Count; i++) // Detection loop
  18.             {
  19.                 SSSPBody body = bodiesToCheck[i];
  20.  
  21.                 if (IgnoresDetectionWith(body)) continue; // If the body has to be ignored, continue
  22.                 if (!SSSPUtils.IsOverlapping(this, body, new SSSPVector2(0, 0))) continue; // If there isn't any overlap, continue
  23.  
  24.                 int encrX = 0, encrY = 0; // Penetration values (X and Y)
  25.  
  26.                 if (Bottom < body.Bottom && Bottom >= body.Top) encrY = body.Top - Bottom; // Calculates Y penetration
  27.                 else if (Top > body.Top && Top <= body.Bottom) encrY = body.Bottom - Top;
  28.  
  29.                 if (Left < body.Left && Right >= body.Left) encrX = body.Left - Right; // Calculates X penetration
  30.                 else if (Right > body.Right && Left <= body.Right) encrX = body.Right - Left;
  31.  
  32.                 invokations.Add(new object[] { body, encrX, encrY }); // Adds "detection event" invokation
  33.             }
  34.  
  35.             for (int i = 0; i < bodiesToCheck.Count; i++) // Resolution loop
  36.             {
  37.                 SSSPBody body = bodiesToCheck[i];
  38.  
  39.                 if (IgnoresDetectionWith(body)) continue; // If the body has to be ignored, continue
  40.                 if (!SSSPUtils.IsOverlapping(this, body, new SSSPVector2(0, 0))) continue; // If there isn't any overlap, continue
  41.  
  42.                 int encrX = 0, encrY = 0, numPxOverlapX, numPxOverlapY; // Penetration values and overlap values
  43.  
  44.                 if (Bottom < body.Bottom && Bottom >= body.Top) encrY = body.Top - Bottom; // Calculates Y penetration
  45.                 else if (Top > body.Top && Top <= body.Bottom) encrY = body.Bottom - Top;
  46.  
  47.                 if (Left < body.Left && Right >= body.Left) encrX = body.Left - Right; // Calculates X penetration
  48.                 else if (Right > body.Right && Left <= body.Right) encrX = body.Right - Left;
  49.  
  50.                 if (Left < body.Left) numPxOverlapX = Right - body.Left; // Calculates X overlap
  51.                 else numPxOverlapX = body.Right - Left;
  52.  
  53.                 if (Top < body.Top) numPxOverlapY = Bottom - body.Top; // Calculates Y overlap
  54.                 else numPxOverlapY = body.Bottom - Top;
  55.  
  56.                 if (IgnoresResolutionWith(body, new SSSPVector2(encrX, encrY))) continue; // If resolution has to be ignored, continue
  57.  
  58.                 if (Left >= body.Left && Top >= body.Top) crushedLeft = true; // Checks for crushing
  59.                 if (Right <= body.Right && Top >= body.Top) crushedRight = true;
  60.                 if (Left >= body.Left && Bottom <= body.Bottom) crushedTop = true;
  61.                 if (Right <= body.Right && Bottom <= body.Bottom) crushedBottom = true;
  62.  
  63.                 if (numPxOverlapX > numPxOverlapY)
  64.                 {
  65.                     Position += new SSSPVector2(0, encrY); // Resolves penetration
  66.                     body.Velocity = new SSSPVector2(body.Velocity.X, Velocity.Y); // Transfers velocity
  67.                 }
  68.                 else
  69.                 {
  70.                     Position += new SSSPVector2(encrX, 0);
  71.                     body.Velocity = new SSSPVector2(Velocity.X, body.Velocity.Y);
  72.                 }
  73.  
  74.                 if (SSSPUtils.IsOverlapping(this, body, new SSSPVector2(PositionPrevious.X - Position.X, Math.Abs(Velocity.Y) * Math.Sign(World.Gravity.Y))))
  75.                     IsCollidingOnBottom = true; // Checks if the body is on the ground
  76.  
  77.                 SSSPVector2 checkPosition = PositionPrevious - (body.PositionPrevious - body.Position); // Calculates difference vector for moving platforms
  78.                 if (encrX > 0 && Velocity.X < 0 && Position.X == checkPosition.X) Velocity = new SSSPVector2(0, Velocity.Y); // Sets velocity to 0 if the body is on a moving platform
  79.                 if (encrX < 0 && Velocity.X > 0 && Position.X == checkPosition.X) Velocity = new SSSPVector2(0, Velocity.Y);
  80.                 if (encrY < 0 && Velocity.Y > 0 && Position.Y == checkPosition.Y) Velocity = new SSSPVector2(Velocity.X, 0);
  81.                 if (encrY > 0 && Velocity.Y < 0 && Position.Y == checkPosition.Y) Velocity = new SSSPVector2(Velocity.X, 0);
  82.             }
  83.  
  84.             if (Position.X == PositionPrevious.X) Velocity = new SSSPVector2(0, Velocity.Y); // If the body hasn't moved, set velocity to 0
  85.             if (Position.Y == PositionPrevious.Y) Velocity = new SSSPVector2(Velocity.X, 0);
  86.  
  87.             foreach (object[] objects in invokations)
  88.             {
  89.                 InvokeOnOverlap((SSSPBody)objects[0], new SSSPVector2((int)objects[1], (int)objects[2])); // Calls overlap events in the bodies
  90.                 SSSPBody b = (SSSPBody)objects[0];
  91.                 b.InvokeOnOverlap(this, new SSSPVector2((int)objects[1] * -1, (int)objects[2] * -1));
  92.             }
  93.  
  94.             if (crushedLeft && crushedRight && crushedTop && crushedBottom)
  95.                 InvokeOnCrush(this, new SSSPVector2(0, 0)); // Calls crush event in the body
  96.  
  97.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement