Advertisement
casencty

line_collision(x1,y1,x2,y2,x3,y3,x4,y4)

Oct 15th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.58 KB | None | 0 0
  1. --https://gamedev.stackexchange.com/questions/26004/how-to-detect-2d-line-on-line-collision
  2. function line_collision(x1,y1,x2,y2,x3,y3,x4,y4)
  3.  
  4. denominator= ((x2 - x1) * (y4 - y3)) - ((y2 - y1) * (x4 - x3));
  5. numerator1 = ((y1 - y3) * (x4 - x3)) - ((x1 - x3) * (y4 - y3));
  6. numerator2 = ((y1 - y3) * (x2 - x1)) - ((x1 - x3) * (y2 - y1));
  7.  
  8. --// Detect coincident lines
  9. if (denominator == 0) then  
  10.   return (numerator1 == 0 and numerator2 == 0)
  11. else
  12.   r = numerator1 / denominator;
  13.   s = numerator2 / denominator;
  14.   return ((r >= 0 and r <= 1) and (s >= 0 and s <= 1));
  15. end
  16. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement