Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Merging adjacent boxes to produce smallest set?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 xexuxjy   Members   -  Reputation: 632

Like
0Likes
Like

Posted 15 November 2012 - 04:59 AM

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,

Sponsor:

#2 Waterlimon   Crossbones+   -  Reputation: 2601

Like
1Likes
Like

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


o3o


#3 caldiar   Members   -  Reputation: 702

Like
1Likes
Like

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

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


#4 Waterlimon   Crossbones+   -  Reputation: 2601

Like
0Likes
Like

Posted 15 November 2012 - 02:32 PM

Yeah, a quadtree is faster and simpler (and its possible to edit it dynamically) but its limited in that if the contiguous regions are on the borders of the quadtree cells it wont be optimal... better than a simple grid tho.

o3o


#5 xexuxjy   Members   -  Reputation: 632

Like
0Likes
Like

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.



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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS