<p>Given an intersection algorithm like <a href="https://gamedev.stackexchange.com/a/18459">the one you linked</a> which first generates a <span class="math-container">$t$</span> value for every plane of the AABB, then computes <span class="math-container">$t_{min}$</span> and <span class="math-container">$t_{max}$</span> from those, you simply need to figure out which of the six initial values correspond to your final <span class="math-container">$t_{min}$</span>. There will be two matches if your ray hits an edge, three if it hits a corner. In that case, this may include a back face (see the fix below).</p>

<p>This will not require a tolerance as <span class="math-container">$t_{min}$</span> is always one of the six values. In fact, it is always one of three intermediate values and you can use that to make your check simpler, faster and better. Given code, from the link above, such as:</p>

<pre><code>float tmin = max(max(min(t1, t2), min(t3, t4)), min(t5, t6));
</code></pre>

<p>you could rewrite as:</p>

<pre><code>float tminx = min(t1, t2);
float tminy = min(t3, t4);
float tmin = max(max(tminx, tminy), min(t5, t6));
int axis = 2;
if (tmin == tminx) axis = 0;
if (tmin == tminy) axis = 1;
</code></pre>

<p>As an added bonus, unlike testing all six values, this will not match the back face in the event that a far edge is hit.</p>

CC BY-SA 4.0
2020-01-24T02:44:36.760Z
I see. So, for example, if a ray hit the left side of the box, passed through, and then exited from the right side, the final tmin should equal tminx, and the final tmax should equal tmaxx. And if it hits an edge on entry, then tmin should equal *two* of (tminx, tminy, tminz), right?
Tyler Shellberg
CC BY-SA 4.0
2020-01-24T12:11:12.260Z
@TylerShellberg that's it exactly. And if you're lucky enough to hit the near corner, it will equal all three.
Olivier
CC BY-SA 4.0
2020-01-22T01:35:51.820Z
2020-01-22T01:35:51.820Z
Olivier
<p>There are plenty of well-known algorithms for determining <em>if</em> a ray hit an Axis-Aligned Bounding Box (AABB), like Andrew Woo's covered in a response <a href="https://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms">here</a>. </p>

<p>Using <code>exit = origin + (dir * tMax)</code> will give you the exact position of exit using distance through the ray. (and same for tMin and the entry)</p>

<p>However, I need a way to not only do that, but also get if we hit the left, right, top, bottom, front, or rear face of the AABB.</p>

<p>The naive approach would be to do some kind of floating-point comparison given the minpoint/maxpoint and try to see what side (if any) the point exactly equals in each dimension, or exactly equals within a tolerance. That seems error-prone, I was wondering if there was a better way.</p>

<p>Is there an algorithm for this?</p>

<raytracing><algorithm>
Ray vs AABB algorithm that also gives which side was hit?
Tyler Shellberg
# Ray vs AABB algorithm that also gives which side was hit?

(Asked by: username_0 on 2020-01-21)

There are plenty of well-known algorithms for determining *if* a ray hit an Axis-Aligned Bounding Box (AABB), like Andrew Woo's covered in a response [here](https://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms).

Using `exit = origin + (dir * tMax)` will give you the exact position of exit using distance through the ray. (and same for tMin and the entry)

However, I need a way to not only do that, but also get if we hit the left, right, top, bottom, front, or rear face of the AABB.

The naive approach would be to do some kind of floating-point comparison given the minpoint/maxpoint and try to see what side (if any) the point exactly equals in each dimension, or exactly equals within a tolerance. That seems error-prone, I was wondering if there was a better way.

Is there an algorithm for this?

**Tags:** raytracing, algorithm


---

## 1 answers

### Answer by username_1 on 2020-01-22. Score: 2 | Accepted answer

Given an intersection algorithm like [the one you linked](https://gamedev.stackexchange.com/a/18459) which first generates a $t$ value for every plane of the AABB, then computes $t\_{min}$ and $t\_{max}$ from those, you simply need to figure out which of the six initial values correspond to your final $t\_{min}$. There will be two matches if your ray hits an edge, three if it hits a corner. In that case, this may include a back face (see the fix below).

This will not require a tolerance as $t\_{min}$ is always one of the six values. In fact, it is always one of three intermediate values and you can use that to make your check simpler, faster and better. Given code, from the link above, such as:

```
float tmin = max(max(min(t1, t2), min(t3, t4)), min(t5, t6));

```

you could rewrite as:

```
float tminx = min(t1, t2);
float tminy = min(t3, t4);
float tmin = max(max(tminx, tminy), min(t5, t6));
int axis = 2;
if (tmin == tminx) axis = 0;
if (tmin == tminy) axis = 1;

```

As an added bonus, unlike testing all six values, this will not match the back face in the event that a far edge is hit.

> **Comments:**
> **username_0**: I see. So, for example, if a ray hit the left side of the box, passed through, and then exited from the right side, the final tmin should equal tminx, and the final tmax should equal tmaxx. And if it hits an edge on entry, then tmin should equal *two* of (tminx, tminy, tminz), right?  
> **username_1**: @username_0 that's it exactly. And if you're lucky enough to hit the near corner, it will equal all three.  

---

