title: How to detect the side on which collision occured
body: <p>This is my first post so I'm trying to make my problem as clear as possible. I'm making a game and I want to improve my collision detection. This is because I want to check what side is being hit and stop the player from moving past it without using something general like <em>if(collision(player, enemy)) player.x = enemy.x - player.w(width)</em> because if the player were to collide with the top it wouldn't keep the player on top.</p>
  
  <p>In the code it checks if any one of the statements is true and then returns it but it doesn't tell me which statement was the one that was equal to true so I can stop the player from moving accordingly, if that makes sense. If you have a more efficient collision detection for me to use it would be greatly appreciated. </p>
  
  <p>I've already tried to make a position variable to be equal to whatever side gets collided into and then stop the player from moving past it but it only works for the left side and won't let my player jump over the enemy or block.</p>
  
  <pre><code>function collision(object1, object2) {
  
         return !(
              object1.x &gt; object2.x + object2.w  ||
              object1.x + object1.w &lt; object2.x  ||
              object1.y &gt; object2.y + object2.h  ||
              object1.y + object1.h &lt; object2.y 
          )
  }
  
  //Only works for the left side
  
  if(collision(player, enemy)) player.x = enemy.x - player.w
  </code></pre>
  
  <p>I expect it to be able to tell me what side is being collided into and then either stop the player from moving past/into it and for the player to be able to be on top of the block/enemy without just being pushed to the left.</p>