Lets say we have 4 Vector2
's (yes this is 2d), so we have lineOneStart, lineOneEnd, lineTwoStart and lineTwoEnd.
How can I detect if the 2 lines cross? I don't care where they cross, I just want to know if they intersect.
Lets say we have 4 Vector2
's (yes this is 2d), so we have lineOneStart, lineOneEnd, lineTwoStart and lineTwoEnd.
How can I detect if the 2 lines cross? I don't care where they cross, I just want to know if they intersect.
Check this formula by Bourke.
I recently had to solve this issue too. Another option is using (getting) the equation of the line (y = mx + c
) but there are several edge cases you need to be concerned with, as well as actually checking if the point of intersection is within the line segment. The formula in the link above works, though I cannot really comment on how the equation is re-arranged, all I'll say is it works ;)
Edit:
As mentioned by AndiDog, another site I used (the example is excellent too) is this tutorial. As this is XNA the second link will be right up your street.