Advertisement
eruaaaaaaa

Untitled

May 14th, 2022 (edited)
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. --//Most formulas used are from https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/
  2.  
  3. local Polynomial = loadstring(game:HttpGet("https://pastebin.com/raw/cJLiwfs9"))()
  4. local Ballistic = {}
  5.  
  6. local math = math
  7. Ballistic.Gravity = 196.2
  8.  
  9. local rad45 = math.rad(45)
  10. local msin = math.sin(rad45)
  11. local mcos = math.cos(rad45)
  12.  
  13. local up = Vector3.new(0, 1, 0)
  14. local onefourth = .25
  15. local onehalf = .5
  16.  
  17. local sqrtwo = math.sqrt(2)
  18.  
  19. function Ballistic.GetMaxTime(speed, height)
  20. --return (2 * speed * msin + height) / Ballistic.Gravity3
  21. local holder = {}
  22.  
  23. local yspeed = msin * speed
  24. local yspeedsq = yspeed * yspeed
  25.  
  26. local ans = Polynomial.SolveQuadric(Ballistic.Gravity, yspeed, height, holder)
  27. --local ans2 = (-yspeed + math.sqrt(yspeedsq + 2 * Ballistic.Gravity * -height)) / Ballistic.Gravity
  28. --local ans3 = (-yspeed - math.sqrt(yspeedsq + 2 * Ballistic.Gravity * -height)) / Ballistic.Gravity
  29.  
  30. --print(table.find(holder, ans2))
  31.  
  32. if ans > 0 then
  33. return math.max(unpack(holder)) - math.min(unpack(holder))
  34. end
  35. end
  36.  
  37. function Ballistic.GetRange(speed, height)
  38. return speed * mcos * (speed * msin + math.sqrt(speed * speed * msin * msin + 2 * Ballistic.Gravity * height)) / Ballistic.Gravity
  39. end
  40.  
  41. function Ballistic.GetRangeAtTime(tim, speed)
  42. return tim * speed * mcos
  43. end
  44.  
  45. function Ballistic.IsInRange(origin, speed, target, tvel)
  46. local diff = origin - target
  47.  
  48. if tvel then
  49. local maxtime = Ballistic.GetRange(speed, diff.Y) / (speed * mcos)
  50. local newtargetpos = target + tvel * maxtime
  51. local newthing = origin - newtargetpos
  52. local range = Ballistic.GetRangeAtTime(maxtime, speed)
  53.  
  54.  
  55. return range >= Vector3.new(newthing.X, 0, newthing.Z).Magnitude
  56. end
  57.  
  58. local range = Ballistic.GetRange(speed, diff.Y)
  59. return range >= diff.Magnitude
  60. end
  61.  
  62. function Ballistic.SolveStationary(origin, speed, target, islob)
  63. local dir = target - origin
  64. local xdir = Vector3.new(dir.X, 0, dir.Z)
  65.  
  66. local xdist = xdir.Magnitude
  67. local sqspeed = speed * speed
  68. local quspeed = sqspeed * sqspeed
  69. local xgrav = Ballistic.Gravity * xdist
  70.  
  71. local sol = quspeed - Ballistic.Gravity * (xgrav * xdist + 2 * dir.Y * sqspeed)
  72.  
  73. if sol < 0 then
  74. return
  75. end
  76.  
  77. local ang = math.atan2(sqspeed + (islob and math.sqrt(sol) or -math.sqrt(sol)), xgrav)
  78. return xdir.Unit * math.cos(ang) + up * math.sin(ang), xdir.Magnitude / (speed * math.cos(ang))
  79. end
  80.  
  81. --//http://playtechs.blogspot.com/2007/04/aiming-at-moving-target.html
  82. function Ballistic.SolveMoving(origin, speed, target, tvel, islob)
  83. local roots = {}
  84. local diff = target - origin
  85.  
  86. local H = diff.X
  87. local J = diff.Z
  88. local K = diff.Y
  89. local L = -onehalf * Ballistic.Gravity
  90.  
  91. local P = tvel.X
  92. local Q = tvel.Y
  93. local R = tvel.Z
  94.  
  95. local c0 = L * L
  96. local c1 = -2 * Q * L
  97. local c2 = Q * Q - 2 * K * L - speed * speed + P * P + R * R
  98. local c3 = 2 * (K * Q + H * P + J * R)
  99. local c4 = K * K + H * H + J * J
  100.  
  101. local num = Polynomial.SolveQuartic(c0, c1, c2, c3, c4, roots)
  102.  
  103. if num > 0 then
  104. local bullet, lob = math.huge, math.huge
  105.  
  106. for i = 1, num do
  107. local val = roots[i]
  108.  
  109. if val > 0 then
  110. if val < bullet then
  111. lob = bullet
  112. bullet = val
  113. elseif val < lob then
  114. lob = val
  115. end
  116. end
  117. end
  118.  
  119. if bullet ~= math.huge then
  120. local smallest = (islob and lob ~= math.huge and lob) or bullet
  121. return Vector3.new(H / smallest + P, K / smallest + Q - L * smallest, J / smallest + R), islob and lob or bullet;
  122. end
  123. end
  124. end
  125.  
  126. function Ballistic.SolveMoving2(origin, speed, jump, grav, target, tvel, islob)
  127. local roots = {}
  128.  
  129. local g = grav
  130. local diff = target - origin
  131.  
  132. local c0 = g * g * onefourth
  133. local c1 = -tvel.Y * g
  134. local c2 = tvel:Dot(tvel) + g * -diff.Y - speed * speed
  135. local c3 = 2 * (tvel:Dot(diff) - speed * jump)
  136. local c4 = diff:Dot(diff) - jump * jump
  137.  
  138. local num = Polynomial.SolveQuartic(c0, c1, c2, c3, c4, roots)
  139.  
  140. if num > 0 then
  141. local bullet, lob = math.huge, math.huge
  142.  
  143. for i = 1, num do
  144. local val = roots[i]
  145.  
  146. if val > 0 then
  147. if val < bullet then
  148. lob = bullet
  149. bullet = val
  150. elseif val < lob then
  151. lob = val
  152. end
  153. end
  154. end
  155.  
  156. if bullet ~= math.huge then
  157. local smallest = (islob and lob ~= math.huge and lob) or bullet
  158. local divisor = speed * smallest + jump
  159.  
  160. return (diff + tvel * smallest - Vector3.new(0, onehalf * g * smallest * smallest, 0)) / divisor, islob and lob or bullet
  161. end
  162. end
  163. end
  164.  
  165. return Ballistic
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement