Voxel Collisions?

Hey guys! So over the past few days I have been working on my own “Minecraft clone” as some would call it. I have block rendering working and I now would like to implement collisions. In Minecraft there are bounding boxes, and for blocks that have irregular shapes (hopper, torch, lever, button, fence, etc.) the block can define one or more custom bounding boxes instead of just a single one meter squared box. I would like this same functionality in my game since it seems easiest to use to me.

My question is how? What strategy do I use to implement this? I really have NO IDEA.

EDIT: it somehow slipped my mind to mention that I am using LWJGL for this.

To decrease the amount of collision detection you have to do, first divide your world into segments. The simplest way voxel engines do it (including Minecraft) is using ‘chunks’: an WxHxD ‘chunk’ of blocks that have a defined boundary. You first find which chunk the object you are testing is in, then you can either subdivide again if the chunk is large enough, or you can check against each block. Alternatively, if the blocks are laid in a repeating fashion, collision detection can be done in O(1) time to find the location of the block the object is supposed to be in, and testing against that.

Google is your friend for the math part of testing if a point is inside a box.

My world is divided into chunks which are 16x256x16. Those are then divided into subchunks which are 16x16x16.

Really what I’m unsure of is which math (there’s multiple ways to do collision as far as I have seen) should I use and how should I use it?

Break it down into what two shapes you want to test for: you want a “player” vs “block” I’m assuming? Both can have simple bounding boxes and the math is straightforward to look up and implement (AABB is your best bet). Minecraft’s blocks are regularly placed, which means there is no need to loop through every single block: just use the player’s location to determine the location of the blocks around him and check against those.