Esempio n. 1
0
        public void SetParametersFromKeplerElements(KeplerElements ke, Vector4 position)
        {
            _eccentricity       = ke.Eccentricity;
            _linearEccentricity = ke.LinierEccentricity;


            OrbitEllipseSemiMaj   = ke.SemiMajorAxis;
            OrbitEllipseSemiMinor = ke.SemiMinorAxis;

            Periapsis = new PointD()
            {
                X = Math.Sin(ke.TrueAnomaly) * ke.Periapsis,
                Y = Math.Cos(ke.TrueAnomaly) * ke.Periapsis
            };
            Apoapsis = new PointD()
            {
                X = Math.Sin(ke.TrueAnomaly) * ke.Apoapsis,
                Y = Math.Cos(ke.TrueAnomaly) * ke.Apoapsis
            };

            if (ke.Inclination > Math.PI * 0.5 && ke.Inclination < Math.PI * 1.5) //ke inclination is in radians.
            {
                IsClockwiseOrbit  = false;
                OrbitAngleRadians = ke.LoAN - ke.AoP;
            }
            else
            {
                OrbitAngleRadians = ke.LoAN + ke.AoP;
            }
            _position = position;
            OnPhysicsUpdate();
        }
Esempio n. 2
0
        void Calcs()
        {
            //double x = (_radialDV * Math.Cos(_origionalAngle)) - (_progradeDV * Math.Sin(_origionalAngle));
            //double y = (_radialDV * Math.Sin(_origionalAngle)) + (_progradeDV * Math.Cos(_origionalAngle));
            _deltaV_MS = _newtonUI.DeltaV; //new ECSLib.Vector3(x, y, 0);


            _newOrbitalVelocity_m = _orbitalVelocityAtChange_m + _deltaV_MS;
            _newOrbitalSpeed_m    = _newOrbitalVelocity_m.Length();

            _newAngle = Math.Atan2(_newOrbitalVelocity_m.X, _newOrbitalVelocity_m.Y);


            _ke_m = OrbitMath.KeplerFromPositionAndVelocity(_stdGravParam_m, _positonAtChange_m, _newOrbitalVelocity_m, _actionDateTime);


            _orbitWidget.SetParametersFromKeplerElements(_ke_m, _positonAtChange_m);

            /*
             * var sgpCBAU = GameConstants.Science.GravitationalConstant * (_massCurrentBody + _massOrderingEntity) / 3.347928976e33;// (149597870700 * 149597870700 * 149597870700);
             * var ralPosCBAU = OrderingEntity.Entity.GetDataBlob<PositionDB>().RelativePosition_AU;
             * var smaCurrOrbtAU = OrderingEntity.Entity.GetDataBlob<OrbitDB>().SemiMajorAxis;
             * var velAU = OrbitProcessor.PreciseOrbitalVector(sgpCBAU, ralPosCBAU, smaCurrOrbtAU);
             */
        }
Esempio n. 3
0
        public override void OnSystemTickChange(DateTime newDate)
        {
            _minDateTime = newDate;
            if (_atDatetime < _minDateTime)
            {
                _atDatetime = _minDateTime;
            }
            if (_orderEntity.HasDataBlob <OrbitDB>())
            {
                _currentKE = _orderEntity.GetDataBlob <OrbitDB>().GetElements();
            }
            else if (_orderEntity.HasDataBlob <OrbitUpdateOftenDB>())
            {
                _currentKE = _orderEntity.GetDataBlob <OrbitUpdateOftenDB>().GetElements();
            }
            else if (_orderEntity.HasDataBlob <OrbitDB>())
            {
                _currentKE = _orderEntity.GetDataBlob <NewtonMoveDB>().GetElements();
            }

            if (_targetSMA == 0)
            {
                _targetSMA = (float)_currentKE.SemiMajorAxis;
            }
        }
