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.

01local Artillery = script.Parent -- The Artillery Model.
02local Missile = Artillery:WaitForChild("Missile") -- The projectile being fired.
03local Base = Artillery:WaitForChild("Base") -- The bottom base of the artillery model.
04 
05local arctan, root, degrees = math.atan, math.sqrt, math.deg -- Shortened mathematic functions.
06local MissileMass = Missile.Size.X * Missile.Size.Y * Missile.Size.Z -- The mass of the missile.
07 
08local v = 500 -- The velocity of the missile when it.
09local v2 = v^2 -- The velocity of the missile, to the power of 2.
10local v4 = v^4 -- The velocity of the missile, to the power of 4.
11 
12local g = (MissileMass * 196.2) -- The force of the gravity that is pulling down the missile.
13local x = ((Base.Position - game.Workspace.Target.Position).magnitude) -- The Range of the Base from the Target.
14local gx = g * x -- Gravity times Range.
15local y = (Base.Position.Y - game.Workspace.Target.Position.Y) -- The height difference of the Base and Target.
View all 24 lines...
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
9 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 :

01local Artillery = game.Workspace:WaitForChild("Artillery"):WaitForChild("Model") -- The Artillery model, paired with a Regeneration button so the actual model is inside that.
02local Missile = Artillery:WaitForChild("Missile") -- The missile being fired from the Artillery.
03local Base = Artillery:WaitForChild("Base") -- The base/bottom of the artillery.
04 
05local arcsin, root, degrees = math.asin, math.sqrt, math.deg -- Prewritten math functions Arcsin, the Square Root, and the conversion of Radians to Degrees.
06local MissileMass = Missile:GetMass() -- The mass of the missile.
07local v = Artillery.FiringData.BulletVelocity.Value -- The initial velocity of the projectile.
08local g = 196.2*MissileMass -- Gravity affecting the missile, in this case the regular ROBLOX gravity.
09 
10local a = (Base.Position.X - game.Workspace.Target.Position.X)^2
11local b = (Base.Position.Z - game.Workspace.Target.Position.Z)^2
12local R = root(a+b) -- Factoring in only horizontal distance to the calculations.
13 
14local solution = degrees(arcsin(g*R/v^2))/2 -- The answer found as shown above.
15local solution2 = 90 - solution -- The link above also shows that the two firing angles are complementary, allowing for this.
16 
17print(solution2) -- Print the two firing solutions.
18print(solution)
Ad

Answer this question