io7m-com/jspatial

Raycasts miss exact matches on boxes

Opened this issue · 6 comments

io7m commented

For example:

A box b is placed at (32, 32) of size (64, 64).

A ray is cast from (16, 32) to (256, 32). The ray should intersect b because it passes through 32 on the Y axis, and that's the minimum Y value of b. However, the current implementation misses the intersection.

io7m commented

This appears to be a bug in the calculation of the inverse ray vector. The problem is that given a ray like the above: (16, 32) with direction (1, 0), the inverse vector turns out to be (1, Infinity), which is definitely wrong!

io7m commented

No, this is actually an expected property of the rays. See: https://tavianator.com/fast-branchless-raybounding-box-intersections/

io7m commented

This is actually caused by NaN values appearing in the computation. Covered here: https://tavianator.com/fast-branchless-raybounding-box-intersections-part-2-nans/

io7m commented

Waiting to hear from the author about whether or not anything can be done about this. I can't work it out from the second part of the article.

I cover this in my latest post: https://tavianator.com/2022/ray_box_boundary.html. TLDR:

-        tmin = max(min(t1, t2), tmin);
-        tmax = min(max(t1, t2), tmax);
+        tmin = min(max(t1, tmin), max(t2, tmin));
+        tmax = max(min(t1, tmax), min(t2, tmax));
...
-    return tmin < tmax;
+    return tmin <= tmax;
io7m commented

Ah, thank you!

Sorry for the ridiculous delay in replying. I had no idea this had even been posted (I get far too many GitHub notifications for any human to deal with!). 🙂