Esempio n. 4
0
        public void SetParametersFromKeplerElements(KeplerElements ke_m, Vector3 position_m)
        {
            _eccentricity         = ke_m.Eccentricity;
            _linearEccentricity_m = ke_m.LinierEccentricity;
            _position_m           = position_m;

            OrbitEllipseSemiMaj_m   = ke_m.SemiMajorAxis;
            OrbitEllipseSemiMinor_m = ke_m.SemiMinorAxis;

            //TODO: Periapsis and Apoapsis calc doesn't look right to me... though it's not currently being used.
            //this was probibly written when the orbit could only be created when the ship was at the pere or apo.

            /*
             * Periapsis = new PointD()
             * {
             *  Y = Math.Sin(ke.TrueAnomaly) * ke.Periapsis,
             *  X = Math.Cos(ke.TrueAnomaly) * ke.Periapsis
             *
             * };
             * Apoapsis = new PointD() {
             *  Y = Math.Sin(ke.TrueAnomaly) * ke.Apoapsis,
             *  X = Math.Cos(ke.TrueAnomaly) * ke.Apoapsis
             * };
             */
            if (ke_m.Inclination > Math.PI * 0.5 && ke_m.Inclination < Math.PI * 1.5) //ke inclination is in radians.
            {
                IsRetrogradeOrbit = true;
            }
            else
            {
                IsRetrogradeOrbit = false;
            }



            LonditudeOfPeriapsis = ke_m.LoAN + ke_m.AoP;
            _loAN        = ke_m.LoAN;
            _aoP         = ke_m.AoP;
            _trueAnomaly = ke_m.TrueAnomalyAtEpoch;
            OnPhysicsUpdate();
        }
