title: Ball to Rectangle collision
body: <p>Hello I am making a game in java that requires collision from a ball into a rectangle.</p>
  <p>pos.x = x value of ball</p>
  <p>pos.y = y value of ball</p>
  <p>vel.x = x velocity of ball</p>
  <p>vel.y = y velocity of ball</p>
  <p>sh = height of ball</p>
  <p>sw = width of ball</p>
  <p>W = width of rect</p>
  <p>H = height of rect</p>
  <p>So far I have :</p>
  <pre><code>  boolean insideRect = pos.x &gt;= X &amp;&amp; pos.x &lt;= X+W &amp;&amp; pos.y &gt;= Y &amp;&amp; pos.y &lt;= Y+H;
    
    if(insideRect){
    
      if(pos.y + sh/2 &gt;= H){
         vel.y = (-1*vel.y)*gravity;
         vel.x *= .6;
         if(pos.y &lt; Y+H/2){
           pos.y = Y;
         } else{
           pos.y = Y+H;
         }
      }
      if(pos.x + sw/2 &gt; W){
         vel.x = -1*vel.x;
      }       
    }
  </code></pre>
  <p>However this just says if it hit the left side of the rect or right, and if it hit the top or bottom.</p>
  <p>So an output if you printed under the if statements would be : (left, up), (right, down), (left, down), (right, up)</p>
  <p>So the problem is that for this to work I can only have one, either left, right, up, or down.</p>
  <p>If I have two, then it thinks that it both hit the ceiling and the right wall, and has two visual outputs do to it.</p>
  <p>How can I work around this?</p>