title: Circle and Rectangle collision
body: <p>I have a program in Processing for a bouncing ball and a rectangle. I can get the collision for the sides of the rectangle correct, but I have no idea how to get the corners. This is what I have so far:</p>
  
   
  
  <pre class="lang-java prettyprint-override"><code>int radius = 20;
  float circleX, circleY;    //positions
  float vx = 3, vy = 3;  //velocities    float boxW, boxH;  //bat dimensions
  
  void setup(){
    size(400,400);
    ellipseMode(RADIUS);
    rectMode(RADIUS);
    circleX = width/4;
    circleY = height/4;
    boxW = 50;
    boxH = 20;
  }
  
  void draw(){
    background(0);
  
    circleX += vx;
    circleY += vy;
  
    //bouncing off sides
    if (circleX + radius &gt; width || circleX &lt; radius){ vx *= -1; }   //left and right
    if (circleY + radius &gt; height || circleY &lt; radius){ vy *= -1; }  //top and bottom
    if (circleY + radius &gt; height){ 
      circleY = (height-radius)-(circleY-(height-radius)); }  //bottom correction
  
    //bouncing off bat
    if (circleY + radius &gt; mouseY - boxH &amp;&amp; circleX &gt; mouseX - boxW &amp;&amp; circleX &lt; mouseX + boxW){ 
        vy *= -1; //top
    }
    if (circleX - radius &lt; mouseX + boxW &amp;&amp; circleY &gt; mouseY - boxH &amp;&amp; circleY &lt; mouseY + boxH){ 
        vx *= -1; //right
    }
    if (circleY - radius &gt; mouseY + boxH &amp;&amp; circleX &gt; mouseX - boxW &amp;&amp; circleX &lt; mouseX + boxW){ 
        vy *= -1; //bottom
    }
    if (circleX + radius &lt; mouseX - boxW &amp;&amp; circleY &gt; mouseY - boxH &amp;&amp; circleY &lt; mouseY + boxH){ 
        vx *= -1; //left
    }
  
    if ([CORNER DETECTION???]){
      vx *= -1;
      vy *= -1;
    }
  
    ellipse(circleX,circleY,radius,radius);
  
    rect(mouseX,mouseY,boxW,boxH);
  }
  </code></pre>
  
  <p>I don't know what to put in the if statement to detect the corner collisions.</p>