# When storing voxels in chunks, how do I access them at the world level?

I'm not looking for how to store the voxels, data wise; I'm looking for the theory. Currently, my voxel engine has no global list of tiles. Each chunk has its own list, and its "hard to do things" like collision detection or anything that may use tiles that are outside of its own chunk. I want to start using height maps, but I have a problem. If each chunk has its own list, then it will be pretty difficult to implement the height maps.

Should I store a global list of voxels, independent of the chunks? Then, I can just easily perform collision detection and terrain generation. However, it would probably be harder to do things such as frustum culling.

So how should I store voxels?

• What do you think makes it hard for heightmap generation? Chunks are just some way to organize the data, just iterate over all chunks as you do with columns/voxels? – Mario Nov 13 '13 at 15:02
• I guess, but still. Storing a global list of voxels just seems so much more simple. I took a look at Minecraft's source code when it was still in a very alpha stage, and I believe it had one list of tiles, and not a list per chunk. – opiop65 Nov 13 '13 at 15:03
• How are you planning to remove the voxels from the global list when a chunk is removed? I can pretty much assure you that it will be easier if every chunk has it's own list/array of voxels. One thing you could try is to do something like that chunk.get(-1, 0, 0) basically return chunk.left.get(chunk_width - 1, 0, 0) so that you dont have to write any hacky code in your collision detection/anywhere else. – user9790 Nov 13 '13 at 15:29

You don't need a new way of storing them, just just need a new way of accessing them. Storing the data in lists in the chunks is fine. You just need a way to index into them at the world level.

The world should contain a list of chunks currently loaded, and the chunks contain lists of voxels. Each chunk should be the same size (contain the same volume of the world). Now when you want to access a specific voxel in the world, you only need to convert world coordinates into two pieces of data: chunk and chunk coordinates.

Imagine a 2D example. You have a 2D world where each chunk contains 10 x 10 pixels. At the world level you want to access the pixel data for pixel (342,103). First you need to find the chunk that contains the data. You can easily find that because you know each chunk contains 10 x 10 pixels. To get chunk coordinates, just divide by the number of pixels per chunk, (int)342/10 = 34 and (int)103/10 = 10, giving you chunk (34,10).

Now you just need to get the coordinates within the chunk. Those coordinates can easily be found by getting the remainder after the division, i.e. a modulo operation. 342%10 = 2 and 103%10 = 3, giving you pixel (2,3).

That means world position (342,103) can be found in chunk (34,10) at position (2,3).

Think of it like graph paper, where the larger thicker lines show you where the chunks are and the smaller grids are the data each chunk holds.

The system used to access each can be very similar. It's just that one system holds chunks, while the other holds voxels.