This is a really interesting and cool project youâre working on, and I hope this will help you.
Basically youâll need to develop an algorithm or function to find if an area is enclosed within your voxel system. A word of warning - any function that tries to do this is likely going to be expensive, and you should only run the function when something is changed that requires you to test if it is enclosed.
Essentially, you should have a voxel coordinate outside of the area (preferably outside the âbuild areaâ if you have one), and a coordinate inside the area (a certain voxel position that you are 100% positive is inside the enclosed area) and use some kind of path-finding algorithm to find out if there is a path that can connect the two. If the path finding algorithm finishes without completing the path, you know that the area is enclosed.
Iâm not the best with code, but hopefully this could give you some insight as to how you may go about doing this. There are also many path-finding algorithms with varying efficiencies and benefits, so I recommend doing some research into this, and hopefully another member of the community could provide some practical application to the task such as some code.
I was thinking of something like shooting rays out from every single block and see if it hits something and then create a part from the furthest points it reaches, but i am not sure if this is going to cause a lot of lag or if its possible to do.
I was thinking about using rays also, but I had concerns about efficiency, and how it would work in the case of bends, corners, or if it wasnât perfectly convex. Iâll have a look, but I think path-finding is going to be the best way of doing this.
If you have a way of collecting all the data into a 3 dimensional table with values of wither 1 or 0 (1 representing a voxel, 0 representing no voxel) you could do a 2D scan going through each iteration of the 3rd axis to see if the 2D cross-section is a complete loop. You could have to do the cross-section scan twice in two different directions to determine if the whole thing is enclosed, but i think i could devise some code to do that for you.
I might be able to slowly go through the whole creation whenever the creation touches the water for the first time and store everything in a table like that.
I could check if the objectspace position matches and store everything.
If you put the entire creation into a 3D array, you can actually do a rigorous check for this. What you need to do is write a 3D âflood fillâ algorithm. With that, you could use one of two approaches:
You could start inside an area that is meant to be enclosed and check if the flood fill hits the edges of the grid
You could start at the edge of the grid and check which cells do not get filled
This isnât something you want to do often, though. Ideally, youâd only ever do this when the submarine changes state, so once itâs launched or if parts of it are damaged/built while itâs deployed.