08 October, 2013

Collisions with triblocks

So I've run into another problem using triblock style voxels.  First a little background into how voxel engines work.  In games like minecraft it seems like there are millions of blocks that are all separate and independent, but the reality is that if each block had to be individually drawn it would slow the system down considerably.  To avoid these millions of draw calls the blocks are instead organized into chunks.  A chunk may be an area of say 16x16x16 blocks that will take into consideration whether the blocks are filled or not and then draws a mesh to represent all of the filled blocks that are next to empty blocks.  When the player interacts with the world and places or removes blocks then the chunk( or chunks in the case of being right on the edge) is redrawn.  Because the whole thing is a mesh together, the collision of the ray from the player's point of view (represented as the sight in the middle of your screen, usually a cross or box) has to determine which box in the chunk is being selected for change, and it accomplishes that in most systems by going halfway into a block and calculating where it is.  Because cubes line up with orthogonal directions fro x, y, and z in a 3d system then they use those directions to identify the block they are in and do the necessary change.  No matter where the collision occurs on the chunk the test will always find the resulting blocks.

My problem is that using equilateral triangles for one of the planes creates a system where sides will be facing directions other than the orthogonal directions of x, y, and z.  To complicate things further if I were to do the same test by going in a half block distance into the block or out from the block using the direction(or negative direction) of the normal vector and the collision occurs near one of the edges I will identify the wrong block for deletion/addition into the mesh.  This is kind of a big problem. One possible solution would be to have each mesh store a triangle list, with each triangle pointing to the block it belongs to and the block it is facing.  The problem with this approach is that even more will have to be stored in memory during run time, and voxel games are already huge memory hogs.  Another issue is that to cut down on the amount of drawing that has to be done if several faces are in the same plane often the triangles are enlarged to cover just the edges of what is showing.  
So what this means is I won't be able to do the large areas I was hoping for back when I first had this grand idea.  Unless of course I figure out how to overcome these obstacles.  Anyway if you are interested in a better explanation please visit https://sites.google.com/site/letsmakeavoxelengine/.  It had a pretty good explanation of what goes into a voxel engine.