예제 #1
0
        /// <summary>
        /// Get orbit curve points without array allocation, if current orbit state is valid.
        /// </summary>
        /// <remarks>
        /// Note: array allocation may sometimes occur, if specified array is null or lenght is not equal to target points count.
        /// </remarks>
        /// <param name="points">Resulting orbit curve array.</param>
        /// <param name="pointsCount">Max orbit curve points count.</param>
        /// <param name="origin">World position of attractor (focus of orbit).</param>
        /// <param name="maxDistance">Max distance for orbit curve points.</param>
        public void GetOrbitPointsNoAlloc(ref Vector3d[] points, int pointsCount, Vector3d origin, double maxDistance = 1000d)
        {
            if (pointsCount < 2)
            {
                points = new Vector3d[0];
                return;
            }
            if (Eccentricity < 1)
            {
                if (points == null || points.Length != pointsCount)
                {
                    points = new Vector3d[pointsCount];
                }

                if (ApoapsisDistance < maxDistance)
                {
                    for (var i = 0; i < pointsCount; i++)
                    {
                        points[i] = GetFocalPositionAtEccentricAnomaly(i * Mathd.PI_2 / (pointsCount - 1d)) + origin;
                    }
                }
                else
                {
                    var maxAngle = CelestialBodyUtils.CalcTrueAnomalyForDistance(maxDistance, Eccentricity, SemiMajorAxis);
                    for (int i = 0; i < pointsCount; i++)
                    {
                        points[i] = GetFocalPositionAtTrueAnomaly(-maxAngle + i * 2d * maxAngle / (pointsCount - 1)) + origin;
                    }
                }
            }
            else
            {
                if (maxDistance < PeriapsisDistance)
                {
                    points = new Vector3d[0];
                    return;
                }

                if (points == null || points.Length != pointsCount)
                {
                    points = new Vector3d[pointsCount];
                }

                var maxAngle = CelestialBodyUtils.CalcTrueAnomalyForDistance(maxDistance, Eccentricity, SemiMajorAxis);
                for (int i = 0; i < pointsCount; i++)
                {
                    points[i] = GetFocalPositionAtTrueAnomaly(-maxAngle + i * 2d * maxAngle / (pointsCount - 1)) + origin;
                }
            }
        }
예제 #2
0
        public Vector3[] GetOrbitPoints(int pointsCount, Vector3 origin, float maxDistance = 1000f)
        {
            if (pointsCount < 2)
            {
                return(new Vector3[0]);
            }
            var result = new Vector3[pointsCount];

            if (eccentricity < 1)
            {
                if (apoapsisDistance < maxDistance)
                {
                    for (var i = 0; i < pointsCount; i++)
                    {
                        result[i] = (Vector3)GetFocalPositionAtEccentricAnomaly(i * Mathd.PI_2 / (pointsCount - 1d)) + origin;
                    }
                }
                else
                {
                    var maxAngle = CelestialBodyUtils.CalcTrueAnomalyForDistance(maxDistance, eccentricity, semiMajorAxis);
                    for (int i = 0; i < pointsCount; i++)
                    {
                        result[i] = (Vector3)GetFocalPositionAtTrueAnomaly(-maxAngle + i * 2d * maxAngle / (pointsCount - 1)) + origin;
                    }
                }
            }
            else
            {
                if (maxDistance < periapsisDistance)
                {
                    return(new Vector3[0]);
                }
                var maxAngle = CelestialBodyUtils.CalcTrueAnomalyForDistance(maxDistance, eccentricity, semiMajorAxis);

                for (int i = 0; i < pointsCount; i++)
                {
                    result[i] = (Vector3)GetFocalPositionAtTrueAnomaly(-maxAngle + i * 2d * maxAngle / (pointsCount - 1)) + origin;
                }
            }
            return(result);
        }