Last active
February 9, 2017 16:25
-
-
Save XProger/260764166f7ef8f3f3a7895778e52545 to your computer and use it in GitHub Desktop.
AABB vs Ray intersection test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool AABB::intersect(const vec3 &rayPos, const vec3 &rayDir, float &t) const { | |
float t1 = INF, t0 = -t1; | |
for (int i = 0; i < 3; i++) | |
if (rayDir[i] != 0) { | |
float lo = (min[i] - rayPos[i]) / rayDir[i]; | |
float hi = (max[i] - rayPos[i]) / rayDir[i]; | |
t0 = _max(t0, _min(lo, hi)); | |
t1 = _min(t1, _max(lo, hi)); | |
} else | |
if (rayPos[i] < min[i] || rayPos[i] > max[i]) | |
return false; | |
t = t0; | |
return (t0 <= t1) && (t1 > 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment