most efficient AABB vs Ray collision algorithms
I found this topic on AABB ray test. Some of them could find hit point of external Ray and BOX. I did my version for both external and internal ray, but it seem not working for internal ray.
What's the problem on my solution? Or any good algo for this use case?
bool hit(thread Ray& ray, float2 range_t, thread HitRecord& record) const constant {
float tmin = range_t.x;
float tmax = range_t.y;
float3 normal = float3(0);
for (auto i : {0, 1, 2}) {
auto min_bound = (mini[i] - ray.origin[i])/ray.direction[i];
auto max_bound = (maxi[i] - ray.origin[i])/ray.direction[i];
auto ts = min(max_bound, min_bound);
auto te = max(max_bound, min_bound);
tmin = max(ts, tmin);
tmax = min(te, tmax);
if (tmax < tmin) { return false; }
//if (tmax <= tmin || tmax < 0) { return false; }
//if (tmax <= tmin || tmax < 0 || tmin < 0) { return false; }
if (ts >= tmin) {
//tmin = ts;
normal = float3(0);
normal[i] = abs(min_bound) < abs(max_bound)? -1:1;
}
}
record.t = tmin;
record.n = normal;
if (tmin < 0) {
record.t = tmax;
}
return true;
}
```
t, tmax, tmin
, they are the distance of ray traveled. @DMGregory \$\endgroup\$ – iaomw Aug 4 '20 at 16:05