I’ve been working on a method of finding connected groups of blocks, and would like some feedback with regards to the method I’m using.
I’m only using parts here as a visual aid; the actual algorithm will be operating on Lua data.
Suppose we have a structure of blocks like this, where every block is snapped to a uniform grid:
Imagine all those blocks are welded together.
That red block is going to be deleted, and we would like to find what groups of connected blocks the structure will be split into. For example, the above structure would be split into two groups:
I’m currently using a simple flood fill algorithm to recursively spread throughout the structure and detect these groups. Starting from the red block, each direction is given its own group (represented as a unique colour) and spreads out as far as it can from that red block. If two groups meet, they merge into one group.
Here’s what that looks like:
This method works perfectly, but I’m concerned about performance, since it’s a recursive flood fill and those can get expensive with more complex structures. Is there a more optimal method for detecting these groups?
Again, the parts are just a visual aid, and the method will have to work on Lua data. Thanks in advance
edit: here’s the place file with the algorithm in. Feel free to have a play about, but I’d like to warn you that this is just prototype code, so it’s pretty messy: connections.rbxl (19.6 KB)