I have a situation where (due to floating point error) a ray that is known to hit a bounding box fails due to floating point error. (1e-8 or so)

Expanding the box is not an acceptable solution in my specific use case. However, if I could "snap" the ray to the box, that would work out well.

In order to do so, I need to find the shortest vector from a given ray to a bounding box. In 2 dimensions, that would look like this:

enter image description here

If I can get that red vector, I can add it to the origin and the ray will be shifted to the bounding box.

In 2D this is apparently relatively easy, as you can get distance between two lines like here, then find the smallest of the 4 lines and use that.

But in 3D I am not aware of how to do this. Does anyone know if there is a way?

  • $\begingroup$ Unfortunately, I've not been able to find the presentation slides online, but I'm pretty sure at SIGGRAPH 2016, Luke Peterson of Imagination Technologies described (or at least hinted at) using the FP rounding modes to make the ray box test conservative. $\endgroup$
    – Simon F
    Feb 19, 2020 at 9:57

1 Answer 1


If you know the rounding error, you could subtract the error from your vectors so that rays would overlap the bounding box. This could be done in your intersection tests.

In 3D, computing shortest distance between a vector and face of a bounding box would be another way to shift the vector to overlap the bounding box. But I doubt this approach would work well as it essentially tries to fix a rounding problem.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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