<p>PBRT has code for axis-aligned bounding boxes.</p>
<p><a href="https://www.pbr-book.org/3ed-2018/Shapes/Basic_Shape_Interface#Bounds3::IntersectP" rel="nofollow noreferrer">https://www.pbr-book.org/3ed-2018/Shapes/Basic_Shape_Interface#Bounds3::IntersectP</a></p>
<p>They only have an intersection predicate for occlusion rays, but extending this to give you the normals and support non-axis-aligned bounding boxes is simple.</p>
<p>To acquire the normals, you create a normal in the proper direction for the hit slab. It loops through the x, y, and z directions, and since this is axis aligned, you return (1, 0, 0), (0, 1, 0), or (0, 0, 1). There is also a check to see if the hit values are reversed, in which case the normals are reversed, e.g. (-1, 0, 0).</p>
<p>If you want to support non-axis-aligned bounding boxes, it's probably easiest to apply the inverse of the box's transform to the intersection ray and then do the axis-aligned test as above. In the end, you apply the transform to the output normals (remember that transforming normals has different rules than transforming vectors).</p>

CC BY-SA 4.0
2022-12-21T18:25:13.770Z
2022-12-21T18:25:13.770Z
Keith
<p>Denote you ray as <span class="math-container">$\vec{P} + \vec{D}*t $</span> and <span class="math-container">$\vec{S}_h$</span> be the half size of the box (ie <span class="math-container">$ \vec{S}_h = (1,1,1)$</span>). The following equation must be satisfied (with abusive notation): <span class="math-container">$ || \frac{\vec{P} + \vec{D}*t}{\vec{S}_h} - 1 ||_{L_{\infty}} = 1 $</span> where vector-division is defined as <span class="math-container">$\frac{\vec{a}}{\vec{b}} = (a_1/b_1,a_2/b_2,a_3/b_3,...)$</span> and subtraction <span class="math-container">$\vec{a} - b = (a_1-b,a_2-b,a_3-b,...)$</span>. Note that the max norm is used. There are branchless solutions to finding both solutions to <span class="math-container">$t$</span> in this equation.</p>
<p>To get the normal after computing <span class="math-container">$t$</span>: evaluate the vector inside the norm, find the basis vector corresponding to the component with largest absolute value and multiply it by the sign of the component.</p>

CC BY-SA 4.0
2023-02-01T09:51:58.040Z
2023-02-01T09:51:58.040Z
Mathis
<p>I've searched everywhere for an equation that suits my needs but i couldn't find anything useful. I'm making a voxel ray-tracer so I need an equation that gets me the intersection of a ray with a box, and the box's normal.</p>
<p>I looked at inigo quilez's <a href="https://iquilezles.org/articles/intersectors/" rel="nofollow noreferrer">intersection code</a> but I can't derive the equation from the code shown so i need the equation itself rather than code.</p>
<hr />
<p>Say we have the ray's origin <span class="math-container">$O(-5, 1, 0)$</span> and the box position <span class="math-container">$B(0, 0, 0)$</span> with the ray's direction as a unit vector being <span class="math-container">$(1, 0, 0)$</span> and the box's side lengths being <span class="math-container">$(2, 2, 2)$</span></p>
<ol>
<li><p>I need the equation to return the distance between the point where the ray hits the box, and the ray's origin</p>
</li>
<li><p>I need the box's normal where the intersection is</p>
</li>
</ol>

<raytracing>
ray-box intersection
Not Gartificial
# ray-box intersection

(Asked by: username_0 on 2022-12-21)

I've searched everywhere for an equation that suits my needs but i couldn't find anything useful. I'm making a voxel ray-tracer so I need an equation that gets me the intersection of a ray with a box, and the box's normal.

I looked at inigo quilez's [intersection code](https://iquilezles.org/articles/intersectors/) but I can't derive the equation from the code shown so i need the equation itself rather than code.

---

Say we have the ray's origin $O(-5, 1, 0)$ and the box position $B(0, 0, 0)$ with the ray's direction as a unit vector being $(1, 0, 0)$ and the box's side lengths being $(2, 2, 2)$

1. I need the equation to return the distance between the point where the ray hits the box, and the ray's origin
2. I need the box's normal where the intersection is

**Tags:** raytracing


---

## 2 answers

### Answer by username_1 on 2022-12-21. Score: 2

PBRT has code for axis-aligned bounding boxes.

<https://www.pbr-book.org/3ed-2018/Shapes/Basic_Shape_Interface#Bounds3::IntersectP>

They only have an intersection predicate for occlusion rays, but extending this to give you the normals and support non-axis-aligned bounding boxes is simple.

To acquire the normals, you create a normal in the proper direction for the hit slab. It loops through the x, y, and z directions, and since this is axis aligned, you return (1, 0, 0), (0, 1, 0), or (0, 0, 1). There is also a check to see if the hit values are reversed, in which case the normals are reversed, e.g. (-1, 0, 0).

If you want to support non-axis-aligned bounding boxes, it's probably easiest to apply the inverse of the box's transform to the intersection ray and then do the axis-aligned test as above. In the end, you apply the transform to the output normals (remember that transforming normals has different rules than transforming vectors).


---

### Answer by username_2 on 2023-02-01. Score: 1

Denote you ray as $\vec{P} + \vec{D}\*t $ and $\vec{S}\_h$ be the half size of the box (ie $ \vec{S}\_h = (1,1,1)$). The following equation must be satisfied (with abusive notation): $ || \frac{\vec{P} + \vec{D}\*t}{\vec{S}\_h} - 1 ||\_{L\_{\infty}} = 1 $ where vector-division is defined as $\frac{\vec{a}}{\vec{b}} = (a\_1/b\_1,a\_2/b\_2,a\_3/b\_3,...)$ and subtraction $\vec{a} - b = (a\_1-b,a\_2-b,a\_3-b,...)$. Note that the max norm is used. There are branchless solutions to finding both solutions to $t$ in this equation.

To get the normal after computing $t$: evaluate the vector inside the norm, find the basis vector corresponding to the component with largest absolute value and multiply it by the sign of the component.


---