Esempio n. 5
0
        internal override void Display()
        {
            if (IsActive)
            {
                var size = new Vector2(200, 100);
                var pos  = new Vector2(_uiState.MainWinSize.X / 2 - size.X / 2, _uiState.MainWinSize.Y / 2 - size.Y / 2);

                ImGui.SetNextWindowSize(size, ImGuiCond.FirstUseEver);
                ImGui.SetNextWindowPos(pos, ImGuiCond.FirstUseEver);

                if (ImGui.Begin(_displayText, ref IsActive, _flags))
                {
                    //put calcs that needs refreshing each frame in here. (ie calculations from mouse cursor position)
                    if (_orbitWidget != null)
                    {
                        switch (CurrentState)
                        {
                        case States.NeedsEntity:

                            break;

                        case States.NeedsTarget:
                        {
                        }

                        break;

                        case States.NeedsInsertionPoint:
                        {
                            if (_newtonUI != null)
                            {
                                if (_newtonUI.Display())
                                {
                                    InsertionCalcs();
                                }
                            }

                            var mousePos = ImGui.GetMousePos();

                            var mouseWorldPos = _uiState.Camera.MouseWorldCoordinate_m();
                            _targetInsertionPoint_m = (mouseWorldPos - GetTargetPosition());         //ralitive to the target body

                            _moveWidget.SetArrivalPosition(_targetInsertionPoint_m);

                            //var velAU = OrbitProcessor.PreciseOrbitalVector(sgpCBAU, ralPosCBAU, smaCurrOrbtAU);


                            _ke_m = OrbitMath.KeplerFromPositionAndVelocity(_stdGravParamTargetBody_m, _targetInsertionPoint_m, _insertionOrbitalVelocity_m, _departureDateTime);


                            _orbitWidget.SetParametersFromKeplerElements(_ke_m, _targetInsertionPoint_m);
                            _apoapsis_m  = _ke_m.Apoapsis;
                            _periapsis_m = _ke_m.Periapsis;
                            Eccentricity = _ke_m.Eccentricity;
                            break;
                        }

                        case States.NeedsActioning:
                        {
                            if (_newtonUI != null)
                            {
                                if (_newtonUI.Display())
                                {
                                    InsertionCalcs();
                                }
                            }
                            _ke_m = OrbitMath.KeplerFromPositionAndVelocity(_stdGravParamTargetBody_m, _targetInsertionPoint_m, _insertionOrbitalVelocity_m, _departureDateTime);

                            _orbitWidget.SetParametersFromKeplerElements(_ke_m, _targetInsertionPoint_m);
                            _apoapsis_m  = _ke_m.Apoapsis;
                            _periapsis_m = _ke_m.Periapsis;
                            Eccentricity = _ke_m.Eccentricity;
                            break;
                        }

                        default:
                            break;
                        }
                    }


                    ImGui.SetTooltip(_tooltipText);
                    ImGui.Text("Target: ");
                    if (TargetEntity != null)
                    {
                        ImGui.SameLine();
                        ImGui.Text(TargetEntity.Name);
                    }

                    //ImGui.Text("Eccentricity: " + _eccentricity.ToString("g3"));

                    if (ImGui.CollapsingHeader("Orbit Data"))
                    {
                        ImGui.Text("Apoapsis: ");
                        ImGui.SameLine();
                        ImGui.Text(Stringify.Distance(_apoapsis_m) + " (Alt: " + Stringify.Distance(_apAlt) + ")");

                        ImGui.Text("Periapsis: ");
                        ImGui.SameLine();
                        ImGui.Text(Stringify.Distance(_periapsis_m) + " (Alt: " + Stringify.Distance(_peAlt) + ")");

                        ImGui.Text("DepartureSpeed: ");
                        //ImGui.SameLine();
                        ImGui.Text(Stringify.Distance(_departureOrbitalSpeed_m) + "/s");

                        ImGui.Text("InsertionSpeed: ");
                        //ImGui.SameLine();
                        ImGui.Text(Stringify.Distance(_insertionOrbitalSpeed_m) + "/s");



                        ImGui.Text("Departure Vector: ");
                        //ImGui.SameLine();
                        ImGui.Text("X: " + Stringify.Distance(_departureOrbitalVelocity_m.X) + "/s");
                        ImGui.Text("Y: " + Stringify.Distance(_departureOrbitalVelocity_m.Y) + "/s");
                        ImGui.Text("Z: " + Stringify.Distance(_departureOrbitalVelocity_m.Z) + "/s");


                        ImGui.Text("Departure Angle: ");
                        ImGui.SameLine();
                        ImGui.Text(_departureAngle.ToString("g3") + " radians or " + Angle.ToDegrees(_departureAngle).ToString("F") + " deg ");

                        /*
                         * var pc = OrbitProcessor.InstantaneousOrbitalVelocityPolarCoordinate(_orderEntityOrbit, _departureDateTime);
                         *
                         * ImGui.Text("Departure Polar Coordinates: ");
                         * ImGui.Text(pc.Item1.ToString() + " AU or " + Distance.AuToMt(pc.Item1).ToString("F") + " m/s");
                         * ImGui.Text(pc.Item2.ToString("g3") + " radians or " + Angle.ToDegrees(pc.Item2).ToString("F") + " deg ");
                         * ;
                         */

                        ImGui.Text("Insertion Vector: ");
                        ImGui.Text("X: " + Stringify.Distance(_insertionOrbitalVelocity_m.X) + "/s");
                        ImGui.Text("Y: " + Stringify.Distance(_insertionOrbitalVelocity_m.Y) + "/s");
                        ImGui.Text("Z: " + Stringify.Distance(_insertionOrbitalVelocity_m.Z) + "/s");

                        ImGui.Text("Insertion Position: ");
                        ImGui.Text("X: " + Stringify.Distance(_targetInsertionPoint_m.X));
                        ImGui.Text("Y: " + Stringify.Distance(_targetInsertionPoint_m.Y));
                        ImGui.Text("Z: " + Stringify.Distance(_targetInsertionPoint_m.Z));

                        ImGui.Text("LoAN: ");
                        ImGui.SameLine();
                        ImGui.Text(_ke_m.LoAN.ToString("g3"));

                        ImGui.Text("AoP: ");
                        ImGui.SameLine();
                        ImGui.Text(_ke_m.AoP.ToString("g3"));

                        ImGui.Text("LoP Angle: ");
                        ImGui.SameLine();
                        ImGui.Text((_ke_m.LoAN + _ke_m.AoP).ToString("g3") + " radians or " + Angle.ToDegrees(_ke_m.LoAN + _ke_m.AoP).ToString("F") + " deg ");

                        if (_orbitWidget != null)
                        {
                            ImGui.Text("Is Retrograde " + _orbitWidget.IsRetrogradeOrbit.ToString());
                        }
                    }

                    //if (CurrentState != States.NeedsActioning) //use alpha on the button if it's not useable.
                    //ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * 0.5f);
                    if (ImGui.Button("Action Order") && CurrentState == States.NeedsActioning) //only do suff if clicked if it's usable.
                    {
                        fsm[(byte)CurrentState, (byte)Events.ClickedAction].Invoke();
                        //ImGui.PopStyleVar();
                    }

                    if (_smMode)
                    {
                        ImGui.SameLine();
                        if (ImGui.Button("Add OrbitDB"))
                        {
                            ActionAddDB();
                        }
                    }

                    ImGui.End();
                }
            }
        }
