I have a 3D grid of cubes, and using the algorithm from this paper http://www.cs.yorku.ca/~amana/research/grid.pdf I've been able to get the integer coordinates of the first tile intersected by a random line.

My question is, is there any simple way to get the intersection floating point position? I've been able to get the normal, but not the position...


The algorithm you linked is known as 3D-DDA. (2D-DDA is the 2D version). When you collide with a voxel it is with either tMaxX, tMaxY, or tMaxZ since that's when the ray intersects that 3d plane. You just need to take:

length = tMax? / rayDirection?

So if you collide with the tMaxX plane then you take:

length = tMaxX / rayDirectionX

where rayDirectionX is the X component of the normalized ray direction. Length in this case is the distance along the ray until it collided. Thus:

intersectionPoint = rayPosition + rayDirection * length

Sidenote: You can take intersectionPointY and intersectionPointZ and modulus them with the voxelSize to get the offset inside of the voxel face. Useful for texture lookups. (Obviously which two axis you modulus with depends on which plane you've hit).

  • \$\begingroup\$ Thank you so much, I was so confused, thinking it would be the whole tmax or rayPosition + delta of every step... \$\endgroup\$ – evilpudding Jun 29 '17 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.