Advertisement
Guest User

C# Collision Detection Problem

a guest
Mar 31st, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. protected void CollisionHandle(List<List<WorldTile>> TileMap)
  2.         {
  3.             int leftTile = (int)Math.Floor((float)_Collbox.Left / WorldTile.Width);
  4.             int rightTile = (int)Math.Ceiling(((float)_Collbox.Right / WorldTile.Width)) - 1;
  5.             int topTile = (int)Math.Floor((float)_Collbox.Top / WorldTile.Height);
  6.             int bottomTile = (int)Math.Ceiling(((float)_Collbox.Bottom / WorldTile.Height)) - 1;
  7.  
  8.             _OnGround = false;
  9.  
  10.             for (int y = topTile; y <= bottomTile; y++)
  11.             {
  12.                 for (int x = leftTile; x <= rightTile; x++)
  13.                 {
  14.                     TileCollision collision = TileCollision.Empty;
  15.                     if (y >= 0)
  16.                     {
  17.                         if (x >= 0)
  18.                         {
  19.                             if (y < TileMap.Count && x < TileMap[y].Count)
  20.                                 collision = TileMap[y][x].Collision;
  21.                         }
  22.                     }
  23.  
  24.                     if (collision != TileCollision.Empty)
  25.                     {
  26.                         _TileBounds = new Rectangle(x * WorldTile.Width, y * WorldTile.Height, WorldTile.Width, WorldTile.Height);
  27.                         Vector2 depth = IntersectDepth(_Collbox, _TileBounds);
  28.                         if (depth != Vector2.Zero)
  29.                         {
  30.  
  31.                             float absDepthX = Math.Abs(depth.X);
  32.                             float absDepthY = Math.Abs(depth.Y);
  33.  
  34.                             if (absDepthY <= absDepthX || collision == TileCollision.OneWay)
  35.                             {
  36.                                 if (_PreviousBottom <= _TileBounds.Top)
  37.                                     _OnGround = true;
  38.  
  39.                                 if ((collision == TileCollision.Solid) || _OnGround)
  40.                                 {
  41.                                     Position = new Vector2((int)Math.Round(Position.X), (int)Math.Round(Position.Y + depth.Y));
  42.                                     UpdateCollBox();
  43.                                 }
  44.                             }
  45.                             else if (collision == TileCollision.Solid)
  46.                             {
  47.                                 Position = new Vector2((int)Math.Round(Position.X + depth.X), (int)Math.Round(Position.Y));
  48.                                 _FacingRight = !_FacingRight;
  49.                                 _Velocity.Y = 0;
  50.                                 UpdateCollBox();
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.  
  57.             _PreviousBottom = _Collbox.Bottom;
  58.         }
  59.  
  60.         private void UpdateCollBox()
  61.         {
  62.             _Collbox = new Rectangle((int)Math.Round(Position.X), (int)Math.Round(Position.Y), Animation.FrameWidth, Animation.FrameHeight);
  63.         }
  64.  
  65.         private Vector2 IntersectDepth(Rectangle rectangleA, Rectangle rectangleB)
  66.         {
  67.             float halfWidthA = rectangleA.Width / 2.0f;
  68.             float halfHeightA = rectangleA.Height / 2.0f;
  69.             float halfWidthB = rectangleB.Width / 2.0f;
  70.             float halfHeightB = rectangleB.Height / 2.0f;
  71.  
  72.             Vector2 centerA = new Vector2(rectangleA.Left + halfWidthA, rectangleA.Top + halfHeightA);
  73.             Vector2 centerB = new Vector2(rectangleB.Left + halfWidthB, rectangleB.Top + halfHeightB);
  74.  
  75.             float distanceX = centerA.X - centerB.X;
  76.             float distanceY = centerA.Y - centerB.Y;
  77.             float minDistanceX = halfWidthA + halfWidthB;
  78.             float minDistanceY = halfHeightA + halfHeightB;
  79.  
  80.             // If no intersection is happening, return Vector2.Zero
  81.             if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
  82.                 return Vector2.Zero;
  83.  
  84.             // Calculate instersection depth
  85.             float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
  86.             float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
  87.             return new Vector2(depthX, depthY);
  88.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement