Well, I’ve thought about this for quite some time now, and I’ve come to hit what I believe is a roadblock. If you’re really going to help me, prepare for a bit of a read since I tend to over-explain things.
See, each of my voxels currently stores its own position values in 3 integers: x, y, z. I understand that this is very inefficient in most cases, but more on that in a second. Within my current structure, the voxels are assigned into groups that don’t have a fixed count to them, nor a fixed position; they need to be dynamic as the player will be interacting with them, and also each group needs to be able to do things on its own - move, rotate, etc. When I got to the point of storing the data, I had two separate ideas, which are below. I’m doing the second option currently.
// first option
private Voxel[][][] group_voxels = new Voxel[w][h][l];
// second option?
private List<Voxel> voxels = new ArrayList<Voxel>();
Now, the question comes down to efficiency of storage and data, as well as perhaps processing and rendering speed - though I’m not 100 percent certain about that last one.
For the first option, the pros are that I can infer the integer positions for each voxel through the index it’s at in the 3d array. However, doing this also creates a lot of wasted space, since odds are the entire group of voxels will never be filled (Giant cube structures aren’t going to be very common). For example, if I have a tree that’s 48 voxels tall, 25 voxels wide, and 25 voxels in depth overall, the empty space (like around the trunk) is sitting there and taking up memory (I think?) where it could be better spent on other things. It also introduces a bit of unneccessary checking when looping through, since it needs to check those empty spaces as well as the far more important occupied spaces.
For the second option, the plus is that there shouldn’t ever be wasted space inside of the list, since only enough voxels are assigned at any point to the list to create their object; the wasted space around the tree trunk would disappear since the list never even bothers about that spot. In addition, I believe that there would be a boost in general speed since when iterating through the list, it again never bothers to check what isn’t there. However, with this method, I can’t come up with any way to infer the position of the voxels through usage of indices; the list doesn’t know anything about the width or length or height, it just stores the voxels. This leads me to using the 3-integer method described as above.
My question is, which is more efficient? I’m not that experienced with serious programming structure and the sort, usually I just throw it together without a care for the efficiency, so I’d like a few opinions as well as maybe some tips and tricks if anyone’s willing to share.
Also, if you actually read all of that, thanks for sticking with it. I can really be a little too wordy at times.