Live data from Hacker News

Translating a Fortran F-16 Simulator to Unity3D

vazgriz.com

121–129 of 129 posts

Re: Translating a Fortran F-16 Simulator to Unity3D

#121

Awesome achievement but I have to say the units stressed me out. I hope that in the real world there's some kind of dimensional analysis code linter that verifies no one is comparing slugs to feet or something, and that altitude doesn't go below zero.

I've seen a few libraries that attempt to put strong unit types into languages, to use the type system to ensure correctness.

In rust I'm familiar with https://docs.rs/uom/latest/uom/ and typescript I've seen safe-units https://jscheiny.github.io/safe-units/ used.

Re: Translating a Fortran F-16 Simulator to Unity3D

#122

The fact that this person put a snarky (???) next to the concept of measuring airspeed in knots makes me wonder just how familiar they are with aerospace in general. A knot is a standard measurement in part because one nautical mile is equal to one minute of latitude. Which is useful until the day we decide to start measuring lat/long in radians. I know it's fashionable in some circles to dump on everything non-metri…

As someone who works in flight simulation I was also taken back hard by this. We are internally metric all the way, but even so the knot is not a weird unit in this space at all. I also never heard about those coordinate conventions.

Re: Translating a Fortran F-16 Simulator to Unity3D

#123

Awesome read except I have one nit. Instead of writing translating functions to convert everything from US Customary units to Unity, Metric, and the rest of sanity, you should have converted everything to metric in the formulas. Rewritten the math. It's not that hard, it's not that difficult, saves you all those function calls, improves performance, and simplifies the math a whole bunch. Air density can become pliabl…

Hi, author here.

I wanted to keep the data tables exactly the same in the C# translation. While formulas can be converted to metric easily, the data tables do not actually have units defined for any of them, so translating them to metric is non-trivial. And the way data is mixed from multiple tables is very complex. So verifying that the calculations have equivalent results in metric is well beyond my ability.

Or rather, the best way to verify it would be to write a flight model that uses customary units (what I describe in the article), and then use that to verify that the metric flight model is equivalent.

At the end of the day, only the inputs and outputs of the flight model need to be converted, which is just handful of multiplication operations. The flight model is very cheap to run, even with conversions at runtime.

Re: Translating a Fortran F-16 Simulator to Unity3D

#124

Awesome achievement but I have to say the units stressed me out. I hope that in the real world there's some kind of dimensional analysis code linter that verifies no one is comparing slugs to feet or something, and that altitude doesn't go below zero.

Hi, author here.

In my experience in both the aerospace industry and the video game industry, there are no tools like this in use. In aerospace specifically, errors like that are caught by manual human review, including third-party validation companies. Unit tests are used to sanity check every calculation. And finally, everything is run in a flight simulator before ever going onto a real aircraft.

In video games however, it's the wild west. Math errors there are funny, not deadly.

Re: Translating a Fortran F-16 Simulator to Unity3D

#125

The fact that this person put a snarky (???) next to the concept of measuring airspeed in knots makes me wonder just how familiar they are with aerospace in general. A knot is a standard measurement in part because one nautical mile is equal to one minute of latitude. Which is useful until the day we decide to start measuring lat/long in radians. I know it's fashionable in some circles to dump on everything non-metri…

Hi author here, I have worked in the aerospace industry on flight control systems. I'm very familiar with knots as a unit.

I'm just annoyed that so many educational resources and even flight code still use customary units for everything. I believe that metric should be used for all internal calculations and knots should only exist at the UI layer for the pilot.

When I play flight games though, I only have intuition for speed in terms of knots. Like 150 kts is takeoff speed and 400 kts is the corner speed.

I just think that the pilot letting out a spool of knotted rope from their plane is a very silly practice to defend :)

Re: Translating a Fortran F-16 Simulator to Unity3D

#126

The fact that this person put a snarky (???) next to the concept of measuring airspeed in knots makes me wonder just how familiar they are with aerospace in general. A knot is a standard measurement in part because one nautical mile is equal to one minute of latitude. Which is useful until the day we decide to start measuring lat/long in radians. I know it's fashionable in some circles to dump on everything non-metri…

Hi author here, I have worked in the aerospace industry on flight control systems. I'm very familiar with knots as a unit. I'm just annoyed that so many educational resources and even flight code still use customary units for everything. I believe that metric should be used for all internal calculations and knots should only exist at the UI layer for the pilot. When I play flight games though, I only have intuition f…

I learnt to fly in metric, every time I see feet and feet-per-minute in sims I'm confused as hell, and you can't ask virtual ATC to switch to metric.

At least nautical miles and knots have some use on this planet specifically...

Re: Translating a Fortran F-16 Simulator to Unity3D

#127
post #99

Earlier quoted context omitted.

> airspeed is generally measured in either knots or Mach boatspeed is also measure in knots... but only very rarely in Mach (and not even primarily because the speed of sound in water is substantially higher than in air)

