Search code examples
algorithmlinevoxelbresenham

Finding Line of Sight in a 3D Grid of Voxel Cubes


I'm developing an application in which voxel cubes are stored in a basic 3D array such that Array.[z][x][y] will access the voxel at height layer z, row x, column y.

I'd like to implement a line of sight check between two cubes (say A and B) where if any point in A has a line of sight to any point of B, then line of sight exists. Each cube is either seen or unseen, no in-between.

However, any of the six faces of a cube can count as an obstructing wall. A cube might have walls on all four sides but not on the top and bottom for example, or walls on just two sides.

Pretty much everything I've found looking for a solution points to a 3D modification of the Bresenham algorithm, like the approach described here.

But I also keep seeing cautions about Bresenham leaving out cubes along the path and there being better alternatives. It also seems like following Bresenham wouldn't give me data about which face of a cube has been encountered, so I'm not sure how I'd properly check if a wall was hit or not in a cube that the line visits. I just can't seem to find much about what the alternatives to Bresenham actually are, either.

Is 3D Bresenham a good way to proceed here? Or maybe because my voxels all have the same width/height/length, there's a better way that won't miss as many cubes?


Solution

  • For retrieving all cells between two points in A and B you can use Woo and Amanatides grid traversal algorithm.
    I am not sure that you'll get all cells touched by "tunnel" formed with A nd B cells.

    "Fast Voxel Traversal Algorithm..."

    Example of implementation

    2D case illustration:

    enter image description here