Advertisement
Guest User

Lua basic collisions with shapes

a guest
Jun 13th, 2023
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | Source Code | 0 0
  1. --[[
  2.     R - Rectangle   : X,  Y,  W,  H
  3.     C - Circle      : X,  Y,  R
  4.     L - Line        : X1, Y1, X2, Y2
  5.     P - Point       : X,  Y
  6.     B - Buffer
  7. ]]--
  8.  
  9. function RRCollision( R1, R2 )
  10.  
  11.     return R1[1] < R2[1] + R2[3] and R1[2] < R2[2] + R2[4] and R2[1] < R1[1] + R1[3] and R2[2] < R1[2] + R1[4]
  12.  
  13. end
  14.  
  15. function CCCollision( C1, C2 )
  16.  
  17.     local CX = C1[1] - C2[1]
  18.     local CY = C1[2] - C2[2]
  19.     local CD = math.sqrt( CX^2 + CY^2 )
  20.  
  21.     return CD <= ( C1[3] + C2[3] )
  22.  
  23. end
  24.  
  25. function LLCollision( L1, L2 )
  26.  
  27.     local uA1 = ( L1[4] - L1[3] ) * ( L2[1] - L2[3] ) - ( L2[4] - L2[3] ) * ( L1[1] - L1[3] )
  28.     local uA2 = ( L2[4] - L2[3] ) * ( L1[2] - L1[1] ) - ( L1[4] - L1[3] ) * ( L2[2] - L2[1] )
  29.     local uA = uA1 / uA2
  30.  
  31.     local uB1 = ( L1[2] - L1[1] ) * ( L2[1] - L2[3] ) - ( L2[2] - L2[1] ) * ( L1[1] - L1[3] )
  32.     local uB2 = ( L2[4] - L2[3] ) * ( L1[2] - L1[1] ) - ( L1[4] - L1[3] ) * ( L2[2] - L2[1] )
  33.     local uB = uB1 / uB2
  34.  
  35.     return uA >= 0 or uA <= 1 or uB >= 0 or uB <= 1
  36.  
  37. end
  38.  
  39. function PPCollision( P1, P2, B )
  40.  
  41.     return P1[1] >= P2[1] - B and P1[2] <= P2[2] + B
  42.  
  43. end
  44.  
  45. function RCCollision( R, C )
  46.  
  47.     local calX = ( C[1] < R[1] ) and R[1] or ( C[1] > R[1] + R[3] ) and R[1] + R[3] or C[1]
  48.     local calY = ( C[2] < R[2] ) and R[2] or ( C[2] > R[2] + R[4] ) and R[2] + R[4] or C[2]
  49.  
  50.     local DX = C[1] - calX
  51.     local DY = C[2] - calY
  52.     local Dis = math.sqrt( DX^2 + DY^2 )
  53.  
  54.     return Dis <= C[3]
  55.  
  56. end
  57.  
  58. function RLCollision( R, L )
  59.  
  60.     local RL = {}
  61.  
  62.     RL[1] = { R[1], R[2], R[1], R[2] + R[4] }
  63.     RL[2] = { R[1], R[2] + R[4], R[1] + R[3], R[2] + R[4] }
  64.     RL[3] = { R[1] + R[3], R[2] + R[4], R[1] + R[3], R[2] }
  65.     RL[4] = { R[1] + R[3], R[2], R[1], R[2] }
  66.  
  67.     local CL = {}
  68.  
  69.     for i = 1, #RL do
  70.  
  71.         table.insert( CL, LLCollision( L, RL[i] ) )
  72.  
  73.     end
  74.  
  75.     for i = 1, #CL do
  76.  
  77.         if CL[i] then
  78.  
  79.             return true
  80.  
  81.         end
  82.  
  83.     end
  84.  
  85.     return false
  86.  
  87. end
  88.  
  89. function CLCollision( C, L )
  90.  
  91.     local I1 = CPCollision( { L[1], L[2] }, C )
  92.     local I2 = CPCollision( { L[3], L[4] }, C )
  93.     if I1 or I2 then
  94.         return true
  95.     end
  96.  
  97.     local DX = L[1] - L[3]
  98.     local DY = L[2] - L[4]
  99.     local LN = math.sqrt( DX^2 + DY^2 )
  100.  
  101.     local DOT = ( ( C[1] - L[1] ) * ( L[3] - L[1] ) + ( C[2] - L[2] ) * ( L[4] - L[2] ) ) / LN^2
  102.  
  103.     local CX = L[1] + DOT * ( L[3] - L[1] )
  104.     local CY = L[2] + DOT * ( L[4] - L[2] )
  105.  
  106.     local OnSeg = LPCollision( L, { CX, CY } )
  107.     if not OnSeg then
  108.         return false
  109.     end
  110.  
  111.     local DX = CX - C[1]
  112.     local DY = CY - C[2]
  113.     local Dis = math.sqrt( DX^2 + DY^2 )
  114.  
  115.     return Dis <= C[3]
  116.  
  117. end
  118.  
  119. function RPCollision( R, P )
  120.  
  121.     return P[1] >= R[1] and P[1] <= R[1] + R[3] and P[2] >= R[2] and P[2] <= R[2] + R[4]
  122.  
  123. end
  124.  
  125. function CPCollision( C, P )
  126.  
  127.     local DX = P[1] - C[1]
  128.     local DY = P[2] - C[2]
  129.     local Dis = math.sqrt( DX^2 + DY^2 )
  130.  
  131.     return Dis < C[3]
  132.  
  133. end
  134.  
  135. function LPCollision( L, P, B )
  136.  
  137.     local D1 = math.sqrt( ( P[1] - L[1] )^2 + ( P[2] - L[2] )^2 )
  138.     local D2 = math.sqrt( ( P[1] - L[3] )^2 + ( P[2] - L[4] )^2 )
  139.     local LL = math.sqrt( ( L[1] - L[3] )^2 + ( L[2] - L[4] )^2 )
  140.  
  141.     return D1 + D2 >= LL - B and D1 + D2 <= LL + B
  142.  
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement