I've been wanting to design a game with a destructible voxel world with physics. I want there to be a custom voxel world, and explosions will take out voxels. Easy. The hard part is preventing floating chunks of voxels (meaning voxels that are not attached to the ground). I have created a demo of the voxel function, but it has a few flaws that I will talk about later.
But here is essentially how I went about designing this demo. All the voxels initially have physics, but have 'immovable' checked in order to prevent them from moving and keep performance acceptable. When there is an explosion, all voxels in a certain range get 'immovable' unchecked and impulse away from the explosion. Then a voxel update is called.
When the game is called to update the world, the game first set s all voxels 'IsOnGround' instance variable to false, then checks to see which ones need to be true. It first checks all of the voxels connected to the ground and sets their 'IsOnGround' to true. Each of these checks the four positions immediately around it, and if there is a voxel in those positions, the process is repeated for each of them. This is visualized in my demo by the red and blue colors each voxel is set to. This works very well, although it is somewhat slow as not to overload the computer.
The part I have trouble with is collapsing the voxels that still have their 'IsOnGround' variable to false after the check. The only way I could think of is having a sprite check each row a few seconds after the update check is called. When the sprite overlaps a voxel, it checks the voxel to see if the voxel's 'IsOnGround' is true or false, collapsing the voxels that are false. This sprite is the red bar that moves across the screen in my demo.
This works but has a few issues. It is slow, as floating voxels will not be collapse until a few seconds after the update. The other issue is that when dealing with complex shapes, the checking may not have time to complete before the sprite comes and collapsed voxels. The final issue is when explosions occur before the first explosion checking is complete, which could mess up the whole system.
Here is the demo file: https://www.dropbox.com/s/9889wxtjayvdyhv/Voxel%20Physics.c3p?dl=0
Do you have any ideas on how to collapse the voxels that are not attached to the ground?
Thanks in advance,
Give me a Pixel
Here is a capx that finds the floating chunks. It's probably not the same as your project but maybe some of it would be a useful reference.
The way it finds the floating chunks is similar to your way. It starts by marking the bottom layer, then visiting each marked and marks their four neighbors. It knows it's done when all the marked have been visited. You then know all the floating blocks are the ones that aren't marked. I utilized an else for that.
An idea I utilized to make it faster is to use a lookup array with the uids of the objects in a grid. So you can find what immovable block is in a location with lookup.at(int(x/32), int(y/32)). This is much faster than relying on the overlap checks that construct provides with that many objects. A side effect of this is we can make the floating island faster too.
Another idea to make it render faster is to use a tilemap for all the non moving tiles and just create the movable sprites as needed. The drawback is you'd have to do the floading blocks check in a different way.
Anyways, maybe you can use some ideas from that capx.
I integrated your example into mine and it worked perfectly. You're a genius, thanks so much!
Develop games in your browser. Powerful, performant & highly capable.
Cool. Glad it was useful.