foreach (var rect in listRect)
{
foreach (var rect2 in listRect)
{
if (Rectangle.Intersect(rect.r,rect2.r) != Rectangle.Empty && rect != rect2)
{
if (rect.r.Left < rect2.r.Right)
rect.speedX = ran.Next(-6,-4);
rect2.speedX = ran.Next(4, 6);
if (rect.r.Right > rect2.r.Left)
rect.speedX = ran.Next(4, 6);
rect2.speedX = ran.Next(-6, -4);
if (rect.r.Top < rect2.r.Bottom)
rect.speedY = ran.Next(4, 6);
rect2.speedY = ran.Next(-6, -4);
if (rect.r.Bottom > rect2.r.Top)
rect.speedY = ran.Next(-6, -4);
rect2.speedY = ran.Next(4, 6);
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.
My code as you probably can imagine does not work and i can't really figure out how I am supposed to solve this.
The rectangles are saved in a list called listRect and the class for the rectangles look like this:
class myRectangle
{
public Rectangle r = new Rectangle();
public int speedX = 5;
public int speedY = 5;
}
How do i make simple collision detection so i know where they collided and how can i use that in my if-else statements?