title: c# Form: Check where two rectangles collided
body: <pre><code>        foreach (var rect in listRect)
          {
              foreach (var rect2 in listRect)
              {
                  if (Rectangle.Intersect(rect.r,rect2.r) != Rectangle.Empty &amp;&amp; rect != rect2)
                  {
                      if (rect.r.Left &lt; rect2.r.Right) 
                          rect.speedX = ran.Next(-6,-4);
                          rect2.speedX = ran.Next(4, 6);
  
                      if (rect.r.Right &gt; rect2.r.Left)
                          rect.speedX = ran.Next(4, 6);
                          rect2.speedX = ran.Next(-6, -4);
  
                      if (rect.r.Top &lt; rect2.r.Bottom)
                          rect.speedY = ran.Next(4, 6);
                          rect2.speedY = ran.Next(-6, -4);
  
                      if (rect.r.Bottom &gt; rect2.r.Top)
                          rect.speedY = ran.Next(-6, -4);
                          rect2.speedY = ran.Next(4, 6);
  </code></pre>
  
  <p>This is my current code for a function that is supposed to check if two rectangles have collided. Then if a collision is detected it is supposed to reverese the speed of the rectangles so they move in opposite directions. </p>
  
  <p>My code as you probably can imagine does not work and i can't really figure out how I am supposed to solve this. </p>
  
  <p>The rectangles are saved in a list called listRect and the class for the rectangles look like this:</p>
  
  <pre><code>class myRectangle
  {
      public Rectangle r = new Rectangle();
      public int speedX = 5;
      public int speedY = 5;
  }
  </code></pre>
  
  <p>How do i make simple collision detection so i know where they collided and how can i use that in my if-else statements?</p>