Skip to content

Instantly share code, notes, and snippets.

@XProger
Last active February 9, 2017 16:25
Show Gist options
  • Save XProger/260764166f7ef8f3f3a7895778e52545 to your computer and use it in GitHub Desktop.
Save XProger/260764166f7ef8f3f3a7895778e52545 to your computer and use it in GitHub Desktop.
AABB vs Ray intersection test
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