Search code examples
c#xna-4.0rectangles

Automatically Merging Ajacent Rectangles


I've been making a top-down shooter game in XNA that requires rectangular collision for the map.

The collision walls for a map is stored in a text file in the format of:rect[0,0,1024,8]

The values correspond to defining a rectangle (x, y, width, height).

I've been thinking that I could write a separate application that can illiterate through the data of the map image, find out the pixels that are black (or any color of the wall) and make rectangles there. Basically, this program will generate the rectangles required for the collision. Ideally, it would be pixel perfect, which would require something like a thousand rectangles each 1 pixel wide that covers all the walls.

Is there a possible way to detect which of these rectangles (or squares I should say) are adjacent to one another, then connect them into the a bigger (but still covering the same area) rectangle?

EG. Lets say I have a wall that is 10 by 2. The program would generate 20 different rectangles, each 1 pixel high. How would I efficiently detect that these rectangles are adjacent and automatically make a 10 by 2 rectangle covering the whole wall instead of having 20 different little pixel rectangles?

EDIT: I've worked out a solution that fits my purposes, for future reference, my code is below:

//map is a bitmap, horizontalCollisions and collisions are List<Rectangle>s
for (int y = 0; y < map.Height; y++) //loop through pixels
        {
            for (int x = 0; x < map.Width; x++)
            {
                if (map.GetPixel(x, y).Name == "ff000000") //wall color
                {
                    int i = 1;
                    while (map.GetPixel(x + i, y).Name == "ff000000")
                    {
                        if (i != map.Width - x)
                        {
                            i++;
                        }
                        if (i == map.Width - x)
                        {
                            break;
                        }
                    }
                    Rectangle r = new Rectangle(x, y, i, 1);//create and add
                    x += i - 1;
                    horizontalCollisions.Add(r);
                }
            }
        }
        for (int j = 0; j < horizontalCollisions.Count; j++)
        {
            int i = 1;
            Rectangle current = horizontalCollisions[j];
            Rectangle r = new Rectangle(current.X, current.Y + 1, current.Width, 1);
            while(horizontalCollisions.Contains(r))
            {
                i++;
                horizontalCollisions.Remove(r);
                r = new Rectangle(current.X, current.Y + i, current.Width, 1);
            }
            Rectangle add = new Rectangle(current.X, current.Y, current.Width, i);
            collisions.Add(add);
        }

            //collisions now has all the rectangles

Basically, it will loop through the pixel data horizontally. When it encounters a wall pixel, it will stop the counter and (using a while loop) move the counter towards the right, one by one until it hits a non-wall pixel. Then, it will create a rectangle of that width, and continue on. After this process, there will be a big list of rectangles, each 1px tall. Basically, a bunch of horizontal lines. The next loop will run through the horizontal lines, and using the same process as above, it will find out of there are any rectangles with the same X value and the same Width value under it (y+1). This will keep incrementing until there are none, in which one big rectangle will be created, and the used rectangles are deleted from the List. The final resulting list contains all the rectangles that will make up all the black pixels on the image (pretty efficiently, I think).


Solution

  • Etiquette may suggest that I should comment this instead of add it as an answer, but I do not yet have that capability, so bear with me.

    I'm afraid I am not able to translate this into code for you, but I can send you towards some academic papers that discuss algorithms that can do some of the things that you're asking.

    Other time this questions has appeared:

    Papers linked in those questions:

    Hopefully these questions and papers can lead help you find the answer you're looking for, or at least scare you off towards finding another solution.