Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I calculate the trajectory of a projectile? [Closed]

Asked by
pyro89 50
9 years ago

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))
0
Edit your question, highlight all your code and click the blue Lua button, it'll put all your code in a code block so we can read it easier :) YellowoTide 1992 — 9y
0
Edited to add code block. BlueTaslem 18071 — 9y
0
Thank you for the edit, I am new to scriptinghelpers though the website is very useful for situations like this. pyro89 50 — 9y
0
Btw, you can get the mass of an object using :GetMass(). it will return the equation you did on line 6 HungryJaffer 1246 — 9y
0
At this point in time, Litozinnamon has already found out the answer with the advent of the Ballistics Tracker in the game Phantom Forces. pyro89 50 — 9y

1 answer

Log in to vote
0
Answered by
pyro89 50
8 years ago

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)
Ad

Answer this question