3D line segment AABB collision, with hit normal?

by ps48   Last Updated October 18, 2019 02:13 AM - source

I'm embarrassed I can't find this, but I'm wanting to detect intersection with a 3D line segment (not an infinite ray) with a 3D AABB, the AABB being defined as two Vec3f's which represent the Min and Max extents. So the AABB can be arbitrarily-sized. I also need the surface normal of the AABB, if there was a hit.

From looking at similar algorithms I at least know it seems good to calculate the inverse direction of the line beforehand, at least, if you're needing to check against multiple AABBs per frame.

I have

struct AABB
    VEC3F min;
    VEC3F max;

// a and b representing start/end points of the line segment
// returns true if intersects, also fills out "normal" if true
bool LineIntersectsAABB(const VEC3F& a, const VEC3F& b,
    const VEC3F& inv_dir, const AABB& aabb,
    VEC3F* normal);

The implementations I've found either do not find the hit normal, and/or they're intended for boxes/cubes where the three dimensions of the box are always equal length, which doesn't work for me. Implementations seem to vary greatly, which is confusing for me (who is trying to understand it), and, considering that I need the hit normal, I'd imagine that that may rule out certain implementations.

Related Questions

Line of Sight Algorritm

Updated September 26, 2018 19:13 PM

Simulating "line of sight" with obstacles on 2d grid?

Updated February 28, 2017 08:13 AM