local k = -math.log(0.05) / 100000 -- decay factor for workspace.AirDensity, roblox docs say that the density is 5% of the AirDensity property at 100k studs soooo
local Cd0 = 0.03 -- in the range of general aviation aircraft, good enough
function gToKgPerM3(r)
return r * 1000
end
-- get air density at Y value, also converts to kg/m^3 so it can be used in lift calc
function AirDensity(y)
local v = workspace.AirDensity -- measured in g/cm^3
local x = (y < 0 and v) or v * math.exp(k * y)
return gToKgPerM3(x)
end
local module = {}
function stom(s)
return s * 0.28
end
function mtos(m)
return m * 3.57
end
-- Calculate the lift coefficient. This is automatically used in BoringMath.Lift so calling this from your code is not needed.
function module.LiftCoefficient(AngleOfAttack)
return 2 * math.pi * AngleOfAttack
end
-- Calculates lift force in newtons.
function module.Lift(Altitude, Speed, Area, AngleOfAttack)
Speed = stom(Speed)
local r = AirDensity(Altitude)
local Cl = module.LiftCoefficient(AngleOfAttack)
return .5 * Cl * r * Speed^2 * Area
end
-- Calculate the drag coefficient. This is automatically used in BoringMath.Drag so calling this from your code is not needed.
function module.DragCoefficient(Cl, Ar, e)
e = e or 0.7
return Cd0 + Cl^2 / (math.pi * Ar * e)
end
-- Calculates drag force in newtons.
function module.Drag(Altitude, Speed, Area, AngleOfAttack, Ar, e)
local r = AirDensity(Altitude)
local Cd = module.DragCoefficient(module.LiftCoefficient(AngleOfAttack), Ar, e or 0.7)
return .5 * Cd * r * Speed^2 * Area
end
-- Converts a force in newtons (i.e the output of BoringMath.Drag and BoringMath.Lift) to an impulse in studs. Takes in the force in newtons and the AssemblyMass of the object.
function module.ForceToImpulse(forceN, massRMU)
local kgPerRMU = 21.952
local metersPerU = 0.28
local massKg = massRMU * kgPerRMU
local velocityChange = forceN / massKg
local distanceMeters = velocityChange
local distanceU = distanceMeters / metersPerU
return distanceU
end
return module