I am trying to script a Firing Computer for my Artillery Model, to be displayed as a GUI. The goal of it is to display accurate information about how to hit a location that would be provided by the player by input. My current goal is to make it calculate the angle needed to hit it, but the calculations (in degrees) make the artillery overshoot the target.
local Artillery = script.Parent -- The Artillery Model. local Missile = Artillery:WaitForChild("Missile") -- The projectile being fired. local Base = Artillery:WaitForChild("Base") -- The bottom base of the artillery model. local arctan, root, degrees = math.atan, math.sqrt, math.deg -- Shortened mathematic functions. local MissileMass = Missile.Size.X * Missile.Size.Y * Missile.Size.Z -- The mass of the missile. local v = 500 -- The velocity of the missile when it. local v2 = v^2 -- The velocity of the missile, to the power of 2. local v4 = v^4 -- The velocity of the missile, to the power of 4. local g = (MissileMass * 196.2) -- The force of the gravity that is pulling down the missile. local x = ((Base.Position - game.Workspace.Target.Position).magnitude) -- The Range of the Base from the Target. local gx = g * x -- Gravity times Range. local y = (Base.Position.Y - game.Workspace.Target.Position.Y) -- The height difference of the Base and Target. local gx2 = g * (x ^ 2) -- G times X to the power of 2. local yv2 = (2 * y * v2) -- 2 times Y times V to the power of 2. local discriminant = root(v4 - (g * (gx2 + yv2))) -- The discriminant of the Quadratic Formula used in both roots. local solution = arctan((v2 + discriminant) / gx) -- The Arc Tangent of one root of the Quadratic Formula equation. local solution2 = arctan((v2 - discriminant) / gx) -- The Arc Tangent of the second root of the Quadratic Formula equation. print(degrees(solution2)) print(degrees(solution))
Answer found! I have searched through the ScriptingHelpers archive and found another article that asks a similar question. By reading through the answers, player TurboFusion provided a link to a website involving projectile trajectories, where I found the answer.
The updated code as of 2/24/2016 is this :
local Artillery = game.Workspace:WaitForChild("Artillery"):WaitForChild("Model") -- The Artillery model, paired with a Regeneration button so the actual model is inside that. local Missile = Artillery:WaitForChild("Missile") -- The missile being fired from the Artillery. local Base = Artillery:WaitForChild("Base") -- The base/bottom of the artillery. local arcsin, root, degrees = math.asin, math.sqrt, math.deg -- Prewritten math functions Arcsin, the Square Root, and the conversion of Radians to Degrees. local MissileMass = Missile:GetMass() -- The mass of the missile. local v = Artillery.FiringData.BulletVelocity.Value -- The initial velocity of the projectile. local g = 196.2*MissileMass -- Gravity affecting the missile, in this case the regular ROBLOX gravity. local a = (Base.Position.X - game.Workspace.Target.Position.X)^2 local b = (Base.Position.Z - game.Workspace.Target.Position.Z)^2 local R = root(a+b) -- Factoring in only horizontal distance to the calculations. local solution = degrees(arcsin(g*R/v^2))/2 -- The answer found as shown above. local solution2 = 90 - solution -- The link above also shows that the two firing angles are complementary, allowing for this. print(solution2) -- Print the two firing solutions. print(solution)