title: Different method for collision detection using faces of a rectangle
body: <p>I have been working on and off of a certain game I'm making (trying to get it finished!) and a problem that has been unsolved for a while in it is the collision detection between a ball and a square.</p>
  
  <p>Basically what I want to happen eventually is depending on the angle/way the rectangle is facing, I want the ball to bounce of it accordingly (I know I could just inverse the ball direction before/as it hit the square).</p>
  
  <p>At the moment though, my current problem is trying to inverse the correct X and Y components depending on the side/face that the ball collides with the square, e.g. if the ball hits the right side of the square, then I need to inverse the ball's X component.</p>
  
  <p>This doesn't seem to work and I was wondering if I could somehow label each side of the rectangle, in terms of for the top of it label that 'face 1' or something, then for the right side of it 'face 2' or 'side 2', etc...</p>
  
  <p>I have provided some code below (this is the code I'm using now):</p>
  
  <pre><code>//(collision with right side of square)
  if (theBall.GetRectangle.Left &lt;= thePaddle.GetRectangle.Right)
  {
      theBall.pVelocity.X = -theBall.pVelocity.X;
  }
  
  //(collision with bottom of square)
  if (theBall.GetRectangle.Top &lt;= thePaddle.GetRectangle.Bottom)
  {
      theBall.pVelocity.Y = -theBall.pVelocity.Y;
  }
  </code></pre>
  
  <p>I have written the code for the other 2 sides of the rectangle but they are just the opposite of the two above, i.e. for collision with top of rectangle = opposite of bottom, etc.</p>
  
  <p>EDIT: the object I am checking against if the ball has collided with DOES NOT move, I mean it only rotates... so I don't know if this is important (it probably is, therefore I apologise for missing this info out at the start).</p>
  
  <p>EDIT @ 23:36: ok, I have tried something.... and it hasn't worked... :(</p>
  
  <pre><code>public Vector2 DistBetweenBallAndBlock(Paddle thePaddle, Ball theBall)
      {
          Vector2 centreOfBall = new Vector2(theBall.Texture.Width / 2, theBall.Texture.Height / 2);
  
          float distX = thePaddle.Position.X - centreOfBall.X;
          float distY = thePaddle.Position.Y - centreOfBall.Y;
  
          if (distX &lt; 0)
          {
              distX = -distX;
          }
  
          if (distY &lt; 0)
          {
              distY = -distY;
          }
  
  
          return new Vector2(distX, distY);
      }
  </code></pre>
  
  <p>I have then tried to just print the result just to get an idea of what's going on and what sort of values are being output:</p>
  
  <pre><code>Vector2 a = ball.DistBetweenBallAndBlock(paddle1, ball);            
          angleOfPaddle = Math.Atan2(a.Y, a.X);
  </code></pre>
  
  <p>I then just print this value to screen, however I am getting the same result of 0.63...</p>