<p>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.</p>

<p>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. </p>

CC BY-SA 4.0
2020-02-18T23:24:27.027Z
2020-02-18T23:24:27.027Z
Alan Liu
<p>I have a situation where (due to floating point error) a ray that is <em>known</em> to hit a bounding box fails due to floating point error. (1e-8 or so)</p>

<p>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.</p>

<p>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: </p>

<p><a href="https://i.sstatic.net/LtRig.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LtRig.png" alt="enter image description here"></a></p>

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

<p>In 2D this is apparently relatively easy, as you can get distance between two lines like <a href="http://csharphelper.com/blog/2014/08/find-the-shortest-distance-between-two-line-segments-in-c/" rel="nofollow noreferrer">here</a>, then find the smallest of the 4 lines and use that. </p>

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

CC BY-SA 4.0
2020-02-19T09:57:09.943Z
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.
Simon F
<raytracing><computational-geometry><geometry>
Determining shortest vector between a ray and an axis-aligned 3D bounding box (correcting a near-miss)
Tyler Shellberg
# Determining shortest vector between a ray and an axis-aligned 3D bounding box (correcting a near-miss)

(Asked by: username_0 on 2020-02-18)

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](https://i.sstatic.net/LtRig.png)](https://i.sstatic.net/LtRig.png)

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](http://csharphelper.com/blog/2014/08/find-the-shortest-distance-between-two-line-segments-in-c/), 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?

**Tags:** raytracing, computational-geometry, geometry

> **Comments:**
> **username_1**: 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.  

---

## 1 answers

### Answer by username_2 on 2020-02-18. Score: 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.


---

