- Home /
Grid Shape Detection
Hello,
I am working on a project that has a 2D-like grid system that have cubes grouping dynamically with one another to create a new shape. When the shape becomes a solid "block" shape, it is then "complete".
What would be the best algorithmic approach to detecting that all the shapes grouped with one another are a "completed block"?
Background:
I have a dynamic amount of cubes that fit into a single grid spot on the playfield grid.
All connected/touching cubes are grouped together in a "cube group".
I need to check this cube group's current "shape" by iterating through all the cubes inside of it; if it's entirely filled in, then it should be set as "complete".
Incomplete Shape
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
Complete Shape
1 1 1
1 1 1
1 1 1
Thanks in advance for any insight into this! Cheers~
Answer by bf0wl3 · Apr 18, 2012 at 07:09 PM
Thanks for the input and suggestions.
The solution I came up with was really quite simple in theory. I'm not sure if it's the "best" or most optimal way, but I am only performing the check every-so-often and not on a per-frame basis.
I got the minX-maxX and minY-maxY ranges of the child objects inside the grid shape. I took the (maxX-minX)*(maxY-minY) and compared that to the childCount of the grid shape.
Since I am only looking for a completed shape, if the grid shape is a 4x4 shape it will check that the childCount is 16. Otherwise it is not "completed" yet.
I had also done an exhaustive loop checking the top-left to the bottom-right of the grid object and making sure there is an object in that grid space, which worked, but is not quite as good as the childCount.
Thanks again, the Unity community rocks!
Answer by inewland · Apr 13, 2012 at 06:49 PM
Just a stab in the dark here, from what I understand...
Couldn't you just iterate each row looking for '1'...if each row has a 1 bool success, otherwise return false?
You could have predefined arrays setup defining your shapes.
Cube Shape
Array[2][2] = { 1, 1,
1, 1 };
When iterating the shape, look for matches in your arrays.
Answer by rutter · Apr 13, 2012 at 06:58 PM
You might try some variation on flood fill algorithms, or check out this Stack Overflow thread which seems to be asking a very similar question.
Your answer
Follow this Question
Related Questions
How to de-pixelize pixel art (smooth out) 2d 0 Answers
2D Sandbox physics problem 1 Answer
board game algorithm 0 Answers
How to make 2d distortion? 2 Answers
Get position from Isometric TileMap 1 Answer