0
\$\begingroup\$

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;
    }

```
\$\endgroup\$
2
  • \$\begingroup\$ Can you provide a test case where this gives a different result than desired? \$\endgroup\$
    – DMGregory
    Aug 4 '20 at 16:00
  • \$\begingroup\$ Hi, I don't have a test case. For any ray which has origin inside the box, the tmax should be positive (must hit somewhere), tmin should be negative (it's behind the ray). Then t, tmax, tmin, they are the distance of ray traveled. @DMGregory \$\endgroup\$
    – iaomw
    Aug 4 '20 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.