Every virtual airline pilot wants to know how hard that last one was. The sim has a variable for it, PLANE TOUCHDOWN NORMAL VELOCITY, and it looks like a one line job.
It isn't quite. The variable keeps the previous landing's number until the next one. Read it at the wrong moment and you'll proudly report yesterday's greaser for today's crunch.
The rule that works
-
While airborne, remember what the touchdown variable says and what your vertical speed is.
- On the first sample where
SIM ON GROUNDturns true, compare. - If the touchdown number changed, it belongs to this landing. If it didn't, fall back to the last vertical speed you saw in the air.
The Hangar Addons Telemetry app uses the same idea.
- On the first sample where
The program
Use the project file from the SimConnect basics.
using System.Runtime.InteropServices;
using Microsoft.FlightSimulator.SimConnect;
using var ready = new EventWaitHandle(false, EventResetMode.AutoReset);
using var sim = new SimConnect("Landing logger", IntPtr.Zero, 0, ready, 0);
var running = true;
var airborne = false;
var valueInAir = 0.0;
var lastDescent = 0.0;
foreach (var (name, unit) in new[]
{
("SIM ON GROUND", "bool"), ("VERTICAL SPEED", "feet per minute"), ("PLANE TOUCHDOWN NORMAL VELOCITY", "feet per second"),
("PLANE BANK DEGREES", "degrees"), ("G FORCE", "gforce")
})
{
sim.AddToDataDefinition(Id.Landing, name, unit, SIMCONNECT_DATATYPE.FLOAT64, 0, SimConnect.SIMCONNECT_UNUSED);
}
sim.RegisterDataDefineStruct<Sample>(Id.Landing);
sim.OnRecvSimobjectData += (_, data) =>
{
var s = (Sample)data.dwData[0];
var fpm = s.Touchdown * 60;
if (s.OnGround < 0.5)
{
airborne = true;
valueInAir = fpm;
lastDescent = s.VerticalSpeed;
return;
}
if (!airborne)
{
return;
}
airborne = false;
// The sim keeps the previous landing's number until the next one, so only trust it if it changed.
var rate = Math.Abs(fpm) > 0 && Math.Abs(fpm - valueInAir) > 0.5 ? Math.Abs(fpm) : Math.Abs(lastDescent);
Console.WriteLine($"Touchdown at {rate:F0} fpm, bank {s.Bank:F1}, {s.GForce:F2} g");
};
sim.OnRecvQuit += (_, _) => running = false;
sim.RequestDataOnSimObject(Id.Landing, Id.Landing, SimConnect.SIMCONNECT_OBJECT_ID_USER, SIMCONNECT_PERIOD.SIM_FRAME, SIMCONNECT_DATA_REQUEST_FLAG.DEFAULT, 0, 0, 0);
while (running)
{
if (ready.WaitOne(1000))
{
sim.ReceiveMessage();
}
}
enum Id { Landing }
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Sample
{
public double OnGround, VerticalSpeed, Touchdown, Bank, GForce;
}
