Hi
As stated in the title I have some rare conditions where DistanceFromInNauticalMiles() returns NaN instead of a number.
I try to track the flown distance. To accomplish this, I get the current coordinates and one minute later the new coordinates, calculate the distance between both points and sum up the total distance everytime.
I simplified my code a little bit to show you what I have. The method CalcPlayerDistance() is the original method I'm using in my program. The main() method is just used to demonstrate how the logic works.
internal class Program
{
private FsLatitude previousPlayerLatitude;
private FsLongitude previousPlayerLongitude;
internal FsLatitude PlayerLatitude { get; private set; }
internal FsLongitude PlayerLongitude { get; private set; }
internal double TrackedPlayerDistance{ get; private set; }
private Offset<long> playerLatitudeOffset = new Offset<long>(0x0560);
private Offset<long> playerLongitudeOffset = new Offset<long>(0x0568);
private double CalcPlayerDistance()
{
double playerDistance;
PlayerLongitude = new FsLongitude(playerLongitudeOffset.Value);
PlayerLatitude = new FsLatitude(playerLatitudeOffset.Value);
// Initialize previous coordinates on start
if (previousPlayerLatitude.DecimalDegrees == 0 && previousPlayerLongitude.DecimalDegrees == 0 && TrackedPlayerDistance == 0)
{
previousPlayerLatitude = PlayerLatitude;
previousPlayerLongitude = PlayerLongitude;
}
FsLatLonPoint currentPosition = new FsLatLonPoint(PlayerLatitude, PlayerLongitude);
playerDistance = currentPosition.DistanceFromInNauticalMiles(new FsLatLonPoint(previousPlayerLatitude, previousPlayerLongitude));
previousPlayerLatitude = PlayerLatitude;
previousPlayerLongitude = PlayerLongitude;
return playerDistance;
}
[STAThread]
public static void Main()
{
Program distanceTest = new Program();
while(true)
{
distanceTest.TrackedPlayerDistance += distanceTest.CalcPlayerDistance();
Thread.Sleep(60000);
}
}
}
At the end I rarely end up with TrackedPlayerDistance being NaN. Unfortunately I can't reproduce it and I dont know when it happens yet since once TrackedPlayerDistance is NaN it will stay NaN.
Im using the same method to calculate the remaining distance to the destination airport:
DistanceToDestNm = (int)Math.Round(new FsLatLonPoint(PlayerLatitude, PlayerLongitude).DistanceFromInNauticalMiles(arrivalAirport));
This would not directly end up in NaN since im casting to int but instead would end up in something like -2147483648. Unlike the first case it never happened here.
Does anyone have an idea what im doing wrong in the first case and how to prevent it? If there is a better way to accomplish what I'm looking for I'm happy for hints.
Regards