3D voxel collision

So I’m making a game where everything is made up of voxels, including the map. I’ve got simple 3D collision detection and handling working exactly how I want it to, except for one thing. I have to loop through every voxel in the map! This is what I used to do for 2D tile-based games, and it worked fine, but I assume this will take a toll on my game’s performance since there’s a whole new dimension to loop through. Is there a way I can only check collision with voxels near the player?

You only need to check for collisions in the area of things that are moving, which is the bounding cube of the minimum and maximum x,y,z of the start and end locations of everything that moved subsequent to the last check. This can be pruned down further, but it’s a good place to start.

If your using voxels, I assume that you are using an array. I am doing 2D collisions in my game using this:


public Block getBlock(float xx, float yy)
{
	int x = Math.round(xx);
	int y = Math.round(yy);
	int chunkX = (int) Math.floor( x/320 );
	int chunkY = (int) Math.floor( y/240 );
	Chunk chunk = getChunk(chunkX, chunkY);
		
	int blockX = ( (x%320)/16);
	int blockY = ( (y%240)/16);
	return chunk.getBlock(blockX, blockY);
}

public Block getBlock(int x, int y)
{
	return level[x][y];
}

Note that the first half of the first function is for getting a world location, and the next gets the block. The second function is what the chunk uses to find that block. As you can see, it doesn’t have to loop through every block. Unfortunately, this does not work in negative. Or, I haven’t found out how to, others can probably find out how to do that.

Math.floor() isn’t needed! Integer division results in the floor!

Not for negative numbers.

The fact that you are doing the division between two integers means that the result too will be an integer, and obviously is already “rounded”, as in rounded towards zero. The floors! They do nothing!