Esempio n. 6
0
        public void TestKeplerElementsFromVectors()
        {
            Vector3 position = new Vector3()
            {
                X = Distance.KmToAU(405400)
            };                                                                //moon at apoapsis
            Vector3 velocity = new Vector3()
            {
                Y = Distance.KmToAU(0.97)
            };                                                              //approx velocity of moon at apoapsis
            double         parentMass = 5.97237e24;
            double         objMass    = 7.342e22;
            double         sgp        = OrbitMath.CalculateStandardGravityParameter(parentMass, objMass);
            KeplerElements elements   = OrbitMath.KeplerFromPositionAndVelocity(sgp, position, velocity, new DateTime());

            Vector3 postionKm = new Vector3()
            {
                X = 405400
            };
            Vector3 velocityKm = new Vector3()
            {
                Y = 0.97
            };
            double sgpKm = OrbitMath.CalculateStandardGravityParameterInM3S2(parentMass, objMass);

            KeplerElements elementsKm = OrbitMath.KeplerFromPositionAndVelocity(sgpKm, postionKm, velocityKm, new DateTime());

            //check that the function is unit agnostic.
            Assert.AreEqual(Distance.AuToKm(elements.SemiMajorAxis), elementsKm.SemiMajorAxis, 0.001);
            Assert.AreEqual(elements.Eccentricity, elementsKm.Eccentricity, 1.0e-9); //this is where inacuarcy from units stars creaping in, not much can do about that.
            Assert.AreEqual(Distance.AuToKm(elements.Apoapsis), elementsKm.Apoapsis, 0.001);
            Assert.AreEqual(Distance.AuToKm(elements.Periapsis), elementsKm.Periapsis, 0.001);

            //var ta = OrbitMath.TrueAnomalyFromEccentricAnomaly(elements.Eccentricity, elements.)
            var speedAU = OrbitMath.InstantaneousOrbitalSpeed(sgp, position.Length(), elements.SemiMajorAxis);

            //var speedVectorAU = OrbitMath.PreciseOrbitalVelocityVector(sgp, position, elements.SemiMajorAxis, elements.Eccentricity, elements.LoAN + elements.AoP);
            //Assert.AreEqual(speedAU, speedVectorAU.Length());

            Assert.AreEqual(elementsKm.Apoapsis + elementsKm.Periapsis, elementsKm.SemiMajorAxis * 2, 0.001);


            var speedKm  = velocityKm.Length();
            var speedKm2 = OrbitMath.InstantaneousOrbitalSpeed(sgpKm, postionKm.Length(), elementsKm.SemiMajorAxis);

            Assert.AreEqual(speedKm, speedKm2, 0.001);


            Assert.GreaterOrEqual(elements.Apoapsis, elements.Periapsis);
            Assert.GreaterOrEqual(elementsKm.Apoapsis, elementsKm.Periapsis);

            //below was some experimentation with different ways of calculating things, and an attempt to use decimal for Eccentricity.
            //not sure it's worth the minor slowdown or complication, didn't seem to fix the problem I was seeing in anycase.
            #region experimentation

            var H   = Vector3.Cross(postionKm, velocityKm).Length();
            var p   = H * H / sgpKm;
            var sma = 1 / (2 / postionKm.Length() - velocityKm.Length() * velocityKm.Length() / sgpKm); //  semi-major axis


            decimal E;
            double  Periapsis;
            double  Apoapsis;

            if (sma < (double)decimal.MaxValue)
            {
                decimal smad = (decimal)sma;
                E = GMath.Sqrt(1 - (decimal)p / smad);

                decimal PlusMinus = smad * E;
                Periapsis = (double)(smad - PlusMinus);
                Apoapsis  = (double)(smad + PlusMinus);
            }
            else
            {
                E = (decimal)Math.Sqrt(1 - p / sma);  // eccentricity

                double PlusMinus = sma * (double)E;
                Periapsis = sma - PlusMinus;
                Apoapsis  = sma + PlusMinus;
            }
            Assert.AreEqual(Periapsis + Apoapsis, sma * 2, 0.0001);
            var peStr = Periapsis.ToString("R");
            var apStr = Apoapsis.ToString("R");
            //Assert.AreEqual(elementsKm.SemiMajorAxis, sma);
            var difference1 = (Periapsis + Apoapsis) - sma * 2;
            var difference2 = (elementsKm.Apoapsis + elementsKm.Periapsis) - elementsKm.SemiMajorAxis * 2;

            #endregion

            if (velocity.Z == 0)
            {
                Assert.IsTrue(elements.Inclination == 0);
            }
        }
Esempio n. 7
0
        public void TestOrbitDBFromVectors(double parentMass, double objMass, Vector3 position, Vector3 velocity)
        {
            double         angleΔ = 0.0000000001;
            double         sgp_m  = OrbitMath.CalculateStandardGravityParameterInM3S2(objMass, parentMass);
            KeplerElements ke     = OrbitMath.KeplerFromPositionAndVelocity(sgp_m, position, velocity, new DateTime());

            Game          game = new Game();
            EntityManager man  = new EntityManager(game, false);

            BaseDataBlob[] parentblobs = new BaseDataBlob[3];
            parentblobs[0] = new PositionDB(man.ManagerGuid)
            {
                X_AU = 0, Y_AU = 0, Z_AU = 0
            };
            parentblobs[1] = new MassVolumeDB()
            {
                Mass = parentMass
            };
            parentblobs[2] = new OrbitDB();
            Entity parentEntity = new Entity(man, parentblobs);


            OrbitDB objOrbit  = OrbitDB.FromVector(parentEntity, objMass, parentMass, sgp_m, position, velocity, new DateTime());
            Vector3 resultPos = OrbitProcessor.GetPosition_AU(objOrbit, new DateTime());

            //check LoAN
            var objLoAN        = objOrbit.LongitudeOfAscendingNode;
            var keLoAN         = ke.LoAN;
            var loANDifference = objLoAN - keLoAN;

            Assert.AreEqual(keLoAN, objLoAN, angleΔ);

            //check AoP
            var objAoP     = objOrbit.ArgumentOfPeriapsis;
            var keAoP      = ke.AoP;
            var difference = objAoP - keAoP;

            Assert.AreEqual(keAoP, objAoP, angleΔ);


            //check MeanAnomalyAtEpoch
            var objM0 = objOrbit.MeanAnomalyAtEpoch;
            var keM0  = ke.MeanAnomalyAtEpoch;

            Assert.AreEqual(keM0, objM0, angleΔ);
            Assert.AreEqual(objM0, OrbitMath.GetMeanAnomalyFromTime(objM0, objOrbit.MeanMotion_DegreesSec, 0), "meanAnomalyError");

            //checkEpoch
            var objEpoch = objOrbit.Epoch;
            var keEpoch  = ke.Epoch;

            Assert.AreEqual(keEpoch, objEpoch);



            //check EccentricAnomaly:
            var objE = (OrbitProcessor.GetEccentricAnomaly(objOrbit, objOrbit.MeanAnomalyAtEpoch_Degrees));
            //var keE =   (OrbitMath.Gete(position, ke.SemiMajorAxis, ke.LinearEccentricity, ke.AoP));

            /*
             * if (objE != keE)
             * {
             *  var dif = objE - keE;
             *  Assert.AreEqual(keE, objE, angleΔ);
             * }
             */
            //check trueAnomaly
            var orbTrueAnom         = OrbitProcessor.GetTrueAnomaly(objOrbit, new DateTime());
            var orbtaDeg            = Angle.ToDegrees(orbTrueAnom);
            var differenceInRadians = orbTrueAnom - ke.TrueAnomalyAtEpoch;
            var differenceInDegrees = Angle.ToDegrees(differenceInRadians);

            if (ke.TrueAnomalyAtEpoch != orbTrueAnom)
            {
                Vector3 eccentVector = OrbitMath.EccentricityVector(sgp_m, position, velocity);
                var     tacalc1      = OrbitMath.TrueAnomaly(eccentVector, position, velocity);
                var     tacalc2      = OrbitMath.TrueAnomaly(sgp_m, position, velocity);

                var diffa = differenceInDegrees;
                var diffb = Angle.ToDegrees(orbTrueAnom - tacalc1);
                var diffc = Angle.ToDegrees(orbTrueAnom - tacalc2);

                var ketaDeg = Angle.ToDegrees(tacalc1);
            }

            Assert.AreEqual(0, Angle.DifferenceBetweenRadians(ke.TrueAnomalyAtEpoch, orbTrueAnom), angleΔ,
                            "more than " + angleΔ + " radians difference, at " + differenceInRadians + " \n " +
                            "(more than " + Angle.ToDegrees(angleΔ) + " degrees difference at " + differenceInDegrees + ")" + " \n " +
                            "ke Angle: " + ke.TrueAnomalyAtEpoch + " obj Angle: " + orbTrueAnom + " \n " +
                            "ke Angle: " + Angle.ToDegrees(ke.TrueAnomalyAtEpoch) + " obj Angle: " + Angle.ToDegrees(orbTrueAnom));

            Assert.AreEqual(ke.Eccentricity, objOrbit.Eccentricity);
            Assert.AreEqual(ke.SemiMajorAxis, objOrbit.SemiMajorAxis);


            var lenke1 = ke.SemiMajorAxis * 2;
            var lenke2 = ke.Apoapsis + ke.Periapsis;

            Assert.AreEqual(lenke1, lenke2, 1.0E-10);
            var lendb1 = objOrbit.SemiMajorAxis_AU * 2;
            var lendb2 = objOrbit.Apoapsis_AU + objOrbit.Periapsis_AU;

            Assert.AreEqual(lendb1, lendb2, 1.0E-10);
            Assert.AreEqual(lenke1, lendb1, 1.0E-10);
            Assert.AreEqual(lenke2, lendb2, 1.0E-10);



            var ke_apkm   = Distance.AuToKm(ke.Apoapsis);
            var db_apkm   = Distance.AuToKm(objOrbit.Apoapsis_AU);
            var differnce = ke_apkm - db_apkm;

            Assert.AreEqual(ke.Apoapsis, objOrbit.Apoapsis_AU, 1.0E-10);
            Assert.AreEqual(ke.Periapsis, objOrbit.Periapsis_AU, 1.0E-10);

            Vector3 posKM    = Distance.AuToKm(position);
            Vector3 resultKM = Distance.AuToKm(resultPos);

            double  keslr       = EllipseMath.SemiLatusRectum(ke.SemiMajorAxis, ke.Eccentricity);
            double  keradius    = OrbitMath.RadiusAtAngle(ke.TrueAnomalyAtEpoch, keslr, ke.Eccentricity);
            Vector3 kemathPos   = OrbitMath.GetRalitivePosition(ke.LoAN, ke.AoP, ke.Inclination, ke.TrueAnomalyAtEpoch, keradius);
            Vector3 kemathPosKM = Distance.AuToKm(kemathPos);

            Assert.AreEqual(kemathPosKM.Length(), posKM.Length(), 0.01);

            Assert.AreEqual(posKM.Length(), resultKM.Length(), 0.01, "TA: " + orbtaDeg);
            Assert.AreEqual(posKM.X, resultKM.X, 0.01, "TA: " + orbtaDeg);
            Assert.AreEqual(posKM.Y, resultKM.Y, 0.01, "TA: " + orbtaDeg);
            Assert.AreEqual(posKM.Z, resultKM.Z, 0.01, "TA: " + orbtaDeg);

            if (velocity.Z == 0)
            {
                Assert.IsTrue(ke.Inclination == 0);
                Assert.IsTrue(objOrbit.Inclination_Degrees == 0);
            }

            //var speedVectorAU = OrbitProcessor.PreciseOrbitalVector(sgp, position, ke.SemiMajorAxis);
            //var speedVectorAU2 = OrbitProcessor.PreciseOrbitalVector(objOrbit, new DateTime());
            //Assert.AreEqual(speedVectorAU, speedVectorAU2);
        }
