Linetest Triangle
Much like testing a line and an axis aligned or OBB intersection, testing a line and triangle intersection utilizes the existing Raycast
function. We are going to cast a ray against the triangle being tested. If the Raycast succeeds, we need to make sure that the t value is along the line segment being tested.
Getting ready
We are about to implement a new Linetest
function, which will test if a line and a triangle intersect. This function returns a Boolean result.
How to do it…
Follow these steps to check if a line intersects a triangle:
- Declare the new
Linetest
function inGeometry3D.h
:bool Linetest(const Triangle& triangle, const Line& line);
- Implement the
Linetest
function inGeometry3D.cpp
:bool Linetest(const Triangle& triangle, const Line& line) {
- Construct a ray out of the line being tested:
Ray ray; ray.origin = line.start; ray.direction = Normalized(line.end - line.start);
- Perform a Raycast:
float t = Raycast(triangle, ray);
- Check that...