Hi,

This is kind of a general question around a specific problem. I have a level format which is effectively a 2D grid with various squares filled in to represent the structure of the level and some supporting data. I use the level format to generate both collision objects (boxes) and the display objects as well. What I'd like to be able to do is merge contiguous boxes into a single box shape reduce the number of collision objects generated.

My first implentation was very simple , it scans the level data row by row and every filled in square in the grid became an individual collision box. This was then improved slightly so that contiguous horizontal objects were merged into a single box.

What I was wondering was if there were any recommended approaches for applying this to testing both directions, to produce the smallest possible number of individual boxes that can represent the same data (i'd like to keep the collision shapes as boxes rather then merging them into more complex concave shapes)?

Many thanks,

**1**

# Merging adjacent boxes to produce smallest set?

Started by xexuxjy, Nov 15 2012 04:59 AM

4 replies to this topic

Sponsor:

###
#2
Crossbones+ - Reputation: **2865**

Posted 15 November 2012 - 09:11 AM

Not sure, but when i once thought about this problem i decided that the following is probably good:

1.For each cell,

2.Expand it on both axes simultaneously as much as possible (1 on y, 1 on x, repeat) until it cant get bigger

3.goto 1 (but skip cells which are already "conquered")

1.For each cell,

2.Expand it on both axes simultaneously as much as possible (1 on y, 1 on x, repeat) until it cant get bigger

3.goto 1 (but skip cells which are already "conquered")

o3o

###
#3
Members - Reputation: **711**

Posted 15 November 2012 - 02:09 PM

Take a look at quadtrees. As your level format is already a 2D grid, this would be ideal for quickly determining the largest possible box you can make out of the smaller boxes. The idea is similar to Waterlimon's.

Starting from the root node, you would drill deeper into the tree until a node has no empty children or you've met a maximum depth. If all 4 children of a node contain data (boxes) then it's safe to represent that as a single block.

Here's a visual, interactive example - http://donar.umiacs.umd.edu/quadtree/regions/regionquad.html

Starting from the root node, you would drill deeper into the tree until a node has no empty children or you've met a maximum depth. If all 4 children of a node contain data (boxes) then it's safe to represent that as a single block.

Here's a visual, interactive example - http://donar.umiacs.umd.edu/quadtree/regions/regionquad.html

**Edited by caldiar, 15 November 2012 - 02:10 PM.**

###
#5
Members - Reputation: **729**

Posted 15 November 2012 - 02:47 PM

Thanks for the ideas Waterlimon and Caldiar, that makes sense. As the levels aren't particularly big (200x200 at the mo) then the simple 'expand' in each direction and mark-off the processed nodes should work well. Bah i'm torn now as i'm also splitting the whole level into segments so that I can activate the ones near the player which the quad tree should also model nicely. can see me ending up trying both.

Thanks again.

Ok, finally finished a 'working' version , what I ended up doing was similar to the original idea above.

Pick a starting square.

scan along x to find the max possible range. (maxX)

scan along y to find the maximum possible range. (maxY)

brute force test each possible rectangles from (maxX , maxY) to (1,1) to see which had the largest valid area (where all squares are of the correct type and are unprocessed).

mark all the squares under that rectangle as processed.

repeat for next valid square through the grid.

It ended up being a little more complicated then I'd originally planned but it works nicely with L-shaped section and other oddities.

I can probably make a few optimisations on it now it's stable but it processes quickly for my largest levels so am not too worried.

Thanks again for the help.

Thanks again.

Ok, finally finished a 'working' version , what I ended up doing was similar to the original idea above.

Pick a starting square.

scan along x to find the max possible range. (maxX)

scan along y to find the maximum possible range. (maxY)

brute force test each possible rectangles from (maxX , maxY) to (1,1) to see which had the largest valid area (where all squares are of the correct type and are unprocessed).

mark all the squares under that rectangle as processed.

repeat for next valid square through the grid.

It ended up being a little more complicated then I'd originally planned but it works nicely with L-shaped section and other oddities.

I can probably make a few optimisations on it now it's stable but it processes quickly for my largest levels so am not too worried.

Thanks again for the help.

**Edited by xexuxjy, 16 November 2012 - 09:48 AM.**