Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --//Most formulas used are from https://www.forrestthewoods.com/blog/solving_ballistic_trajectories/
- local Polynomial = loadstring(game:HttpGet("https://pastebin.com/raw/cJLiwfs9"))()
- local Ballistic = {}
- local math = math
- Ballistic.Gravity = 196.2
- local rad45 = math.rad(45)
- local msin = math.sin(rad45)
- local mcos = math.cos(rad45)
- local up = Vector3.new(0, 1, 0)
- local onefourth = .25
- local onehalf = .5
- local sqrtwo = math.sqrt(2)
- function Ballistic.GetMaxTime(speed, height)
- --return (2 * speed * msin + height) / Ballistic.Gravity3
- local holder = {}
- local yspeed = msin * speed
- local yspeedsq = yspeed * yspeed
- local ans = Polynomial.SolveQuadric(Ballistic.Gravity, yspeed, height, holder)
- --local ans2 = (-yspeed + math.sqrt(yspeedsq + 2 * Ballistic.Gravity * -height)) / Ballistic.Gravity
- --local ans3 = (-yspeed - math.sqrt(yspeedsq + 2 * Ballistic.Gravity * -height)) / Ballistic.Gravity
- --print(table.find(holder, ans2))
- if ans > 0 then
- return math.max(unpack(holder)) - math.min(unpack(holder))
- end
- end
- function Ballistic.GetRange(speed, height)
- return speed * mcos * (speed * msin + math.sqrt(speed * speed * msin * msin + 2 * Ballistic.Gravity * height)) / Ballistic.Gravity
- end
- function Ballistic.GetRangeAtTime(tim, speed)
- return tim * speed * mcos
- end
- function Ballistic.IsInRange(origin, speed, target, tvel)
- local diff = origin - target
- if tvel then
- local maxtime = Ballistic.GetRange(speed, diff.Y) / (speed * mcos)
- local newtargetpos = target + tvel * maxtime
- local newthing = origin - newtargetpos
- local range = Ballistic.GetRangeAtTime(maxtime, speed)
- return range >= Vector3.new(newthing.X, 0, newthing.Z).Magnitude
- end
- local range = Ballistic.GetRange(speed, diff.Y)
- return range >= diff.Magnitude
- end
- function Ballistic.SolveStationary(origin, speed, target, islob)
- local dir = target - origin
- local xdir = Vector3.new(dir.X, 0, dir.Z)
- local xdist = xdir.Magnitude
- local sqspeed = speed * speed
- local quspeed = sqspeed * sqspeed
- local xgrav = Ballistic.Gravity * xdist
- local sol = quspeed - Ballistic.Gravity * (xgrav * xdist + 2 * dir.Y * sqspeed)
- if sol < 0 then
- return
- end
- local ang = math.atan2(sqspeed + (islob and math.sqrt(sol) or -math.sqrt(sol)), xgrav)
- return xdir.Unit * math.cos(ang) + up * math.sin(ang), xdir.Magnitude / (speed * math.cos(ang))
- end
- --//http://playtechs.blogspot.com/2007/04/aiming-at-moving-target.html
- function Ballistic.SolveMoving(origin, speed, target, tvel, islob)
- local roots = {}
- local diff = target - origin
- local H = diff.X
- local J = diff.Z
- local K = diff.Y
- local L = -onehalf * Ballistic.Gravity
- local P = tvel.X
- local Q = tvel.Y
- local R = tvel.Z
- local c0 = L * L
- local c1 = -2 * Q * L
- local c2 = Q * Q - 2 * K * L - speed * speed + P * P + R * R
- local c3 = 2 * (K * Q + H * P + J * R)
- local c4 = K * K + H * H + J * J
- local num = Polynomial.SolveQuartic(c0, c1, c2, c3, c4, roots)
- if num > 0 then
- local bullet, lob = math.huge, math.huge
- for i = 1, num do
- local val = roots[i]
- if val > 0 then
- if val < bullet then
- lob = bullet
- bullet = val
- elseif val < lob then
- lob = val
- end
- end
- end
- if bullet ~= math.huge then
- local smallest = (islob and lob ~= math.huge and lob) or bullet
- return Vector3.new(H / smallest + P, K / smallest + Q - L * smallest, J / smallest + R), islob and lob or bullet;
- end
- end
- end
- function Ballistic.SolveMoving2(origin, speed, jump, grav, target, tvel, islob)
- local roots = {}
- local g = grav
- local diff = target - origin
- local c0 = g * g * onefourth
- local c1 = -tvel.Y * g
- local c2 = tvel:Dot(tvel) + g * -diff.Y - speed * speed
- local c3 = 2 * (tvel:Dot(diff) - speed * jump)
- local c4 = diff:Dot(diff) - jump * jump
- local num = Polynomial.SolveQuartic(c0, c1, c2, c3, c4, roots)
- if num > 0 then
- local bullet, lob = math.huge, math.huge
- for i = 1, num do
- local val = roots[i]
- if val > 0 then
- if val < bullet then
- lob = bullet
- bullet = val
- elseif val < lob then
- lob = val
- end
- end
- end
- if bullet ~= math.huge then
- local smallest = (islob and lob ~= math.huge and lob) or bullet
- local divisor = speed * smallest + jump
- return (diff + tvel * smallest - Vector3.new(0, onehalf * g * smallest * smallest, 0)) / divisor, islob and lob or bullet
- end
- end
- end
- return Ballistic
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement