Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8973909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:33:19+00:00 2026-06-15T18:33:19+00:00

I’ve been making a top-down shooter game in XNA that requires rectangular collision for

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:33:20+00:00Added an answer on June 15, 2026 at 6:33 pm

    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:

    • Find the set of largest contiguous rectangles to cover multiple areas

    • Puzzle: Find largest rectangle (maximal rectangle problem)

    Papers linked in those questions:

    • Fast Algorithms To Partition Simple Rectilinear Polygons

    • Polygon Decomposition

    • The Maximal Rectangle Problem

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I've tracked down a weird MySQL problem to the two different ways I was
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.