56 lines
2.1 KiB
GDScript3
56 lines
2.1 KiB
GDScript3
|
extends Node
|
||
|
|
||
|
@export var pressure_ASL := 101.325 # 1 atm. in kPa
|
||
|
@export var density_ASL := 1.225 # at 288.15k in kg/m^3
|
||
|
@export var temperature_ASL := 288.15 # surface temperature in Kelvin (288.15K = 15C)
|
||
|
@export var tropopause_altitude := 11000.0 # meters above ASL
|
||
|
@export var tropopause_temp := 216.65 # (216.65K = -56.5C)
|
||
|
@export var gravity := 9.8 # here until I figure out the best way to get the project's default gravity.
|
||
|
|
||
|
const AIR_MOLAR_MASS := 0.029 # kg/mol
|
||
|
const GAS_CONSTANT := 8.31447
|
||
|
const MACH_COEFF := 20.02948
|
||
|
|
||
|
var pressure_constant: float
|
||
|
var density_constant: float
|
||
|
var temp_lapse_at_tropo: float # temperature lapse at troposphere, in K or C
|
||
|
|
||
|
# Use this for initialization
|
||
|
func _ready() -> void:
|
||
|
_recalculate()
|
||
|
|
||
|
func _recalculate() -> void:
|
||
|
pressure_constant = (gravity * AIR_MOLAR_MASS) / (GAS_CONSTANT * temperature_ASL)
|
||
|
density_constant = 1 / density_ASL
|
||
|
temp_lapse_at_tropo = (temperature_ASL - tropopause_temp) / tropopause_altitude
|
||
|
|
||
|
func pressure_by_alt(altitude: float, atm := false) -> float: # returns the pressure at altitude in kPa (default) or atm.
|
||
|
var ret := exp(pressure_constant * altitude)
|
||
|
if !atm:
|
||
|
ret *= pressure_ASL
|
||
|
return ret
|
||
|
|
||
|
func density_by_alt(altitude: float, relative := false) -> float: # returns the density at altitude in kg/m^3 (default), or relative.
|
||
|
var ret := pressure_by_alt(altitude) / (0.287058 * temperature_by_alt(altitude))
|
||
|
if relative:
|
||
|
ret *= density_constant
|
||
|
return ret
|
||
|
|
||
|
func temperature_by_alt(altitude: float) -> float:
|
||
|
if altitude < tropopause_altitude:
|
||
|
return temperature_ASL - altitude * temp_lapse_at_tropo
|
||
|
else: # mesopause coming soon!
|
||
|
return tropopause_temp
|
||
|
|
||
|
func speed_of_sound_by_alt(altitude: float) -> float:
|
||
|
return sqrt(temperature_by_alt(altitude)) * MACH_COEFF
|
||
|
|
||
|
func mach_by_alt(altitude: float, speed: float) -> float:# a little bit expensive - use with caution
|
||
|
return speed / speed_of_sound_by_alt(altitude)
|
||
|
|
||
|
func atm_to_kPa(pressure: float) -> float:
|
||
|
return pressure * pressure_ASL
|
||
|
|
||
|
func kPa_to_atm(pressure: float) -> float:
|
||
|
return pressure / pressure_ASL
|