Historically, I'm fairly certain airspeed was measured in knots because of boatspeed. In software terms, the problem of navigating over the Earth's surface was forked from boats to planes, and why re-invent the wheel? It also tends to be more of a standard in professional aviation, whereas some small civilian bugsmashers in the US use MPH, presumably because it's easier for the doctors and lawyers of the world to und…

US customary is prevalent because USA after WW2 was the China of aviation - market got flooded by aircraft that had us customary units.

Nautical miles and knots were present in long range navigation, especially oceanic

Re: Translating a Fortran F-16 Simulator to Unity3D

#128
post #64

When I was growing up computers were getting faster by leaps and bounds, and I remember a video game that was so 'sophisticated' that it needed a math (FP) co-processor (80387) to play some parts of it: > The program requires a minimum of a 12MHz 80286, 1MB RAM, DOS 5.0 or DR DOS, one 1.2MB 5 1⁄4" or 1.44MB 3 1⁄2" disk drive, hard drive with 11MB of free disk space, and VGA graphics. In addition, Falcon3.O supports a…

I have fond memories of playing the original Falcon on my Amiga 500. It felt like magic after years of playing F15 Strike Eagle on the Apple IIc. Hearing real sound effects kicking in the afterburner and getting too close to the ground ("Pull Up! Pull Up!") were all so satisfying. I remember being so excited when Falcon 3.0 came out. But it just felt like a let down. The graphics were amazing for the time and it seem…

That and F-18 as well.

I think that magic is now gone, back then playing games was a bit like reading a book, we had to make use of our imagination to compensate for the lousy graphics, especially when being able to visit arcades.

Then suddenly having computers at home with similar arcade like graphics felt like the future.

Now we get real time rendering without useful gameplay, in many AAA games.

Re: Translating a Fortran F-16 Simulator to Unity3D

#129

Awesome read except I have one nit. Instead of writing translating functions to convert everything from US Customary units to Unity, Metric, and the rest of sanity, you should have converted everything to metric in the formulas. Rewritten the math. It's not that hard, it's not that difficult, saves you all those function calls, improves performance, and simplifies the math a whole bunch. Air density can become pliabl…

Hi, author here. I wanted to keep the data tables exactly the same in the C# translation. While formulas can be converted to metric easily, the data tables do not actually have units defined for any of them, so translating them to metric is non-trivial. And the way data is mixed from multiple tables is very complex. So verifying that the calculations have equivalent results in metric is well beyond my ability. Or rat…

This is exactly what unit tests are for. You did the first (hard part), dissecting the Fortran ark for goodies. You built a Unity equivalent (slightly less hard than deciphering ancient text but still high on difficulty). Now, write tests that test those inputs and outputs (translation calls included). Great, now that all the tests pass, change to metric until the tests pass again.

I know it sounds daunting. Making a painting does too to someone who doesn’t paint. However, there are steps you can follow that make it super easy to create masterpieces and all the greats follow the same process.

So where you translate ft to m or slug/ft to slug/m or however - the surrounding math is perfect for a unit test. Keeping you from having to build another flight model and do analog mark-2 eyeball testing.

You could just leave it - someone will pick it up and do the work.

*EDIT* I ran some of it through my models and converted AirDataComputer to Kelvin, kg/m, m/s:

    public class AirDataComputer {
    /// 
    /// Density at sea level in kg/m³ (standard atmosphere)
    /// 
    public const float SeaLevelDensity = 1.225f;

    /// 
    /// Max altitude in meters
    /// 
    public const float MaxAltitude = 10668.0f; // ~35,000 ft in meters

    /// 
    /// Calculates air data based on velocity and altitude using SI units
    /// 
    /// Velocity in m/s
    /// Altitude in meters
    /// Air data
    public AirData CalculateAirData(float velocity, float altitude) {
        const float baseTemperature = 288.15f;        // Sea level temp in K (~15°C)
        const float minTemperature = 216.65f;         // Tropopause temp in K (~ -56.5°C)
        const float temperatureGradient = 0.0065f;    // Lapse rate in K/m

        const float gamma = 1.4f;                     // Ratio of specific heats
        const float gasConstant = 287.05f;            // J/(kg·K), specific gas constant for air
        const float densityPower = 4.14f;

        altitude = Mathf.Clamp(altitude, 0, MaxAltitude);

        // Calculate temperature in Kelvin using linear lapse rate model
        float temperatureFactor = 1.0f - (temperatureGradient * altitude / baseTemperature);
        float T = Mathf.Max(minTemperature, baseTemperature * temperatureFactor);

        // Speed of sound in m/s
        float speedOfSound = Mathf.Sqrt(gamma * gasConstant * T);
        float altitudeMach = velocity / speedOfSound;

        // Air density using barometric approximation
        float rho = SeaLevelDensity * Mathf.Pow(temperatureFactor, densityPower);

        // Dynamic pressure in Pascals (N/m²)
        float qBar = 0.5f * rho * velocity * velocity;

        return new AirData() {
            altitudeMach = altitudeMach,
            qBar = qBar
        };
    }
Post reply on HN