Line intersection fails because of float precision?

FMath::SegmentIntersection2D fails only when two vectors are directed at eachother.
This might be a problem with float precision, as the most minimal difference in position / direction would cause an intersection to fail. What method is used in this situation as alternative?

Yellow line > Lines used as input for the intersection check.
Red dot > Intersection point. Top right fails.

afbeelding

Purely mathematically speaking, cross product will tell you if the vectors are colinear. From there you can figure out the rest in a few ways, easiest would be checking if one of the vector endpoints lies on a line segment between the two vectors positions by doing a dot product between new vector (positions of two vectors) with itself and comparing it to a dot product between new vector and one of original vectors. if the 2nd dot product is positive and smaller than the first one then the vectors are on the same line.

Unreal wise, you could do a cross product between the vectors, then if 0 do FMath::PointDistToLine and check one of vector end points distance to the line between the two vector positions.

1 Like