title: 2D Monogame: Rectangle collision detection, side specific
body: <p>I have two rectangles, one player, one map. The player needs to not be able to walk through the map. Both the player and map have a rectangle with position and texture width and height, both also have a Vector position. <code>Rectangle.Intersect()</code> only outputs a boolean value, I cannot figure out how I might find out which side was collided with. I found this function <a href="https://gamedev.stackexchange.com/questions/45838/get-collision-details-from-rectangle-intersects">here</a> which outputs a Vector representing how much the rectangles overlapped.</p>
  
  <pre><code>public static Vector2 GetIntersectionDepth(this Rectangle rectA,                 Rectangle rectB)
      {
          // Calculate half sizes.
          float halfWidthA = rectA.Width / 2.0f;
          float halfHeightA = rectA.Height / 2.0f;
          float halfWidthB = rectB.Width / 2.0f;
          float halfHeightB = rectB.Height / 2.0f;
  
          // Calculate centers.
          Vector2 centerA = new Vector2(rectA.Left + halfWidthA, rectA.Top + halfHeightA);
          Vector2 centerB = new Vector2(rectB.Left + halfWidthB, rectB.Top + halfHeightB);
  
          // Calculate current and minimum-non-intersecting distances between centers.
          float distanceX = centerA.X - centerB.X;
          float distanceY = centerA.Y - centerB.Y;
          float minDistanceX = halfWidthA + halfWidthB;
          float minDistanceY = halfHeightA + halfHeightB;
  
          // If we are not intersecting at all, return (0, 0).
          if (Math.Abs(distanceX) &gt;= minDistanceX || Math.Abs(distanceY) &gt;= minDistanceY)
              return Vector2.Zero;
  
          // Calculate and return intersection depths.
          float depthX = distanceX &gt; 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
          float depthY = distanceY &gt; 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
          return new Vector2(depthX, depthY);
      }
  </code></pre>
  
  <p>This function will give negative numbers based on side, however I cannot figure out how to use them effectively. I tried:</p>
  
  <pre><code>Vector2 overlap =   RectangleExtensions.GetIntersectionDepth(map.Dungeon[x,y].BoundingBox, player.BoundingBox);
  if (overlap.X &gt; 0) //This should be collision on the left
  {
      //Move the player back
  }
  </code></pre>
  
  <p>However this causes some strange bugs, especially when attempting the same for the Y player and map values.</p>
  
  <p>The question: How can collision detection be done in monogame with rectangles that would let you know which side was collided with, using this function or otherwise.</p>
  
  <p>Thanks for any help!</p>