title: How to fix circle and rectangle overlap in collision response?
body: <p>Since in the digital world a real collision almost never happens, we will always have a situation where the "colliding" circle overlaps the rectangle. </p>
  
  <p><strong>How to put back the circle in the situation where it collides perfectly with the rectangle without overlap?</strong></p>
  
  <p>Suppose that the rectangle is stopped (null velocity) and axis-aligned.</p>
  
  <p>I would solve this problem with <a href="http://en.wikipedia.org/wiki/Collision_detection#A_posteriori_.28discrete.29_versus_a_priori_.28continuous.29" rel="nofollow noreferrer">a posteriori</a> approach (in two dimensions).</p>
  
  <p><strong>In short I have to solve this equation for t</strong>:</p>
  
  <p><img src="https://i.stack.imgur.com/dth5U.gif" alt="enter image description here"></p>
  
  <p><strong>Where:</strong></p>
  
  <ul>
  <li><p><img src="https://i.stack.imgur.com/M7oJO.gif" alt="t"> is a number that answers to the question: how many frames ago did the
  collision happen perfectly?</p></li>
  <li><p><img src="https://i.stack.imgur.com/FNNoy.gif" alt="r"> is the radius of the circle.</p></li>
  <li><p><img src="https://i.stack.imgur.com/SvI3t.gif" alt="(x, y)"> is the center of the circle</p></li>
  <li><p><img src="https://i.stack.imgur.com/sHleQ.gif" alt="(v.x, v.y)"> is its velocity.</p></li>
  <li><p><img src="https://i.stack.imgur.com/pUXxt.gif" alt="A(t)"> and <img src="https://i.stack.imgur.com/bsUYQ.gif" alt="B(t)"> are functions that return the x and y coordinates of
  the point where the circle and the rectangle collide (when the circle is
  at <img src="https://i.stack.imgur.com/6yUh5.gif" alt="(x - t * v.x, y - t * v.y)"> position, that is in the position in which perfectly collide with the rectangle).</p></li>
  </ul>
  
  <p>Recently I solved a <a href="https://stackoverflow.com/questions/18683179/how-to-fix-circles-overlap-in-collision-response">similar problem</a> for collisions between circles, but now I don't know the law of the functions A and B.</p>