Advertisement
Guest User

Missile Range Calculator

a guest
May 15th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1.  
  2. missileComposition = {
  3.   length = 2,
  4.   thumper = false,
  5.   seekerHead = true, --set this to true if the missile has a radar seeker, infrared seeker (not the single pixel seeker) or laser designator receiver (not the beam rider receiver)
  6.   singlePixelSeeker = false, --set this to true only if the missile has a single pixel ir seeker
  7.   numberOfLowDragComponents = 0, --number of components with 0.01 base drag (should all be at the front, but behind the thumper or seeker if there is one)
  8.   numberOfHighDragComponents = 1, --number of components with 0.05 base drag (should all be at the end of the missle)
  9.   numberOfEjectors = 2,
  10.   numberOfTorpedoPropellers = 0,
  11.   numberOfThrusters = 1,
  12.   displayedThrust = 1000,
  13.   numberOfFuelTanks = 1,
  14.   numberOfRegulators = 0
  15. }
  16.  
  17. height = 10 --height at which the missile will fly. The range will depend on this because the drag depends on the height.
  18.  
  19.  
  20. --------------------------------------------------------------------------------------------------------------------------------------------------
  21.  
  22. local firstTime = true;
  23. function Update(I)
  24.   if firstTime then
  25.     local flightDistance = getDistanceAfterTime(missileComposition, missileComposition.effectiveTime, height);
  26.     I:Log("Missile will fly " .. flightDistance .. "m in " .. missileComposition.effectiveTime .. "s.");
  27.     firstTime = false;
  28.   end
  29. end
  30.  
  31.  
  32. function getDistanceAfterTime(m, totalFlightTime, height)
  33.   local distanceTraveled = 0;
  34.   local velocityMagnitude = 0;
  35.   local timeStep = 0.025
  36.   local time = 0
  37.   while time < totalFlightTime do
  38.     local accelerationMagnitude = getAcceleration(m, height, velocityMagnitude, timeStep)
  39.     velocityMagnitude = velocityMagnitude + accelerationMagnitude * timeStep
  40.     distanceTraveled = distanceTraveled + velocityMagnitude * timeStep
  41.     time = time + timeStep
  42.   end
  43.   return distanceTraveled;
  44. end
  45.  
  46. function getAcceleration(m, height, speed, timeStep)
  47.   local newSpeed = speed + getThrustAccelerationMagnitude(m) * timeStep;
  48.   return getThrustAccelerationMagnitude(m) - getDragAccelerationMagnitude(m, height, speed, newSpeed)
  49. end
  50.  
  51. function getDragAccelerationMagnitude(m, height, speed, newSpeed)
  52.   local airDensity = getAirDensityAtAltitude(height)
  53.   local dragMagnitudePerVelocity = math.max(0.1, airDensity * speed * m.dragCoefficient)
  54.   local dragMagnitude = dragMagnitudePerVelocity * newSpeed
  55.   return dragMagnitude
  56. end
  57.  
  58. function getThrustAccelerationMagnitude(m)
  59.   return m.thrust / m.mass
  60. end
  61.  
  62. function getAirDensityAtAltitude(altitude)
  63.   if altitude < 0 then
  64.     return 7
  65.   elseif altitude < 275 then
  66.     return 1 - altitude / 550
  67.   else
  68.     return 0.5 * math.pow(0.01, (altitude - 275) / (1200 - 275));
  69.   end
  70. end
  71.  
  72. function setMass(m)
  73.   if m.thumper then
  74.     m.mass = 0.2 * (m.length + 1)
  75.   else
  76.     m.mass = 0.2 * m.length
  77.   end
  78. end
  79.  
  80. function setThrust(m)
  81.   m.thrust = m.displayedThrust / 40
  82. end
  83.  
  84. function setInitialSpeed(m)
  85.   m.initialSpeed = 0.75 / m.mass + m.numberOfEjectors * 70 + m.numberOfTorpedoPropellers * 50 + m.numberOfThrusters * 15
  86. end
  87.  
  88. function setDragCoefficient(m)
  89.   local i = 1
  90.   local a = 0.1
  91.   if m.thumper or m.singlePixelSeeker then
  92.     i = i + 1
  93.   else
  94.     if m.seekerHead then
  95.       a = a + 0.05 / i
  96.       i = i + 1
  97.     end
  98.   end
  99.   for j = 1, m.numberOfLowDragComponents do
  100.     a = a + 0.01 / i
  101.     i = i + 1
  102.   end
  103.   while i <= m.length * 2 - m.numberOfHighDragComponents do
  104.     a = a + 0.02 / i
  105.     i = i + 1
  106.   end
  107.   for j = 1, m.numberOfHighDragComponents do
  108.     a = a + 0.05 / i
  109.     i = i + 1
  110.   end
  111.   a = a / 100
  112.   m.dragCoefficient = a
  113. end
  114.  
  115. function setEffectiveTime(m)
  116.   local fuelTime = m.numberOfFuelTanks * 5000 / m.displayedThrust;
  117.   local regulatorTime = m.numberOfRegulators * 180 + 45;
  118.   m.effectiveTime = math.min(fuelTime, regulatorTime);
  119. end
  120.  
  121.  
  122. setMass(missileComposition)
  123. setThrust(missileComposition)
  124. setInitialSpeed(missileComposition)
  125. setDragCoefficient(missileComposition)
  126. setEffectiveTime(missileComposition)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement