In 3D space, the intersection algorithm of straight lines and triangles is the most basic algorithm for collision detection and selection in computer 3D graphics.
The pick example in DirectX SDK provides the original code and has different understandings of this Code.
Here, we use an affine coordinate system to describe it.
Basic knowledge
Spatial plane equation, N * P + D = 0; or N * P = D,
Here * is the point multiplication of the vector, n is the normal vector of the plane, and P is any point on the plane.
D indicates the distance from the origin point to the plane, or the distance from the plane to the origin point. Note that this distance is a vector.
Distance from point to space plane
For N * P = D, D is the distance from the origin point to the plane.
N * P0-D is the distance from point to plane
To solve the intersection problem between a line segment and a spatial triangle, we can solve it in two steps.
Step 1
Calculates the intersection of a line segment and a space triangle in the plane.
Calculates the distance between the two endpoints of a line segment and the plane. If the symbols are the same, the distance between the two endpoints and the plane must be in the same test.
The point at which the distance difference can be obtained is 0. This point is the point of intersection.
Step 2: whether the intersection is inside the spatial triangle
In this case, we can use the affine Coordinate System Decomposition Method to Calculate
First, we will introduce what is an affine coordinate system.
We are familiar with the Cartesian coordinate system of the flute. The two coordinate axes are perpendicular to each other.
The two coordinate axes are non-vertical in the affine coordinate system. As long as they are not parallel, they can form an affine coordinate system.
Obviously, the Cartesian coordinate system of flute is a special case of the affine coordinate system.
The following facts exist in both orthogonal and affine coordinate systems:
Assume that the vectors of the two coordinate axes are X and Y.
Any point P in the space can be expressed as P = O + x * x + y * y
O is the origin, and X and Y are the coordinates in this coordinate system.
Any point in the space. In this coordinate system, the values of X and Y are unique.
For a point in space, it is used to make parallel lines of the coordinate axis and intersection with the coordinate axis. The distance from the point of intersection to the origin is the coefficient.
For orthogonal coordinate systems, parallel lines and coordinate axes enclose a rectangle.
The parallel line and coordinate axis are not a parallelogram for the affine coordinate system.
If we regard a vertex of a triangle as the origin of the affine coordinate system, and the two edges from the vertex are used as the unit vectors of the two coordinate axes of the affine coordinate system, then, the coordinate values of any point in the plane under the affine coordinate system can distinguish the relationship between the point and the triangle. If the coordinates are u and v, u> 0, V> 0 indicates that the point is in the first quadrant.
U + v <= 1 indicates that the point is inside the triangle.
If 0 <= u <= 1 and 0 <= V <= 1, The point is inside the parallelogram composed of two edges.
Based on the principle, we can determine whether the vertex is inside a triangle or parallelogram.
Specific calculation method
The three vertices of a triangle are P1, P2, P3, and point po on the same plane.
The two edge vectors are u = P2-P1 v = P3-P1
Point po' = po-p1 in this coordinate system
After solving this equation, we can obtain the coordinates in the affine coordinate system:
Po' = u * u + V * V
Because the point is three-dimensional, as long as the number of U and V locations, we can calculate the two coordinate components in the three-dimensional coordinates.
In fact, we only need to find the two coordinate components with small absolute values among the three coordinate components of the flat vector.
In fact, this is just the solution of a binary equations.
No need to give specific algorithms