Linetest Axis Aligned Bounding Box
We can use the existing Raycast against the AABB function to check if a line intersects an AABB. Given a line segment with end points A and B, we can create a ray out of the line:
ray.origin = A ray.direcion = Normalized(B - A);
With this ray, we can perform a Raycast. If the ray intersects the AABB, we check to make sure that the value of t is less than the length of the line. If it is, the segment intersects the Bounding Box:
Getting ready
We are going to implement a function to check if a Line and an AABB intersect. This function will return a Boolean result. We can avoid checking the length of the line segment by squaring it, and also squaring the value of t. That way, the actual comparison is done in a squared space.
How to do it…
Follow these steps to implement line testing against an AABB:
Declare the
Linetest
function inGeometry3D.h
:bool Linetest(const AABB& aabb, const Line& line);
Implement the
Linetest
function inGeometry3D.cpp
:bool Linetest...