Assets/Scripts/OrbitUpdater.cs
/// <summary>
/// True anomaly using Newton-Raphson iteration
/// </summary>
/// <param name="orbit"></param>
/// <param name="dateTime"></param>
/// <returns></returns>
True anomaly using Newton-Raphson iteration
True anomaly using Newton - Raphson iteration
public float CalculateTrueAnomaly(DateTime dateTime)
    {
        const float epsilon = 0.000001f;
        float trueAnomoly;
        float meanAnomoly = CalculateMeanAnomaly(dateTime);
        float eccentricAnomoly, oldEccentricAnomoly, newEccentricAnomoly = meanAnomoly + (CurrentEccentricity / 2);
        int iterations = 0;
        do
        {
            iterations++;
            oldEccentricAnomoly = newEccentricAnomoly;
            newEccentricAnomoly = oldEccentricAnomoly - (oldEccentricAnomoly - (CurrentEccentricity * Mathf.Sin(oldEccentricAnomoly)) - meanAnomoly) / (1.0f - (CurrentEccentricity * Mathf.Cos(oldEccentricAnomoly)));
        }
        while (Mathf.Abs(oldEccentricAnomoly - newEccentricAnomoly) > epsilon && iterations < MaxIterations);
        if (iterations == MaxIterations)
        {
            trueAnomoly = TrueAnomaly;
        }
        else
        {
            eccentricAnomoly = newEccentricAnomoly;
            float cosEccentricAnomoly = Mathf.Cos(eccentricAnomoly);
            trueAnomoly = Mathf.Acos((cosEccentricAnomoly - CurrentEccentricity) / (1.0f - (CurrentEccentricity * cosEccentricAnomoly)));
            if (meanAnomoly > Mathf.PI)
            {
                trueAnomoly = (Mathf.PI * 2.0f) - trueAnomoly;
            }
            if (float.IsNaN(trueAnomoly))
            {
                trueAnomoly = TrueAnomaly;
            }
        }
        return trueAnomoly;
    }
public float CalculateTrueAnomaly ( DateTime dateTime ) { const float epsilon = 0.000001f ; float trueAnomoly ; float meanAnomoly = CalculateMeanAnomaly ( dateTime ) ; float eccentricAnomoly , oldEccentricAnomoly , newEccentricAnomoly = meanAnomoly + ( CurrentEccentricity / 2 ) ; int iterations = 0 ; do { iterations ++ ; oldEccentricAnomoly = newEccentricAnomoly ; newEccentricAnomoly = oldEccentricAnomoly - ( oldEccentricAnomoly - ( CurrentEccentricity * Mathf . Sin ( oldEccentricAnomoly ) ) - meanAnomoly ) / ( 1.0f - ( CurrentEccentricity * Mathf . Cos ( oldEccentricAnomoly ) ) ) ; } while ( Mathf . Abs ( oldEccentricAnomoly - newEccentricAnomoly ) > epsilon && iterations < MaxIterations ) ; if ( iterations == MaxIterations ) { trueAnomoly = TrueAnomaly ; } else { eccentricAnomoly = newEccentricAnomoly ; float cosEccentricAnomoly = Mathf . Cos ( eccentricAnomoly ) ; trueAnomoly = Mathf . Acos ( ( cosEccentricAnomoly - CurrentEccentricity ) / ( 1.0f - ( CurrentEccentricity * cosEccentricAnomoly ) ) ) ; if ( meanAnomoly > Mathf . PI ) { trueAnomoly = ( Mathf . PI * 2.0f ) - trueAnomoly ; } if ( float . IsNaN ( trueAnomoly ) ) { trueAnomoly = TrueAnomaly ; } } return trueAnomoly ; }
True anomaly using Newton-Raphson iteration
True anomaly using Newton - Raphson iteration
// Mean Anomoly // Eccentric Anomaly // Solve [ 0 = E - e sin(E) - M ] for E using Newton-Raphson: Xn+1 = Xn - [ f(Xn) / f'(Xn) ] // E = Eccentric Anomaly, M = Mean Anomaly // Iteractions // Solve cos(bodyAngle) = ( cos(E) - e ) / (1 - e cos(E) ) to get body angle with sun // E = Eccentric Anomaly, e = Eccentricity // Arccos returns value between 0 and Pi, but when Mean Anomoly > Pi (ie past halfway point) take (TwoPi - angle) to get solution between Pi and TwoPi // Fail
None
dateTime
DateTime
None
orbit
None