If there is a given triangle (tx1, ty1, tz1), (tx2, ty2, tz2), (tx3, ty3, tz3) and two given point of a line (lx1, ly1, lz1), (lx2, ly2, lz2), then how to calculate, if the line hits the triangle, or not? And if yes, where?
How to calculate triangle-line collision in 3D?
919 Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 3 best solutions below
On
Since solving linear systems of equations may not be nice in realtime, consider the following:
Find the plane of the triangle. Project the line onto that plane. This may make it a point, may make it a line. If there is a solution, the projected line must cross at least one edge of the triangle or sit entirely inside it. Then for the line to be the solution to the collision test, it must have an intersection point with the plane of the triangle.
If it has an intersection point with that plane, the line intersects the triangle if and only if the point of intersection is within the triangle.
On
There is good C++ code at the Geometric Tools site.
He has functions that do triangle/segment, triangle/ray, or triangle/line intersections.
The advantage of reading code, as opposed to a mathematical description is that the code considers floating point tolerances (if it's good code, anyway). This is important in intersection problems, even in simple ones like this. The mathematical account just says you should check that two numbers are equal, or that some number is equal to zero. In floating point arithmetic, these sorts of tests won't work, of course.
So, take your pick -- a mathematics solution or a software solution. They are based on the same principles, mostly, but the details are different.
Such an intersection point $\vec p$ would be a solution of $$ \tag1\begin{cases}\vec p=a \vec t_1+b \vec t_2+c \vec t_3=d \vec l_1+e \vec l_2\\ a+b+c=d+e=1\end{cases}$$ with additional constraint that $a,b,c\ge 0$ (and also $d,e\ge0$ if you want to intersect the line segment, not the infinitely long line). As $(1)$ is a linear system of five equations in five unknowns,