Advertisement
glitchdetector

FiveM Lua Trig

Aug 8th, 2020 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. function GetDistanceToPointBetweenCoords(pos, start, stop)
  2.     local startDist, stopDist, length = #(pos - start), #(pos - stop), #(start - stop)
  3.     return GetDistanceToLine(startDist, stopDist, length)
  4. end
  5.  
  6. function GetDistanceToPointAlongCoords(pos, start, stop)
  7.     local startDist, stopDist, length = #(pos - start), #(pos - stop), #(start - stop)
  8.     return GetDistanceToLineInf(startDist, stopDist, length)
  9. end
  10.  
  11. function GetDistanceToLine(startDist, stopDist, length)
  12.     if math.max(startDist, stopDist) > length then
  13.         return math.min(startDist, stopDist)
  14.     end
  15.     return GetDistanceToLineInf(startDist, stopDist, length)
  16. end
  17.  
  18. function GetDistanceToLineInf(startDist, stopDist, length)
  19.     local s = (1/2) * (startDist + stopDist + length)
  20.     local area = math.sqrt(s * (s - startDist) * (s - stopDist) * (s - length))
  21.     return area / (length / 2)
  22. end
  23.  
  24. -- Get angle between two coords via an origin point
  25. -- https://i.imgur.com/2GVvt5I.png
  26. -- Returns a negative value is target is on the left side of the line
  27. function CalculateAngleBetweenPoints(origin, line, target)
  28.     local p1x, p1y = origin.x, origin.y
  29.     local p2x, p2y = line.x, line.y
  30.     local p3x, p3y = target.x, target.y
  31.     local numerator = p2y*(p1x-p3x)+p1y*(p3x-p2x)+p3y*(p2x-p1x)
  32.     local denominator = (p2x-p1x)*(p1x-p3x)+(p2y-p1y)*(p1y-p3y)
  33.     local ratio = numerator / denominator
  34.     local anglerad = math.atan(ratio)
  35.     local angledeg = (anglerad*180)/math.pi
  36.     return angledeg
  37. end
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement