Is there a way to check if an area is sealed off?

I was wondering if there is a way to see if the area inside of a submarine is sealed off or not.

It could be done via 1.25 stud voxels if that makes it easier.

I am working on a feature which allows players to create submarines which they can walk in or boats that can sink.

The players can place blocks on a 1.25 studs voxel grid.

3 Likes

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.

Anyways, I hope this helps. Good luck!

2 Likes

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 have no idea how i can acomplish this for now.

2 Likes

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.

2 Likes

There shouldn’t be any rotated parts, all of the parts should be placed in a grid, unless someone used a motor and rotated it.

So i think if you would fire a ray from each block to all surfaces, it could work, but i came up with another method while thinking about this.

Maybe it might be better to check all of the welded parts instead,

  1. Go through all of the connected parts.
  2. Store everything in a table with an orientation difference of 90. Example: Vec3(0,180,90)
  3. Turn the parts into multiple surfaces.
  4. Check if the surfaces make up an open area inside?

I have no idea how i could acomplish this though.

2 Likes

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.

2 Likes

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.

2 Likes

Yea that would definitely work. It would also be a good idea to store it all so that you could save/load creations for future use if needed.

1 Like

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:

  1. You could start inside an area that is meant to be enclosed and check if the flood fill hits the edges of the grid

  2. 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.

2 Likes