1
        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?

3
  • are the rectangles moving along an axis? are they spinning or do they stay parralel to one another? Commented Oct 21, 2013 at 7:57
  • They are moving "freely" along X,Y axis and can collide from any direction with each other. No spinning or stuff like that. Commented Oct 21, 2013 at 7:59
  • 1
    I added a possible soluution below, I think rectangle.intersect might be a good way forward. Good Luck! Commented Oct 21, 2013 at 8:14

3 Answers 3

1

First you must compare first to other, by example

for(int first=0; first<listRect.Count;first++)
{
    for(int second=first+1;second<listRect.Count;second++)
    {
    }
}

Next step you must check position rectangles

if(listRect[first].r.X<listRect[second].r.X)
{
   if(listRect[first].r.Right > listRect[second].r.Left)
   {
       listRect[first].speedX = ran.Next(-6,-4);
       listRect[second].speedX = ran.Next(4, 6);
   }
}
else
{
    if(listRect[first].r.Left < listRect[second].r.Right)
    {
       listRect[first].speedX = ran.Next(4, 6);
       listRect[second].speedX = ran.Next(-6,-4);
    }
}

And similarly for top and down.

Sign up to request clarification or add additional context in comments.

6 Comments

List<myRectangle> listRect = new List<myRectangle>(); This is how i iniaztilze my list I am not sure its compatible with your solution.
Oh thanks! Do i define the centerX property in my rectangle class?
At which property of rectangle you add speedX?
Oh.. no.. How you define current position of Rectangle?
Uh i don't but it would be using the rectangle.r.X and rectangle.r.Y as defiend in the c# rectangle code.
|
0

I think you should not compare the Left of an element to the Right of another one. Consider this code:

                if (rect.r.Left < rect2.r.Left) 
                    rect.speedX = ran.Next(-6,-4);
                    rect2.speedX = ran.Next(4, 6);

                if (rect.r.Left > rect2.r.Left)
                    rect.speedX = ran.Next(4, 6);
                    rect2.speedX = ran.Next(-6, -4);

                if (rect.r.Top < rect2.r.Top)
                    rect.speedY = ran.Next(4, 6);
                    rect2.speedY = ran.Next(-6, -4);

                if (rect.r.Top > rect2.r.Top)
                    rect.speedY = ran.Next(-6, -4);
                    rect2.speedY = ran.Next(4, 6);

You just need to know which rectangle is on which side of each rectangle. Also I would suggest you to do this:

                    rect.speedX = -1 * rect.speedX;
                    rect2.speedY = -1 * rect.speedY;

Checking which rectangle is where relative to the other one doesn't show you the collision side.

Comments

0

How about using rectangle.intersect? I think this has the functionality you want and saves you some coding..

You can use it this way:

if (Rect.r.IntersectsWith(rect2.r))
                {
                    if (rect.r.Left < rect2.r.Left) 
                        rect.speedX = ran.Next(-6,-4);
                        rect2.speedX = ran.Next(4, 6);

                    if (rect.r.Left > rect2.r.Left)
                        rect.speedX = ran.Next(4, 6);
                        rect2.speedX = ran.Next(-6, -4);

                    if (rect.r.Top < rect2.r.Top)
                        rect.speedY = ran.Next(4, 6);
                        rect2.speedY = ran.Next(-6, -4);

                    if (rect.r.Top > rect2.r.Top)
                        rect.speedY = ran.Next(-6, -4);
                        rect2.speedY = ran.Next(4, 6);
                }

You can find the documentation on msdn.

EDIT: you also just need to compare left to left and top to top (or right to right and bottom to bottom etc...) to change your speed, I edited the code snippet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.