# How to detect floating voxel block?

I’m trying to implement detection of floating voxels, I’ve tried multiple times but still can’t get it fully working. Attached is a great description of what I want to achieve. How would you implement this?

Hi, I am not sure how advanced are you with programming. I would use so called Breadth First Search (BFS). I would store these blocks in a 3D grid. I dont see a point why to keep the distances but you can have them if you would like to. For pure detection I would have just True and False. True means the block is connected to the base and False means its not connected to the base. When you place a new block, you put it into the Queue (check the BFS algorithm). Then you repeat: 1. Take a block from the Queue 2. Check if it has some True neighbouring blocks - if so mark the block as True 3. Put all of his False neighbours to the Queue and Go to step 1.

i ended up solving my problem by implementing CCL (Connected Component Labeling)
this is the main method for finding and labeling grid blocks that are connected or not.
this is in my main grid builder class (the class that instantiates the grid and such).

``````    private TileGridBlock block;
private int nextLabel;
public void FindConnectedBlocks()
{
nextLabel = 2;

for (int y = 0; y < tileGridHeight; y++)
{
for (int z = 0; z < tileGridWidth; z++)
{
for (int x = 0; x < tileGridWidth; x++)
{
block = gridBlocks[y + tileGridHeight * (z + tileGridWidth * x)];

//SET BLOCK ID'S
//if block enabled; id = 1
if (block.GetEnabled())
{
block.id = 1;
//block.name = block.id.ToString();
}
//if block disabled; id = 0
else
{
block.id = 0;
//block.name = block.id.ToString();
}
//SET BLOCK ID'S

//LABEL BLOCKS
if (block.id == 1)
{
//if it has no neighbour, assign new label
//left = 0, back = 5, backleft = 6, backright = 7, below = 17, belowleft = 18, belowback = 23, belowbackleft = 24, belowbackright = 25
if (block.neighbourGridBlocks[0] != null && block.neighbourGridBlocks[0].id == 0 &&
block.neighbourGridBlocks[5] != null && block.neighbourGridBlocks[5].id == 0 &&
block.neighbourGridBlocks[6] != null && block.neighbourGridBlocks[6].id == 0 &&
block.neighbourGridBlocks[7] != null && block.neighbourGridBlocks[7].id == 0 &&
block.neighbourGridBlocks[17] != null && block.neighbourGridBlocks[17].id == 0 &&
block.neighbourGridBlocks[18] != null && block.neighbourGridBlocks[18].id == 0 &&
block.neighbourGridBlocks[23] != null && block.neighbourGridBlocks[23].id == 0 &&
block.neighbourGridBlocks[24] != null && block.neighbourGridBlocks[24].id == 0 &&
block.neighbourGridBlocks[25] != null && block.neighbourGridBlocks[25].id == 0)
{
block.label = nextLabel;
nextLabel += 1;
block.name = block.label.ToString();
//labels = new int[nextLabel - 1];
}
//if it has a neighbour with label, label = neighbour label
//left
if (block.neighbourGridBlocks[0] != null && block.neighbourGridBlocks[0].id == 1)
{
block.label = block.neighbourGridBlocks[0].label;
block.name = block.label.ToString();
}
//back
if (block.neighbourGridBlocks[5] != null && block.neighbourGridBlocks[5].id == 1)
{
block.label = block.neighbourGridBlocks[5].label;
block.name = block.label.ToString();
}
//backleft
if (block.neighbourGridBlocks[6] != null && block.neighbourGridBlocks[6].id == 1)
{
block.label = block.neighbourGridBlocks[6].label;
block.name = block.label.ToString();
}
//backright
if (block.neighbourGridBlocks[7] != null && block.neighbourGridBlocks[7].id == 1)
{
block.label = block.neighbourGridBlocks[7].label;
block.name = block.label.ToString();
}
//below
if (block.neighbourGridBlocks[17] != null && block.neighbourGridBlocks[17].id == 1)
{
block.label = block.neighbourGridBlocks[17].label;
block.name = block.label.ToString();
}
//belowleft
if (block.neighbourGridBlocks[18] != null && block.neighbourGridBlocks[18].id == 1)
{
block.label = block.neighbourGridBlocks[18].label;
block.name = block.label.ToString();
}
//belowback
if (block.neighbourGridBlocks[23] != null && block.neighbourGridBlocks[23].id == 1)
{
block.label = block.neighbourGridBlocks[23].label;
block.name = block.label.ToString();
}
//belowbackleft
if (block.neighbourGridBlocks[24] != null && block.neighbourGridBlocks[24].id == 1)
{
block.label = block.neighbourGridBlocks[24].label;
block.name = block.label.ToString();
}
//belowbackright
if (block.neighbourGridBlocks[25] != null && block.neighbourGridBlocks[25].id == 1)
{
block.label = block.neighbourGridBlocks[25].label;
block.name = block.label.ToString();
}
}
}
}
}
}
``````

and on each gridblock i have this in update to check for shortest neighbour label
then I simply check if non connected blocks are a specific label and I make them float.
hope this helps someone, cheers.

``````void Update()
{
if (GetEnabled() && coord.y > 0)
{
foreach (TileGridBlock gb in neighbourGridBlocks)
{
if (gb != null && gb.GetEnabled())
{
if (gb.label < label)
{
label = gb.label;
name = label.ToString();
}
}
}
``````