Esempio n. 8
0
        internal override void Display()
        {
            if (IsActive)
            {
                var size = new System.Numerics.Vector2(200, 100);
                var pos  = new System.Numerics.Vector2(_state.MainWinSize.X / 2 - size.X / 2, _state.MainWinSize.Y / 2 - size.Y / 2);

                ImGui.SetNextWindowSize(size, ImGuiCond.FirstUseEver);
                ImGui.SetNextWindowPos(pos, ImGuiCond.FirstUseEver);

                if (ImGui.Begin(_displayText, ref IsActive, _flags))
                {
                    //put calcs that needs refreshing each frame in here. (ie calculations from mouse cursor position)
                    if (_orbitWidget != null)
                    {
                        switch (CurrentState)
                        {
                        case States.NeedsEntity:

                            break;

                        case States.NeedsTarget:
                        {
                        }

                        break;

                        case States.NeedsInsertionPoint:
                        {
                            var maxprogradeDV = _maxDV - Math.Abs(_radialDV);
                            var maxradialDV   = _maxDV - Math.Abs(_progradeDV);
                            if (ImGui.SliderFloat("Prograde DV", ref _progradeDV, -maxprogradeDV, maxprogradeDV))
                            {
                                InsertionCalcs();
                            }
                            if (ImGui.SliderFloat("Radial DV", ref _radialDV, -maxradialDV, maxradialDV))
                            {
                                InsertionCalcs();
                            }

                            var mousePos = ImGui.GetMousePos();

                            var mouseWorldPos = _state.Camera.MouseWorldCoordinate();
                            _targetInsertionPoint_AU = (mouseWorldPos - GetTargetPosition());         //ralitive to the target body

                            _moveWidget.SetArrivalPosition(_targetInsertionPoint_AU);

                            //var velAU = OrbitProcessor.PreciseOrbitalVector(sgpCBAU, ralPosCBAU, smaCurrOrbtAU);


                            var ke = OrbitMath.KeplerFromPositionAndVelocity(_stdGravParamTargetBody, _targetInsertionPoint_AU, _insertionOrbitalVelocity, _departureDateTime);
                            _ke = ke;

                            _orbitWidget.SetParametersFromKeplerElements(ke, _targetInsertionPoint_AU);
                            _apoapsisKm   = Distance.AuToKm(ke.Apoapsis);
                            _periapsisKM  = Distance.AuToKm(ke.Periapsis);
                            _eccentricity = ke.Eccentricity;
                            break;
                        }

                        /*
                         * case States.NeedsSecondApsis:
                         * {
                         *   TODO: when we've got newtonion engines, allow second apsis choice and expend Dv.
                         *  var mousePos = ImGui.GetMousePos();
                         *
                         *  var mouseWorldPos = _state.Camera.MouseWorldCoordinate();
                         *
                         *  var ralitivePos = (GetTargetPosition() - mouseWorldPos);
                         *  _orbitWidget.SetPeriapsis(ralitivePos.X, ralitivePos.Y);
                         *
                         *  //_periapsisKM = Distance.AuToKm((GetTargetPosition() - mouseWorldPos).Length());
                         *  var distanceSelected = Distance.AuToKm((GetTargetPosition() - mouseWorldPos).Length());
                         *  var d1 = Math.Max(_peMin, distanceSelected); //can't be lower than body radius
                         *  _periapsisKM = Math.Min(d1, _apoapsisKm);  //can't be higher than apoapsis.
                         *
                         *  break;
                         * }*/
                        case States.NeedsActioning:
                        {
                            var maxprogradeDV = _maxDV - Math.Abs(_radialDV);
                            var maxradialDV   = _maxDV - Math.Abs(_progradeDV);
                            if (ImGui.SliderFloat("Prograde DV", ref _progradeDV, -maxprogradeDV, maxprogradeDV))
                            {
                                InsertionCalcs();
                            }
                            if (ImGui.SliderFloat("Radial DV", ref _radialDV, -maxradialDV, maxradialDV))
                            {
                                InsertionCalcs();
                            }
                            var ke = OrbitMath.KeplerFromPositionAndVelocity(_stdGravParamTargetBody, _targetInsertionPoint_AU, _insertionOrbitalVelocity, _departureDateTime);
                            _ke = ke;
                            _orbitWidget.SetParametersFromKeplerElements(ke, _targetInsertionPoint_AU);
                            _apoapsisKm   = Distance.AuToKm(ke.Apoapsis);
                            _periapsisKM  = Distance.AuToKm(ke.Periapsis);
                            _eccentricity = ke.Eccentricity;
                            break;
                        }

                        default:
                            break;
                        }
                    }


                    ImGui.SetTooltip(_tooltipText);
                    ImGui.Text("Target: ");
                    if (TargetEntity != null)
                    {
                        ImGui.SameLine();
                        ImGui.Text(TargetEntity.Name);
                    }
                    ImGui.Text("Apoapsis: ");
                    ImGui.SameLine();
                    ImGui.Text(_apoapsisKm.ToString("g3") + " (Alt: " + _apAlt.ToString("g3") + ")");

                    ImGui.Text("Periapsis: ");
                    ImGui.SameLine();
                    ImGui.Text(_periapsisKM.ToString("g3") + " (Alt: " + _peAlt.ToString("g3") + ")");

                    ImGui.Text("DepartureVelocity: ");
                    //ImGui.SameLine();
                    ImGui.Text(_departureOrbitalSpeed.ToString() + " AU");
                    ImGui.Text(Distance.AuToKm(_departureOrbitalSpeed).ToString() + " KM");

                    ImGui.Text("InsertionVelocity: ");
                    //ImGui.SameLine();
                    ImGui.Text(_insertionOrbitalSpeed.ToString() + " AU");
                    ImGui.Text(Distance.AuToKm(_insertionOrbitalSpeed).ToString() + " KM");

                    ImGui.Text("Eccentricity: ");
                    ImGui.Text(_eccentricity.ToString("g3"));

                    ImGui.Text("Departure Vector: ");
                    ImGui.SameLine();
                    ImGui.Text(_departureOrbitalVelocity.ToString("g3"));
                    ImGui.Text(Distance.AuToMt(_departureOrbitalVelocity).ToString("N") + "m/s");

                    ImGui.Text("Departure Angle: ");
                    ImGui.SameLine();
                    ImGui.Text(_departureAngle.ToString("g3") + " radians or " + Angle.ToDegrees(_departureAngle).ToString("F") + " deg ");

                    var pc = OrbitProcessor.InstantaneousOrbitalVelocityPolarCoordinate(_orderEntityOrbit, _departureDateTime);

                    ImGui.Text("Departure Polar Coordinates: ");
                    ImGui.Text(pc.Item1.ToString() + " AU or " + Distance.AuToMt(pc.Item1).ToString("F") + " m/s");
                    ImGui.Text(pc.Item2.ToString("g3") + " radians or " + Angle.ToDegrees(pc.Item2).ToString("F") + " deg ");;


                    ImGui.Text("Insertion Vector: ");
                    ImGui.SameLine();
                    ImGui.Text(_insertionOrbitalVelocity.ToString("g3"));

                    ImGui.Text("LoAN: ");
                    ImGui.SameLine();
                    ImGui.Text(_ke.LoAN.ToString("g3"));

                    ImGui.Text("AoP: ");
                    ImGui.SameLine();
                    ImGui.Text(_ke.AoP.ToString("g3"));

                    ImGui.Text("LoP Angle: ");
                    ImGui.SameLine();
                    ImGui.Text((_ke.LoAN + _ke.AoP).ToString("g3") + " radians or " + Angle.ToDegrees(_ke.LoAN + _ke.AoP).ToString("F") + " deg ");

                    if (_orbitWidget != null)
                    {
                        ImGui.Text("Clockwise " + _orbitWidget.IsClockwiseOrbit.ToString());
                    }



                    //if (CurrentState != States.NeedsActioning) //use alpha on the button if it's not useable.
                    //ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * 0.5f);
                    if (ImGui.Button("Action Order") && CurrentState == States.NeedsActioning) //only do suff if clicked if it's usable.
                    {
                        fsm[(byte)CurrentState, (byte)Events.ClickedAction].Invoke();
                        //ImGui.PopStyleVar();
                    }

                    if (_smMode)
                    {
                        ImGui.SameLine();
                        if (ImGui.Button("Add OrbitDB"))
                        {
                            ActionAddDB();
                        }
                    }

                    ImGui.End();
                }
            }
        }