title: Unexpected behaviour from Physics and Collision Detection
body: <p>I have worked on this problem for months and my current solution still does not work. I am working on simple bounding box collision between a square pawn and a vector of square tiles.</p>
  
  <p>In this current method the player will collide correctly on the bottom, left and right but not on top. I do not understand why. </p>
  
  <p>This is the physics function, which deals with motion:</p>
  
  <pre><code>void loop(Pawn *pawn) {
  
      int currentFPS = fps-&gt;getFPS();
  
      //friction
      if (pawn-&gt;velX &gt; 0.0) {
          pawn-&gt;velX *= 0.91f;
      }
      if (pawn-&gt;velY &gt; 0) {
          pawn-&gt;velY *= 0.91f;
      }
      if (pawn-&gt;velX &lt; 0) {
          pawn-&gt;velX *= 0.91f;
      }
      if (pawn-&gt;velY &lt; 0) {
          pawn-&gt;velY *= 0.91f;
      }
  
      //gravity
      pawn-&gt;velY += gravity;
  
      //motion
      pawn-&gt;posY += pawn-&gt;velY; //move along the horizontal axis
      pawn-&gt;posX += pawn-&gt;velX; //move along the vertical axis
  
      //handle collision
      for (int i = 0; i &lt; vectorSize; i++) {
          collide(pawn, getLevelCollision[i]);
      }
  
  }
  </code></pre>
  
  <p>This is the collision function, which checks for collision and then corrects the pawn position based on which edge is intersecting the colliding rectangle:</p>
  
  <pre><code>void collide(Pawn *pawn, SDL_Rect rect2) {
  
      bool collide = false;
  
      //create positions of rect1
      float rect1Top = pawn-&gt;posY;
      float rect1Bottom = rect1Top + pawn-&gt;position.h;
      float rect1Left = pawn-&gt;posX;
      float rect1Right = rect1Left + pawn-&gt;position.w;
  
      //create position of rect2
      float rect2Top = rect2.y;
      float rect2Bottom = rect2.y + rect2.h;
      float rect2Left = rect2.x;
      float rect2Right = rect2.x + rect2.w;
  
  
  
      //if the bottom edge of rect1 intersects rect2 vertically
      if (rect1Bottom &gt; rect2Top &amp;&amp; rect1Bottom &lt; rect2Bottom) {
          //if rect1 and rect2 are on top of each other
          if (rect1Right &gt; rect2Left &amp;&amp; rect1Right &lt; rect2Right || rect1Left &lt; rect2Right &amp;&amp; rect1Left &gt; rect2Left) {
              pawn-&gt;posY = rect2Top - pawn-&gt;position.h;
  
              rect1Top = pawn-&gt;posY;
              rect1Top = pawn-&gt;position.h;
          }
      }
  
      //if the left edge of rect1 intersects rect2 horizontally
      else if (rect1Left &lt; rect2Right &amp;&amp; rect1Left &gt; rect2Left) {
          //if rect1 and rect2 are on top of each other
          if (rect1Top &lt; rect2Bottom &amp;&amp; rect1Top &gt; rect2Top || rect1Bottom &gt; rect2Top &amp;&amp; rect1Bottom &lt; rect2Bottom) {
              pawn-&gt;posX = rect2Right;
  
              rect1Left = pawn-&gt;posX;
              rect1Right = rect1Left + pawn-&gt;position.w;
          }
      }
  
      //if the right edge of rect1 intersects rect2 horizontally
      else if (rect1Right &gt; rect2Left &amp;&amp; rect1Right &lt; rect2Right) {
          //if rect1 and rect2 are on top of each other
          if (rect1Top &lt; rect2Bottom &amp;&amp; rect1Top &gt; rect2Top || rect1Bottom &gt; rect2Top &amp;&amp; rect1Bottom &lt; rect2Bottom) {
              pawn-&gt;posX = rect2Left - pawn-&gt;position.w;
  
              rect1Left = pawn-&gt;posX;
              rect1Right = rect1Left + pawn-&gt;position.w;
          }
      }
  
      //if the top edge of rect1 intersects rect2 vertically
      if (rect1Top &lt; rect2Bottom &amp;&amp; rect1Top &gt; rect2Top) {
          //if rect1 and rect2 are on top of each other
          if (rect1Right &gt; rect2Left &amp;&amp; rect1Right &lt; rect2Right || rect1Left &lt; rect2Right &amp;&amp; rect1Left &gt; rect2Left) {
              pawn-&gt;posY = rect2Bottom;
  
              rect1Top = pawn-&gt;posY;
              rect1Top = rect1Top + pawn-&gt;position.h;
          }
      }
  }
  </code></pre>
  
  <p>I cannot understand why the top edge of the pawn does not collide as every possible iteration of if / else if has been tried as well as rearranging the order of the if statements.</p>
  
  <p>I am desperate for insight on this problem as well as a new set of eyes to see what I am clearly not able to.</p>