Beispiel #1
0
        public override void OnPhysicsUpdate()
        {
            DateTime atDateTime = _entity.Manager.ManagerSubpulses.StarSysDateTime;

            if (_orbitDB != null)
            {
                //var ecAnom = Angle.ToRadians(_orbitDB.)
                var truAnom = OrbitProcessor.GetTrueAnomaly(_orbitDB, atDateTime);
                var heading = OrbitMath.HeadingFromPeriaps(_positionDB.RelativePosition_AU, _orbitDB.Eccentricity, _orbitDB.SemiMajorAxis, truAnom);
                Heading = (float)(heading + _lop);
            }
            else if (_newtonMoveDB != null)
            {
                Heading = (float)Math.Atan2(_newtonMoveDB.CurrentVector_kms.Y, _newtonMoveDB.CurrentVector_kms.X);
            }
            else if (_tlmoveDB != null)
            {
                Heading = _tlmoveDB.Heading_Radians;
            }
        }
Beispiel #2
0
        public void OrbitMath_CalculateAngularMomentum_When_ZeroXPositiveYVelocity_Should_GiveCorrectResults()
        {
            // To determine what the Kepler Elements should be, use : http://orbitsimulator.com/formulas/OrbitalElements.html
            Vector3 position = new Vector3()
            {
                X = Distance.AuToMt(0.25), Y = Distance.AuToMt(0.25)
            };
            Vector3 velocity = new Vector3()
            {
                X = 0, Y = Distance.KmToM(2)
            };
            var expectedResult = new Vector3(
                0,
                0,
                74798935350000.0
                );
            var calculatedResult = OrbitMath.CalculateAngularMomentum(position, velocity);

            Assert.IsTrue(TestVectorsAreEqual(expectedResult, calculatedResult, 1.0d));
        }
        internal override void ActionCommand(DateTime atDateTime)
        {
            if (!IsRunning)
            {
                var portDB = _entityCommanding.GetDataBlob <IndustryAbilityDB>();

                foreach (var job in portDB.ProductionLines[_launchSlot].Jobs)
                {
                    if (job.ItemGuid == _jobID)
                    {
                        _yardJob = job;
                        ShipDesign design = (ShipDesign)_factionEntity.GetDataBlob <FactionInfoDB>().IndustryDesigns[job.ItemGuid];
                        if (_entityCommanding.HasDataBlob <ColonyInfoDB>())
                        {
                            var planet = _entityCommanding.GetDataBlob <ColonyInfoDB>().PlanetEntity;

                            FuelCost       = OrbitMath.FuelCostToLowOrbit(planet, design.MassPerUnit);
                            targetPosition = new Vector3(0, OrbitMath.LowOrbitRadius(planet), 0);
                            IsRunning      = true;
                        }
                        else
                        {
                            FuelCost = OrbitMath.TsiolkovskyFuelCost(design.MassPerUnit, 275, 1);
                            //targetOrbit = (OrbitDB)_entityCommanding.GetDataBlob<OrbitDB>().Clone();
                            targetPosition = _entityCommanding.GetDataBlob <PositionDB>().RelativePosition_m;
                            IsRunning      = true;
                        }
                    }
                }
            }
            else //IsRunning
            {
                //_entityCommanding.GetDataBlob<CargoStorageDB>().StoredCargoTypes
                ShipDesign design = (ShipDesign)_factionEntity.GetDataBlob <FactionInfoDB>().IndustryDesigns[_yardJob.ItemGuid];
                ShipFactory.CreateShip(design, _factionEntity, targetPosition, orbitalParent, (StarSystem)orbitalParent.Manager);
                _hasLaunched = true;
            }
        }
Beispiel #4
0
        public void TestMeanAnomalyCalcs(OrbitDB orbitDB)
        {
            double sgp   = orbitDB.GravitationalParameterAU;
            double o_a   = orbitDB.SemiMajorAxis;
            double o_e   = orbitDB.Eccentricity;
            double o_i   = Angle.ToRadians(orbitDB.Inclination);
            double o_Ω   = Angle.ToRadians(orbitDB.LongitudeOfAscendingNode);
            double o_M0  = Angle.ToRadians(orbitDB.MeanAnomalyAtEpoch);
            double o_n   = Angle.ToRadians(orbitDB.MeanMotion);
            double o_ω   = Angle.ToRadians(orbitDB.ArgumentOfPeriapsis);
            double o_lop = OrbitMath.LonditudeOfPeriapsis2d(o_Ω, o_ω, o_i);

            DateTime o_epoch = orbitDB.Epoch;



            double periodInSeconds = orbitDB.OrbitalPeriod.TotalSeconds;
            double segmentTime     = periodInSeconds / 16;

            //lets break the orbit up and check the paremeters at different points of the orbit:
            for (int i = 0; i < 16; i++)
            {
                TimeSpan timeSinceEpoch  = TimeSpan.FromSeconds(segmentTime * i);
                DateTime segmentDatetime = o_epoch + timeSinceEpoch;
                double   o_M             = OrbitMath.GetMeanAnomalyFromTime(o_M0, o_n, timeSinceEpoch.TotalSeconds); //orbitProcessor uses this calc directly
                double   o_E             = OrbitProcessor.GetEccentricAnomaly(orbitDB, o_M);
                double   o_ν             = OrbitProcessor.GetTrueAnomaly(orbitDB, segmentDatetime);

                var pos = OrbitProcessor.GetPosition_AU(orbitDB, segmentDatetime);
                var vel = OrbitMath.InstantaneousOrbitalVelocityVector(sgp, pos, o_a, o_e, o_ν);

                var M1 = OrbitMath.GetMeanAnomaly(o_e, o_E);

                Assert.AreEqual(o_M, M1, 1.0E-7, "MeanAnomaly M expected: " + Angle.ToDegrees(o_M) + " was: " + Angle.ToDegrees(M1));
            }
        }
Beispiel #5
0
        private void HardRefresh(EntityState orderEntity)
        {
            _orderEntity  = orderEntity.Entity;
            _newtonThrust = _orderEntity.GetDataBlob <NewtonThrustAbilityDB>();
            var myMass     = _orderEntity.GetDataBlob <MassVolumeDB>().MassDry;
            var parentMass = Entity.GetSOIParentEntity(_orderEntity).GetDataBlob <MassVolumeDB>().MassDry;

            _sgp = OrbitMath.CalculateStandardGravityParameterInM3S2(myMass, parentMass);

            _siblingEntities = Entity.GetSOIParentEntity(_orderEntity).GetDataBlob <PositionDB>().Children.ToArray();
            List <string> names = new List <string>();

            foreach (var entity in _siblingEntities)
            {
                //TODO: this is going to show *all* entities, not just the ones this faction can see.
                //going to need to come up with a way to get this data. (filtering for this should be done in the engine not ui)
                string name = entity.GetDataBlob <NameDB>().GetName(_orderEntity.FactionOwner);
                names.Add(name);
            }

            _siblingNames = names.ToArray();

            OnSystemTickChange(orderEntity.Entity.StarSysDateTime);
        }
Beispiel #6
0
        internal override void Display()
        {
            _isRunningFrame = true;
            if (IsActive)
            {
                SetFrameRateArray();
                if (ImGui.Begin("debug", ref IsActive))
                {
                    ImGui.Text(_state.PrimarySystemDateTime.ToString());
                    ImGui.Text("Cursor World Coordinate:");
                    var mouseWorldCoord = _state.Camera.MouseWorldCoordinate();
                    ImGui.Text("x" + mouseWorldCoord.X);
                    ImGui.SameLine();
                    ImGui.Text("y" + mouseWorldCoord.Y);

                    if (ImGui.CollapsingHeader("FrameRates", ImGuiTreeNodeFlags.CollapsingHeader))
                    {
                        //plot vars: (label, values, valueOffset, overlayText, scaleMin, scaleMax, graphSize, Stride)
                        //core game processing rate.
                        //ImGui.PlotHistogram("##GRHistogram", _gameRatesDisplay, 10, _timeSpan.TotalSeconds.ToString(), 0, 1f, new ImVec2(0, 80), sizeof(float));
                        //ImGui.PlotHistogram("##GRHistogram1", _gameRatesDisplay, 0 , _timeSpan.TotalSeconds.ToString(), 0, 1f, new ImVec2(0, 80), sizeof(float));
                        //string label, ref float values...
                        //ImGui.PlotHistogram(
                        ImGui.PlotHistogram("Game Tick ##GTHistogram", ref _gameRates[0], _gameRates.Length, _gameRateIndex, _currentGFPS.ToString(), 0f, largestGFPS, new Vector2(248, 60), sizeof(float));
                        ImGui.PlotLines("Game Tick ##GTPlotlines", ref _gameRates[0], _gameRates.Length, _gameRateIndex, _currentGFPS.ToString(), 0, largestGFPS, new Vector2(248, 60), sizeof(float));
                        //current star system processing rate.
                        ImGui.PlotHistogram("System Tick ##STHistogram", ref _systemRates[0], _systemRates.Length, _systemRateIndex, _currentSFPS.ToString(), 0f, 1f, new Vector2(248, 60), sizeof(float));
                        ImGui.PlotLines("System Tick ##STPlotlines", ref _systemRates[0], _systemRates.Length, _systemRateIndex, _currentSFPS.ToString(), 0, 1, new Vector2(248, 60), sizeof(float));
                        //ui framerate
                        ImGui.PlotHistogram("Frame Rate ##FPSHistogram", ref _frameRates[0], _frameRates.Length, _frameRateIndex, _currentFPS.ToString(), 0f, 10000, new Vector2(248, 60), sizeof(float));

                        foreach (var item in _systemState.StarSystem.ManagerSubpulses.ProcessTime)
                        {
                            ImGui.Text(item.Key.Name);
                            ImGui.SameLine();
                            ImGui.Text(item.Value.ToString());
                        }
                    }

                    if (ImGui.CollapsingHeader("GraphicTests", ImGuiTreeNodeFlags.CollapsingHeader))
                    {
                        var window = GraphicDebugWindow.GetWindow(_state);
                        window.Display();
                        window.Enable(true, _state);
                    }

                    ImGui.Text("Selected Star System: " + _state.SelectedStarSysGuid);
                    ImGui.Text("Number Of Entites: " + _state.SelectedSystem.NumberOfEntites);
                    if (ImGui.CollapsingHeader("Log"))
                    {
                        ImGui.BeginChild("LogChild", new Vector2(800, 300), true);
                        ImGui.Columns(4, "Events", true);
                        ImGui.Text("DateTime");
                        ImGui.NextColumn();
                        ImGui.Text("Faction");
                        ImGui.NextColumn();
                        ImGui.Text("Entity");
                        ImGui.NextColumn();
                        ImGui.Text("Event Message");
                        ImGui.NextColumn();

                        foreach (var gameEvent in StaticRefLib.EventLog.GetAllEvents())
                        {
                            string entityStr = "";
                            if (gameEvent.Entity != null)
                            {
                                if (gameEvent.Entity.HasDataBlob <NameDB>())
                                {
                                    entityStr = gameEvent.Entity.GetDataBlob <NameDB>().DefaultName;
                                }
                                else
                                {
                                    entityStr = gameEvent.Entity.Guid.ToString();
                                }
                            }
                            string factionStr = "";
                            if (gameEvent.Faction != null)
                            {
                                if (gameEvent.Faction.HasDataBlob <NameDB>())
                                {
                                    factionStr = gameEvent.Faction.GetDataBlob <NameDB>().DefaultName;
                                }
                                else
                                {
                                    factionStr = gameEvent.Faction.Guid.ToString();
                                }
                            }

                            ImGui.Separator();
                            ImGui.Text(gameEvent.Time.ToString());
                            ImGui.NextColumn();
                            ImGui.Text(factionStr);
                            ImGui.NextColumn();
                            ImGui.Text(entityStr);
                            ImGui.NextColumn();
                            ImGui.TextWrapped(gameEvent.Message);

                            ImGui.NextColumn();
                        }
                        //ImGui.Separator();
                        //ImGui.Columns();
                        ImGui.EndChild();
                    }
                    if (ImGui.CollapsingHeader("Entity List"))
                    {
                        List <Entity> factionOwnedEntites = _state.SelectedSystem.GetEntitiesByFaction(_state.Faction.Guid);
                        List <string> entityNames         = new List <string>();
                        foreach (var entity in factionOwnedEntites)
                        {
                            var name = entity.GetDataBlob <NameDB>();
                            if (name != null)
                            {
                                entityNames.Add(name.GetName(_state.Faction.Guid));
                            }
                        }
                        int item = 0;
                        ImGui.ListBox("Entites", ref item, entityNames.ToArray(), entityNames.Count);
                    }
                    if (_selectedEntityState != null && _selectedEntityState.Name != null && _selectedEntity.IsValid)
                    {
                        if (ImGui.CollapsingHeader("Selected Entity: " + _state.LastClickedEntity.Name + "###NameHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                        {
                            ImGui.Text(_state.LastClickedEntity.Entity.Guid.ToString());
                            if (_selectedEntity.HasDataBlob <PositionDB>())
                            {
                                var positiondb = _selectedEntity.GetDataBlob <PositionDB>();
                                var posv4      = positiondb.AbsolutePosition_AU;
                                ImGui.Text("x: " + posv4.X);
                                ImGui.Text("y: " + posv4.Y);
                                ImGui.Text("z: " + posv4.Z);
                                if (positiondb.Parent != null)
                                {
                                    ImGui.Text("Parent: " + positiondb.Parent.GetDataBlob <NameDB>().DefaultName);
                                    ImGui.Text("Dist: " + Distance.AuToKm(positiondb.RelativePosition_AU.Length()));
                                }
                            }
                            if (_selectedEntity.HasDataBlob <MassVolumeDB>())
                            {
                                if (ImGui.CollapsingHeader("MassVolumeDB: ###MassVolDBHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                                {
                                    MassVolumeDB mvdb = _selectedEntity.GetDataBlob <MassVolumeDB>();
                                    ImGui.Text("Mass " + mvdb.Mass + "Kg");
                                    ImGui.Text("Volume " + mvdb.Volume + "Km^3");
                                    ImGui.Text("Density " + mvdb.Density + "g/cm^3");
                                    ImGui.Text("Radius " + mvdb.Radius + "Km");
                                }
                            }
                            if (_selectedEntity.HasDataBlob <OrbitDB>())
                            {
                                if (ImGui.Checkbox("Draw SOI", ref _drawSOI))
                                {
                                    SimpleCircle cir;
                                    if (_drawSOI)
                                    {
                                        var soiradius = OrbitProcessor.GetSOI(_selectedEntity);
                                        var colour    = new SDL2.SDL.SDL_Color()
                                        {
                                            r = 0, g = 255, b = 0, a = 100
                                        };
                                        cir = new SimpleCircle(_selectedEntity.GetDataBlob <PositionDB>(), soiradius, colour);

                                        _state.SelectedSysMapRender.UIWidgets.Add(nameof(cir), cir);
                                    }
                                    else
                                    {
                                        _state.SelectedSysMapRender.UIWidgets.Remove(nameof(cir));
                                    }
                                }

                                if (ImGui.CollapsingHeader("OrbitDB: ###OrbitDBHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                                {
                                    OrbitDB orbitDB = _selectedEntity.GetDataBlob <OrbitDB>();

                                    //if (_state.CurrentSystemDateTime != lastDate)
                                    //{
                                    pos        = OrbitProcessor.GetAbsolutePosition_AU(orbitDB, _state.PrimarySystemDateTime);
                                    truAnomoly = OrbitProcessor.GetTrueAnomaly(orbitDB, _state.PrimarySystemDateTime);
                                    lastDate   = _state.PrimarySystemDateTime;
                                    //}

                                    ImGui.Text("x: " + pos.X);
                                    ImGui.Text("y: " + pos.Y);
                                    ImGui.Text("z: " + pos.Z);
                                    ImGui.Text("Eccentricity: " + orbitDB.Eccentricity);
                                    ImGui.Text("AoP:" + orbitDB.ArgumentOfPeriapsis);
                                    ImGui.Text("TrueAnomaly: " + truAnomoly);
                                    ImGui.Text("MeanMotion: " + orbitDB.MeanMotion + " in Deg/s");
                                    ImGui.Text("MeanVelocity: " + OrbitMath.MeanOrbitalVelocityInAU(orbitDB) + "Au/s");
                                    ImGui.Text("MeanVelocity: " + Distance.AuToKm(OrbitMath.MeanOrbitalVelocityInAU(orbitDB)) + "Km/s");
                                    ImGui.Text("SOI Radius: " + Distance.AuToKm(OrbitProcessor.GetSOI(_state.LastClickedEntity.Entity)));
                                    ImGui.Text("Orbital Period:" + orbitDB.OrbitalPeriod);
                                    ImGui.Text("SemiMajAxis: " + orbitDB.SemiMajorAxis);
                                    ImGui.Text("Periapsis: " + Distance.AuToKm(orbitDB.Periapsis).ToString("g3") + " Km");
                                    ImGui.Text("Appoapsis: " + Distance.AuToKm(orbitDB.Apoapsis).ToString("g3") + " Km");
                                    if (orbitDB.Parent != null)
                                    {
                                        ImGui.Text("Parent: " + orbitDB.Parent.GetDataBlob <NameDB>().DefaultName);
                                    }
                                    if (orbitDB.Children.Count > 0)
                                    {
                                        foreach (var item in orbitDB.Children)
                                        {
                                            ImGui.Text(item.GetDataBlob <NameDB>().DefaultName);
                                        }
                                    }
                                }
                            }

                            if (_selectedEntity.HasDataBlob <NewtonMoveDB>())
                            {
                                if (ImGui.Checkbox("Draw Parent SOI", ref _drawParentSOI))
                                {
                                    SimpleCircle psoi;
                                    SimpleLine   psoilin;
                                    if (_drawParentSOI)
                                    {
                                        var myPos  = _selectedEntity.GetDataBlob <PositionDB>();
                                        var parent = myPos.Parent;
                                        var pObt   = parent.GetDataBlob <OrbitDB>();
                                        var cnmve  = _selectedEntity.GetDataBlob <NewtonMoveDB>();

                                        var soiradius = OrbitProcessor.GetSOI(parent);
                                        var colour    = new SDL2.SDL.SDL_Color()
                                        {
                                            r = 0, g = 255, b = 0, a = 100
                                        };
                                        psoi = new SimpleCircle(parent.GetDataBlob <PositionDB>(), soiradius, colour);
                                        var pmass  = parent.GetDataBlob <MassVolumeDB>().Mass;
                                        var mymass = _selectedEntity.GetDataBlob <MassVolumeDB>().Mass;

                                        var    sgp          = GameConstants.Science.GravitationalConstant * (pmass + mymass) / 3.347928976e33;
                                        var    vel          = Distance.KmToAU(cnmve.CurrentVector_kms);
                                        var    cpos         = myPos.RelativePosition_AU;
                                        var    eccentVector = OrbitMath.EccentricityVector(sgp, cpos, vel);
                                        double ce           = eccentVector.Length();
                                        var    r            = cpos.Length();
                                        var    v            = vel.Length();

                                        var ca = 1 / (2 / r - Math.Pow(v, 2) / sgp);
                                        var cp = EllipseMath.SemiLatusRectum(ca, ce);

                                        var cAoP = Math.Atan2(eccentVector.Y, eccentVector.X);

                                        /*
                                         * var pa = pObt.SemiMajorAxis;
                                         * var pe = pObt.Eccentricity;
                                         * var pp = EllipseMath.SemiLatusRectum(pa, pe);
                                         */
                                        double θ = OrbitMath.AngleAtRadus(soiradius, cp, ce);
                                        θ += cAoP;

                                        var x = soiradius * Math.Cos(θ);
                                        var y = soiradius * Math.Sin(θ);
                                        psoilin = new SimpleLine(parent.GetDataBlob <PositionDB>(), new PointD()
                                        {
                                            X = x, Y = y
                                        }, colour);

                                        _state.SelectedSysMapRender.UIWidgets.Add(nameof(psoi), psoi);
                                        _state.SelectedSysMapRender.UIWidgets.Add(nameof(psoilin), psoilin);
                                    }
                                    else
                                    {
                                        _state.SelectedSysMapRender.UIWidgets.Remove(nameof(psoi));
                                        _state.SelectedSysMapRender.UIWidgets.Remove(nameof(psoilin));
                                    }
                                }
                            }

                            if (_state.LastClickedEntity.OrbitIcon != null)
                            {
                                if (ImGui.CollapsingHeader("OrbitIcon: ###OrbitIconHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                                {
                                    OrbitDB orbitDB = _selectedEntity.GetDataBlob <OrbitDB>();

                                    //string startRadian = _state.LastClickedEntity.OrbitIcon._ellipseStartArcAngleRadians.ToString();
                                    //string startDegrees = Angle.ToDegrees(_state.LastClickedEntity.OrbitIcon._ellipseStartArcAngleRadians).ToString();
                                    //ImGui.Text("StartAngleRadians: " + startRadian);
                                    //ImGui.Text("StartAngleDegrees: " + startDegrees);
                                    if (ImGui.CollapsingHeader("OrbitIconLines", ImGuiTreeNodeFlags.CollapsingHeader))
                                    {
                                        var window = OrbitalDebugWindow.GetWindow(_state.LastClickedEntity);
                                        window.Display();
                                        window.Enable(true, _state);
                                    }
                                }
                            }

                            if (_selectedEntity.HasDataBlob <PropulsionAbilityDB>())
                            {
                                if (ImGui.CollapsingHeader("Propulsion: ###PropulsionHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                                {
                                    PropulsionAbilityDB propulsionDB = _selectedEntity.GetDataBlob <PropulsionAbilityDB>();
                                    ImGui.Text("NonNewt Engine Power: " + propulsionDB.TotalEnginePower);
                                    ImGui.Text("Max Speed: " + propulsionDB.MaximumSpeed_MS);
                                    ImGui.Text("CurrentVector: " + propulsionDB.CurrentVectorMS);
                                    ImGui.Text("Current Speed: " + ECSLib.Vector3.Magnitude(propulsionDB.CurrentVectorMS));
                                    if (_state.LastClickedEntity.Entity.HasDataBlob <CargoStorageDB>())
                                    {
                                        var fuelsGuid = propulsionDB.FuelUsePerKM;
                                        var storage   = _state.LastClickedEntity.Entity.GetDataBlob <CargoStorageDB>();
                                        foreach (var fuelItemGuid in fuelsGuid.Keys)
                                        {
                                            var fuel = _state.Game.StaticData.GetICargoable(fuelItemGuid);
                                            ImGui.Text(fuel.Name);
                                            ImGui.SameLine();
                                            ImGui.Text(StorageSpaceProcessor.GetAmount(storage, fuel).ToString());
                                        }
                                    }
                                }
                                if (_state.LastClickedEntity.Entity.HasDataBlob <TranslateMoveDB>())
                                {
                                    var db = _state.LastClickedEntity.Entity.GetDataBlob <TranslateMoveDB>();
                                    if (ImGui.CollapsingHeader("Transit: ###TransitHeader", ImGuiTreeNodeFlags.CollapsingHeader))
                                    {
                                        ImGui.Text("EntryPoint " + db.TranslateEntryPoint_AU);
                                        ImGui.Text("ExitPoint  " + db.TranslateExitPoint_AU);
                                        ImGui.Text("EDA " + db.PredictedExitTime.ToString());
                                        double distance = Distance.DistanceBetween(db.TranslateEntryPoint_AU, db.TranslateExitPoint_AU);
                                        ImGui.Text("Distance " + distance + " AU");
                                        ImGui.SameLine();
                                        double distancekm = Distance.AuToKm(distance);
                                        ImGui.Text(distancekm.ToString() + " KM");
                                        var timeToTarget = db.PredictedExitTime - _state.PrimarySystemDateTime;
                                        ImGui.Text("Remaining TTT " + timeToTarget);
                                        var totalTime = db.PredictedExitTime - db.EntryDateTime;
                                        ImGui.Text("Total TTT  " + totalTime);
                                        double speed = ((distancekm * 1000) / totalTime.TotalSeconds);
                                        ImGui.Text("speed2 " + speed);
                                        ImGui.Text("LastDateTime: ");
                                        ImGui.Text(db.LastProcessDateTime.ToString());
                                        ImGui.Text("Time Since Last: ");
                                        var timelen = _state.PrimarySystemDateTime - db.LastProcessDateTime;
                                        ImGui.Text(timelen.ToString());
                                    }
                                }
                            }
                            if (_selectedEntity.HasDataBlob <SensorInfoDB>())
                            {
                                var actualEntity = _selectedEntity.GetDataBlob <SensorInfoDB>().DetectedEntity;
                                if (actualEntity.IsValid && actualEntity.HasDataBlob <AsteroidDamageDB>())
                                {
                                    var dmgDB = actualEntity.GetDataBlob <AsteroidDamageDB>();
                                    ImGui.Text("Remaining HP: " + dmgDB.Health.ToString());
                                }
                            }
                        }
                    }
                }
                //else IsActive = false;
                ImGui.End();
            }
            _isRunningFrame           = false;
            _dateChangeSinceLastFrame = false;
        }
Beispiel #7
0
 private void Awake()
 {
     instance = this;
 }
Beispiel #8
0
        public void TestingKeplerConversions(OrbitDB orbitDB)
        {
            double sgp   = orbitDB.GravitationalParameterAU;
            double o_a   = orbitDB.SemiMajorAxis;
            double o_e   = orbitDB.Eccentricity;
            double o_i   = Angle.ToRadians(orbitDB.Inclination);
            double o_Ω   = Angle.ToRadians(orbitDB.LongitudeOfAscendingNode);
            double o_M0  = Angle.ToRadians(orbitDB.MeanAnomalyAtEpoch);
            double o_n   = Angle.ToRadians(orbitDB.MeanMotion);
            double o_ω   = Angle.ToRadians(orbitDB.ArgumentOfPeriapsis);
            double o_lop = OrbitMath.LonditudeOfPeriapsis2d(o_Ω, o_ω, o_i);

            DateTime o_epoch = orbitDB.Epoch;

            double periodInSeconds = OrbitMath.GetOrbitalPeriodInSeconds(sgp, o_a);

            Assert.AreEqual(periodInSeconds, orbitDB.OrbitalPeriod.TotalSeconds, 0.1);

            //lets break the orbit up and check the rest of the paremeters at different points of the orbit:
            double segmentTime = periodInSeconds / 16;

            for (int i = 0; i < 16; i++)
            {
                TimeSpan timeSinceEpoch  = TimeSpan.FromSeconds(segmentTime * i);
                DateTime segmentDatetime = o_epoch + timeSinceEpoch;

                double o_M = OrbitMath.GetMeanAnomalyFromTime(o_M0, o_n, timeSinceEpoch.TotalSeconds); //orbitProcessor uses this calc directly
                double o_E = OrbitProcessor.GetEccentricAnomaly(orbitDB, o_M);
                double o_ν = OrbitProcessor.GetTrueAnomaly(orbitDB, segmentDatetime);

                var pos = OrbitProcessor.GetPosition_AU(orbitDB, segmentDatetime);
                var vel = (Vector3)OrbitMath.InstantaneousOrbitalVelocityVector(sgp, pos, o_a, o_e, o_ν);

                var ke = OrbitMath.KeplerFromPositionAndVelocity(sgp, pos, vel, segmentDatetime);

                var    ke_epoch = ke.Epoch;
                double ke_a     = ke.SemiMajorAxis;
                double ke_e     = ke.Eccentricity;
                double ke_i     = ke.Inclination;
                double ke_Ω     = ke.LoAN;
                double ke_M0    = ke.MeanAnomalyAtEpoch;
                double ke_n     = ke.MeanMotion;
                double ke_ω     = ke.AoP;

                double ke_E = OrbitMath.GetEccentricAnomalyNewtonsMethod(ke.Eccentricity, ke_M0);
                double ke_ν = OrbitMath.TrueAnomalyFromEccentricAnomaly(ke_e, ke_E);


                Assert.Multiple(() =>
                {
                    //these should not change (other than floating point errors) between each itteration
                    Assert.AreEqual(o_a, ke_a, 0.001, "SemiMajorAxis a"); //should be more accurate than this, though if testing from a given set of ke to state, and back, the calculated could be more acurate...
                    Assert.AreEqual(o_e, ke_e, 0.00001, "Eccentricity e");
                    Assert.AreEqual(o_i, ke_i, 1.0E-7, "Inclination i expected: " + Angle.ToDegrees(o_i) + " was: " + Angle.ToDegrees(ke_i));
                    Assert.AreEqual(o_Ω, ke_Ω, 1.0E-7, "LoAN Ω expected: " + Angle.ToDegrees(o_Ω) + " was: " + Angle.ToDegrees(ke_Ω));
                    Assert.AreEqual(o_ω, ke_ω, 1.0E-7, "AoP ω expected: " + Angle.ToDegrees(o_ω) + " was: " + Angle.ToDegrees(ke_ω));
                    Assert.AreEqual(o_n, ke_n, 1.0E-7, "MeanMotion n expected: " + Angle.ToDegrees(o_n) + " was: " + Angle.ToDegrees(ke_n));

                    //these will change between itterations:

                    Assert.AreEqual(o_E, ke_E, 1.0E-7, "EccentricAnomaly E expected: " + Angle.ToDegrees(o_E) + " was: " + Angle.ToDegrees(ke_E));
                    Assert.AreEqual(o_ν, ke_ν, 1.0E-7, "True Anomaly ν expected: " + Angle.ToDegrees(o_ν) + " was: " + Angle.ToDegrees(ke_ν));
                    Assert.AreEqual(o_M, ke_M0, 1.0E-7, "MeanAnomaly M expected: " + Angle.ToDegrees(o_M) + " was: " + Angle.ToDegrees(ke_M0));
                });
            }
        }
Beispiel #9
0
        public void TestOrbitalVelocityCalcs(OrbitDB orbitDB)
        {
            double sgp   = orbitDB.GravitationalParameterAU;
            double o_a   = orbitDB.SemiMajorAxis;
            double o_e   = orbitDB.Eccentricity;
            double o_i   = Angle.ToRadians(orbitDB.Inclination);
            double o_Ω   = Angle.ToRadians(orbitDB.LongitudeOfAscendingNode);
            double o_M0  = Angle.ToRadians(orbitDB.MeanAnomalyAtEpoch);
            double o_n   = Angle.ToRadians(orbitDB.MeanMotion);
            double o_ω   = Angle.ToRadians(orbitDB.ArgumentOfPeriapsis);
            double o_lop = OrbitMath.LonditudeOfPeriapsis2d(o_Ω, o_ω, o_i);

            DateTime o_epoch = orbitDB.Epoch;

            double periodInSeconds = orbitDB.OrbitalPeriod.TotalSeconds;
            double segmentTime     = periodInSeconds / 16;

            //lets break the orbit up and check the paremeters at different points of the orbit:
            for (int i = 0; i < 16; i++)
            {
                TimeSpan timeSinceEpoch  = TimeSpan.FromSeconds(segmentTime * i);
                DateTime segmentDatetime = o_epoch + timeSinceEpoch;
                double   o_M             = OrbitMath.GetMeanAnomalyFromTime(o_M0, o_n, timeSinceEpoch.TotalSeconds); //orbitProcessor uses this calc directly
                double   o_E             = OrbitProcessor.GetEccentricAnomaly(orbitDB, o_M);
                double   o_ν             = OrbitProcessor.GetTrueAnomaly(orbitDB, segmentDatetime);

                var pos = OrbitProcessor.GetPosition_AU(orbitDB, segmentDatetime);
                var vel = (Vector3)OrbitMath.InstantaneousOrbitalVelocityVector(sgp, pos, o_a, o_e, o_ν);

                Vector3 angularVelocity = Vector3.Cross(pos, vel);
                double  r       = pos.Length();
                double  speedau = OrbitMath.InstantaneousOrbitalSpeed(sgp, r, o_a);
                (double speed, double heading)polarVelocity = OrbitMath.InstantaneousOrbitalVelocityPolarCoordinate(sgp, pos, o_a, o_e, o_ν);
                //Tuple<double, double> polarVelocity2 = OrbitMath.PreciseOrbitalVelocityPolarCoordinate2(sgp, pos, o_a, o_e, o_ν, o_lop);
                double heading = OrbitMath.HeadingFromPeriaps(pos, o_e, o_a, o_ν);
                heading += o_lop;
                //Assert.IsTrue(angularVelocity.Z > 0); //TODO:this will break if we test an anti clockwise orbit.
                Assert.IsTrue(speedau > 0); //I'm assuming that speed will be <0 if retrograde orbit.
                Assert.AreEqual(vel.Length(), speedau, 1.0E-7);
                Assert.AreEqual(vel.Length(), polarVelocity.Item1, 1.0E-7);

                double hackHeading = OrbitMath.HackVelocityHeading(orbitDB, segmentDatetime);
                double hackheadD   = Angle.ToDegrees(hackHeading);
                double headingD    = Angle.ToDegrees(heading);


                if (o_e == 0)                   //we can make this work with ellipses if we add the lop to the position.
                {
                    if (pos.X > 0 && pos.Y > 0) //top right quadrant
                    {
                        //Assert.IsTrue(polarVelocity.Item2 > Math.PI * 0.5 && polarVelocity.Item2 < Math.PI);
                        //Assert.IsTrue(hackHeading > Math.PI * 0.5 && hackHeading < Math.PI);
                        Assert.IsTrue(heading >= Math.PI * 0.5 && heading <= Math.PI);
                    }
                    if (pos.X < 0 && pos.Y > 0)//top left quadrant
                    {
                        //Assert.IsTrue(polarVelocity.Item2 > Math.PI && polarVelocity.Item2 < Math.PI * 1.5);
                        //Assert.IsTrue(hackHeading > Math.PI && hackHeading < Math.PI * 1.5);
                        Assert.IsTrue(heading >= Math.PI && heading <= Math.PI * 1.5);
                    }
                    if (pos.X < 0 && pos.Y < 0)//bottom left quadrant
                    {
                        //Assert.IsTrue(polarVelocity.Item2 > Math.PI * 1.5 && polarVelocity.Item2 < Math.PI * 2);
                        //Assert.IsTrue(hackHeading > Math.PI * 1.5 && hackHeading < Math.PI * 2);
                        Assert.IsTrue(heading >= Math.PI * 1.5 && heading <= Math.PI * 2);
                    }
                    if (pos.X > 0 && pos.Y < 0)//bottom right quadrant
                    {
                        //Assert.IsTrue(polarVelocity.Item2 > 0 && polarVelocity.Item2 < Math.PI * 0.5);
                        //Assert.IsTrue(hackHeading > 0 && hackHeading < Math.PI * 0.5);
                        Assert.IsTrue(heading >= 0 && heading <= Math.PI * 0.5);
                    }
                }
            }
        }
Beispiel #10
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);
            }
        }
        public static void LaunchMissile(Entity launchingEntity, Entity targetEntity, double launchForce, OrdnanceDesign missileDesign, int count)
        {
            var     atDatetime       = launchingEntity.Manager.StarSysDateTime;
            var     parentPositionDB = launchingEntity.GetDataBlob <PositionDB>();
            Vector3 parentPosition   = parentPositionDB.AbsolutePosition_m;
            var     parentPosRal     = parentPositionDB.RelativePosition_m;
            var     tgtEntityOrbit   = targetEntity.GetDataBlob <OrbitDB>();

            if (targetEntity.HasDataBlob <OrbitUpdateOftenDB>())
            {
                tgtEntityOrbit = targetEntity.GetDataBlob <OrbitUpdateOftenDB>();
            }

            //MissileLauncherAtb launcherAtb;
            VolumeStorageDB cargo = launchingEntity.GetDataBlob <VolumeStorageDB>();

            int numMis = cargo.TypeStores[missileDesign.CargoTypeID].CurrentStoreInUnits[missileDesign.ID];

            if (numMis < 1)
            {
                return;
            }



            double launchSpeed = launchForce / missileDesign.MassPerUnit;

            double  burnTime        = ((missileDesign.WetMass - missileDesign.DryMass) / missileDesign.BurnRate) * 0.8; //use 80% of fuel.
            double  drymass         = (missileDesign.WetMass - missileDesign.DryMass) * 0.8;                            //use 80% of fuel.
            double  launchManuverDv = OrbitMath.TsiolkovskyRocketEquation(missileDesign.WetMass, drymass, missileDesign.ExaustVelocity);
            double  totalDV         = OrbitMath.TsiolkovskyRocketEquation(missileDesign.WetMass, missileDesign.DryMass, missileDesign.ExaustVelocity);
            double  speed           = launchSpeed + launchManuverDv;
            var     misslPositionDB = (PositionDB)parentPositionDB.Clone();
            Vector3 parentVelocity  = Entity.GetRalitiveFutureVelocity(launchingEntity, launchingEntity.StarSysDateTime);



            var orderabledb = new OrderableDB();
            var newtmovedb  = new NewtonMoveDB(misslPositionDB.Parent, parentVelocity);

            string defaultName  = "Missile";
            string factionsName = missileDesign.Name;

            if (count > 1)
            {
                defaultName  += " x" + count;
                factionsName += " x" + count;
            }

            List <BaseDataBlob> dataBlobs = new List <BaseDataBlob>();

            dataBlobs.Add(new ProjectileInfoDB(launchingEntity.Guid, count));
            dataBlobs.Add(new ComponentInstancesDB());
            dataBlobs.Add(misslPositionDB);
            dataBlobs.Add(MassVolumeDB.NewFromMassAndVolume(missileDesign.WetMass, missileDesign.WetMass));
            dataBlobs.Add(new NameDB(defaultName, launchingEntity.FactionOwner, factionsName));
            dataBlobs.Add(newtmovedb);
            dataBlobs.Add(orderabledb);
            var newMissile = Entity.Create(launchingEntity.Manager, launchingEntity.FactionOwner, dataBlobs);

            foreach (var tuple in missileDesign.Components)
            {
                EntityManipulation.AddComponentToEntity(newMissile, tuple.design, tuple.count);
            }

            var newtdb = newMissile.GetDataBlob <NewtonThrustAbilityDB>();

            newtdb.DryMass_kg = missileDesign.MassPerUnit;
            newtdb.SetFuel(missileDesign.WetMass - missileDesign.MassPerUnit);


            bool directAttack = false;


            if (directAttack)
            {
                /*
                 * var tgtintercept = OrbitMath.GetInterceptPosition_m(parentPosition, speed, tgtEntityOrbit, atDatetime);
                 * var tgtEstPos = tgtintercept.position + targetEntity.GetDataBlob<PositionDB>().RelativePosition_m;
                 *
                 * var tgtCurPos = Entity.GetPosition_m(targetEntity, atDatetime);
                 *
                 * var vectorToTgt = Vector3.Normalise(tgtCurPos - parentPosRal);
                 *
                 * //var vectorToTgt = Vector3.Normalise(tgtEstPos - parentPosRal);
                 * var launcherVector = vectorToTgt * launchSpeed;
                 *
                 *
                 * var launchVelocity = parentVelocity + launcherVector;
                 * var manuverDV = vectorToTgt * launchManuverDv;
                 *
                 * launchVelocity = parentVelocity + launcherVector;
                 */
                ThrustToTargetCmd.CreateCommand(launchingEntity.FactionOwner, newMissile, launchingEntity.StarSysDateTime, targetEntity);
            }
            else
            {
                var launchOrbit = launchingEntity.GetDataBlob <OrbitDB>();
                if (launchingEntity.HasDataBlob <OrbitUpdateOftenDB>())
                {
                    launchOrbit = launchingEntity.GetDataBlob <OrbitUpdateOftenDB>();
                }

                var launchTrueAnomaly = OrbitProcessor.GetTrueAnomaly(launchOrbit, atDatetime);
                var targetTrueAnomaly = OrbitProcessor.GetTrueAnomaly(tgtEntityOrbit, atDatetime);
                var phaseAngle        = targetTrueAnomaly - launchTrueAnomaly;
                var manuvers          = InterceptCalcs.OrbitPhasingManuvers(launchOrbit, atDatetime, phaseAngle);


                var manuverDV = manuvers[0].deltaV;
                //newtmovedb.ActionOnDateTime = atDatetime;
                //newtmovedb.DeltaVForManuver_FoRO_m = manuverDV;
                NewtonThrustCommand.CreateCommand(launchingEntity.FactionOwner, newMissile, atDatetime, manuverDV);

                DateTime futureDate = atDatetime + TimeSpan.FromSeconds(manuvers[1].timeInSeconds);
                Vector3  futureDV   = manuvers[1].deltaV;
                NewtonThrustCommand.CreateCommand(launchingEntity.FactionOwner, newMissile, futureDate, futureDV);
                //ThrustToTargetCmd.CreateCommand(launchingEntity.FactionOwner, newMissile, futureDate + TimeSpan.FromSeconds(1), targetEntity);
            }

            cargo.RemoveCargoByUnit(missileDesign, 1); //remove missile from parent.
        }
 public void FixedUpdate()
 {
     OrbitMath.OrbitPrediction prediction = OrbitMath.GetStaticOrbitPrediction(Time.time, this);
     transform.localPosition = prediction.localPosition;
 }
 public OrbitMath.OrbitPrediction GetPrediction(float time)
 {
     //Todo Cache
     return(OrbitMath.GetStaticOrbitPrediction(time, this));
 }
Beispiel #14
0
        internal override void Display()
        {
            if (IsActive && ImGui.Begin("Ordnance Design"))
            {
                ImGui.Combo("Current Designs", ref _payloadSelectedIndex, _currentDesignNames, _currentDesignNames.Length);

                ImGui.NewLine();
                BorderGroup.BeginBorder("Payload:");
                if (ImGui.Combo("Payload type", ref _payloadSelectedIndex, _payload, _payload.Length))
                {
                    //_selectedPayload = _payloadTypes[_payloadSelectedIndex].GetAttribute<OrdnancePayloadAtb>();
                    _selectedComponentDesigns[_payloadTypes[_payloadSelectedIndex]] = _payloadCount;
                    RefreshMass();
                }
                if (ImGui.SliderInt("Payload Count", ref _payloadCount, 1, 100))
                {
                    _selectedComponentDesigns[_payloadTypes[_payloadSelectedIndex]] = _payloadCount;
                    RefreshMass();
                }
                var whmass = _payloadTypes[_payloadSelectedIndex].Mass * _payloadCount;
                ImGui.Text("Mass: " + Stringify.Mass(whmass));
                //ImGui.Text("Payload Trigger Type: " + _selectedPayload.Trigger);
                BorderGroup.EndBoarder();
                ImGui.NewLine();

                BorderGroup.BeginBorder("Electronics Suite:");
                if (ImGui.Combo("ElectronicsSuite", ref _electronicsSelectedIndex, _electronicsPackage, _electronicsPackage.Length))
                {
                    _selectedComponentDesigns[_eleccPackTypes[_electronicsSelectedIndex]] = 1;
                    RefreshMass();
                }
                var mass = _eleccPackTypes[_electronicsSelectedIndex].Mass;
                ImGui.Text("Mass: " + Stringify.Mass(mass));
                BorderGroup.EndBoarder();

                ImGui.NewLine();

                BorderGroup.BeginBorder("Engine:");

                if (ImGui.Combo("Engine Designs", ref _engineSelectedIndex, _engineDesigns, _engineDesigns.Length))
                {
                    ComponentDesign engineDesign = _engineTypes[_engineSelectedIndex];
                    _selectedComponentDesigns[engineDesign] = _engineCount;
                    RefreshMass();
                }
                if (ImGui.SliderInt("Engine Count", ref _engineCount, 1, 256))
                {
                    _selectedComponentDesigns[_engineTypes[_engineSelectedIndex]] = _engineCount;
                    RefreshMass();
                }
                var emass = _engineTypes[_engineSelectedIndex].Mass * _engineCount;
                ImGui.Text("Mass: " + Stringify.Mass(emass));
                BorderGroup.EndBoarder();

                ImGui.NewLine();
                ImGui.Text("Total Mass: " + Stringify.Mass(_totalMass));
                var enginedesign = _engineTypes[_engineSelectedIndex];
                var atb          = enginedesign.GetAttribute <NewtonionThrustAtb>();

                double burnRate      = atb.FuelBurnRate * _engineCount;
                double exaustVel     = atb.ExhaustVelocity;
                double thrustNewtons = burnRate * exaustVel;
                double burnTime      = _fuelKG / burnRate;
                double dv            = OrbitMath.TsiolkovskyRocketEquation(_totalMass, _totalMass - _fuelKG, exaustVel);
                ImGui.Text("Burn Time: " + burnTime + "s");
                ImGui.Text("Thrust: " + Stringify.Thrust(thrustNewtons));
                ImGui.Text("DeltaV: " + Stringify.Velocity(dv));

                ImGui.InputText("Design Name", _designName, (uint)_designName.Length);
                NewDesignButton();
            }
        }
Beispiel #15
0
        internal override void Display()
        {
            if (IsActive)
            {
                ImVec2 size = new ImVec2(200, 100);
                ImVec2 pos  = new ImVec2(_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))
                {
                    ImGui.SetTooltip(_tooltipText);
                    ImGui.Text("Target: ");
                    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("OrbitalVelocity: ");
                    ImGui.SameLine();
                    ImGui.Text(_PreciseOrbitalSpeedKm_s.ToString());

                    if (ImGui.Button("Action Order"))
                    {
                        fsm[(byte)CurrentState, (byte)Events.ClickedAction].Invoke();
                    }

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

                    if (_orbitWidget != null)
                    {
                        switch (CurrentState)
                        {
                        case States.NeedsEntity:

                            break;

                        case States.NeedsTarget:

                            break;

                        case States.NeedsInsertionPoint:
                        {
                            var mousePos = ImGui.GetMousePos();

                            var mouseWorldPos = _state.Camera.MouseWorldCoordinate();
                            _targetInsertionPoint_AU = (mouseWorldPos - GetTargetPosition());

                            //var intercept = InterceptCalcs.GetInterceptPosition(OrderingEntity.Entity, TargetEntity.Entity.GetDataBlob<OrbitDB>(), TargetEntity.Entity.Manager.ManagerSubpulses.SystemLocalDateTime);

                            //var distanceAU = (GetTargetPosition() - mouseWorldPos).Length();
                            //var distanceSelectedKM = Distance.AuToKm(distanceAU);
                            //var d1 = Math.Min(_apMax, distanceSelectedKM); //can't be higher than SOI
                            //_apoapsisKm = Math.Max(d1, _peMin); //cant be lower than the body radius

                            //_moveWidget.SetArrivalRadius(distanceAU);
                            _moveWidget.SetArrivalPosition(_targetInsertionPoint_AU);

                            var massCurrBody   = OrderingEntity.Entity.GetDataBlob <OrbitDB>().Parent.GetDataBlob <MassVolumeDB>().Mass;
                            var massTargetBody = TargetEntity.Entity.GetDataBlob <MassVolumeDB>().Mass;
                            var mymass         = OrderingEntity.Entity.GetDataBlob <MassVolumeDB>().Mass;

                            var ralPosCBAU    = OrderingEntity.Entity.GetDataBlob <PositionDB>().RelativePosition_AU;
                            var smaCurrOrbtAU = OrderingEntity.Entity.GetDataBlob <OrbitDB>().SemiMajorAxis;

                            var sgpCBAU = GameConstants.Science.GravitationalConstant * (massCurrBody + mymass) / 3.347928976e33;        // (149597870700 * 149597870700 * 149597870700);
                            var velAU   = OrbitProcessor.PreciseOrbitalVector(sgpCBAU, ralPosCBAU, smaCurrOrbtAU);
                            var sgpTBAU = GameConstants.Science.GravitationalConstant * (massTargetBody + mymass) / 3.347928976e33;

                            var ke = OrbitMath.KeplerFromVelocityAndPosition(sgpTBAU, _targetInsertionPoint_AU, velAU);

                            _PreciseOrbitalSpeedKm_s = Distance.AuToKm(velAU.Length());

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

                        /*
                         * case States.NeedsSecondApsis:
                         * {
                         *   TODO: when we've got newtonion engines, allow second apsis choise 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:
                            break;

                        default:
                            break;
                        }
                    }



                    ImGui.End();
                }
            }
        }
        internal override void Display()
        {
            if (IsActive && ImGui.Begin("Ordnance Design"))
            {
                ImGui.Combo("Current Designs", ref _payloadSelectedIndex, _currentDesignNames, _currentDesignNames.Length);

                ImGui.NewLine();
                BorderGroup.Begin("Payload:");
                if (ImGui.Combo("Payload type", ref _payloadSelectedIndex, _payload, _payload.Length))
                {
                    //_selectedPayload = _payloadTypes[_payloadSelectedIndex].GetAttribute<OrdnancePayloadAtb>();
                    _selectedComponentDesigns[_payloadTypes[_payloadSelectedIndex]] = _payloadCount;
                    RefreshMass();
                }
                if (ImGui.SliderInt("Payload Count", ref _payloadCount, 1, 100))
                {
                    _selectedComponentDesigns[_payloadTypes[_payloadSelectedIndex]] = _payloadCount;
                    RefreshMass();
                }
                var whmass = _payloadTypes[_payloadSelectedIndex].MassPerUnit * _payloadCount;
                ImGui.Text("Mass: " + Stringify.Mass(whmass));
                //ImGui.Text("Payload Trigger Type: " + _selectedPayload.Trigger);
                BorderGroup.End();
                ImGui.NewLine();

                BorderGroup.Begin("Electronics Suite:");
                if (ImGui.Combo("ElectronicsSuite", ref _electronicsSelectedIndex, _electronicsPackage, _electronicsPackage.Length))
                {
                    _selectedComponentDesigns[_eleccPackTypes[_electronicsSelectedIndex]] = 1;
                    RefreshMass();
                }
                var mass = _eleccPackTypes[_electronicsSelectedIndex].MassPerUnit;
                ImGui.Text("Mass: " + Stringify.Mass(mass));
                BorderGroup.End();

                ImGui.NewLine();

                BorderGroup.Begin("Engine:");

                if (ImGui.Combo("Engine Types", ref _engineDesignTypeIndex, _engineTypeNames, _engineTypeNames.Length))
                {
                    _engineDesigner = new ComponentDesigner(_engineTemplates[_engineDesignTypeIndex], _factionTech);
                    _engineDesigner.SetAttributes();
                }

                if (_engineDesigner != null)
                {
                    PartDesignUI.GuiDesignUI(_engineDesigner);
                    _thrusterSizeKG = _engineDesigner.MassValue;
                    RefreshMass();
                }

                if (ImGui.SliderFloat("Fuel", ref _fuelKG, 1, 1000))
                {
                    RefreshMass();
                }

                BorderGroup.End();

                ImGui.NewLine();
                ImGui.Text("Total Mass: " + Stringify.Mass(_totalMass));

                double burnRate  = 0;
                double exaustVel = 0;

                if (_engineDesigner != null)
                {
                    var atb = _engineDesigner.GetAttribute <NewtonionThrustAtb>();
                    burnRate  = atb.FuelBurnRate;
                    exaustVel = atb.ExhaustVelocity;
                }

                double thrustNewtons = burnRate * exaustVel;
                double burnTime      = _fuelKG / burnRate;
                double dv            = OrbitMath.TsiolkovskyRocketEquation(_totalMass, _totalMass - _fuelKG, exaustVel);
                ImGui.Text("Burn Time: " + burnTime + "s");
                ImGui.Text("Thrust: " + Stringify.Thrust(thrustNewtons));
                ImGui.Text("Acceleration (wet): " + Stringify.Velocity(thrustNewtons / _totalMass));
                ImGui.Text("Acceleration (dry): " + Stringify.Velocity(thrustNewtons / (_totalMass - _fuelKG)));
                ImGui.Text("DeltaV: " + Stringify.Velocity(dv));

                ImGui.InputText("Design Name", _designName, (uint)_designName.Length);
                NewDesignButton();
            }
        }
 public virtual float GetMass()
 {
     mass = OrbitMath.MassOfCircle(radius, massScale);
     return(mass);
 }
 public Vector2 GravityForce(Vector2 position, float mass = 1)
 {
     return(OrbitMath.GravityForce(position, transform.position, this.mass, mass));
 }
Beispiel #19
0
        public OrbitalDebugWidget(EntityState entityState) : base(entityState.OrbitIcon.BodyPositionDB)
        {
            var orbitIcon = entityState.OrbitIcon;

            _bodyPosition = orbitIcon.BodyPositionDB;

            _orbitDB = entityState.Entity.GetDataBlob <OrbitDB>();
            if (_orbitDB.Parent == null) //primary star
            {
                _positionDB = orbitIcon.BodyPositionDB;
            }
            else
            {
                _positionDB = _orbitDB.Parent.GetDataBlob <PositionDB>(); //orbit's position is parent's body position.
            }



            EntityGuid = entityState.Entity.Guid;

            _loan = Angle.ToRadians(_orbitDB.LongitudeOfAscendingNode);
            _aop  = Angle.ToRadians(_orbitDB.ArgumentOfPeriapsis);
            _loP  = orbitIcon._loP_radians;

            var cP = new PointD()
            {
                X = orbitIcon.WorldPosition.X, Y = orbitIcon.WorldPosition.Y
            };

            cP.X -= orbitIcon._linearEccentricity;

            var f1 = new PointD()
            {
                X = cP.X + orbitIcon._linearEccentricity, Y = cP.Y
            };
            var f2 = new PointD()
            {
                X = cP.X - orbitIcon._linearEccentricity, Y = cP.Y
            };
            var coVertex = new PointD()
            {
                X = cP.X, Y = cP.Y + orbitIcon.SemiMinor
            };
            var periapsisPnt = new PointD()
            {
                X = cP.X - orbitIcon.SemiMaj, Y = cP.Y
            };
            var apoapsisPnt = new PointD()
            {
                X = cP.X + orbitIcon.SemiMaj, Y = cP.Y
            };

            _cP           = DrawTools.RotatePoint(cP, _loP);
            _f1           = DrawTools.RotatePoint(f1, _loP);
            _f2           = DrawTools.RotatePoint(f2, _loP);
            _coVertex     = DrawTools.RotatePoint(coVertex, _loP);
            _periapsisPnt = DrawTools.RotatePoint(periapsisPnt, _loP);
            _apoapsisPnt  = DrawTools.RotatePoint(apoapsisPnt, _loP);


            _semiMajAxis = orbitIcon.SemiMaj;
            _semiMinAxis = orbitIcon.SemiMinor;

            DateTime systemDateTime = _orbitDB.Parent.Manager.ManagerSubpulses.StarSysDateTime;

            _trueAnom = OrbitProcessor.GetTrueAnomaly(_orbitDB, systemDateTime);
            double secondsFromEpoch = (systemDateTime - _orbitDB.Epoch).TotalSeconds;

            _meanAnom = OrbitMath.GetMeanAnomalyFromTime(Angle.ToRadians(_orbitDB.MeanAnomalyAtEpoch), Angle.ToRadians(_orbitDB.MeanMotion), secondsFromEpoch);

            _eccentricAnom = OrbitProcessor.GetEccentricAnomaly(_orbitDB, _meanAnom);
            _bodyPosPnt    = new PointD()
            {
                X = (_bodyPosition.AbsolutePosition_AU + _worldPosition).X,
                Y = (_bodyPosition.AbsolutePosition_AU + _worldPosition).Y
            };
            CreateLines();
        }
Beispiel #20
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();
                }
            }
        }
Beispiel #21
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);
        }
    public float GetCenterMass()
    {
        float centerMass = OrbitMath.MassOfCircle(radius, massScale);

        return(centerMass);
    }
Beispiel #23
0
        public void TestIntercept()
        {
            double        myMass     = 10000;
            double        parentMass = 1.989e30; //solar mass.
            Game          game       = new Game();
            EntityManager mgr        = new EntityManager(game, false);

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

            Vector3 currentPos_m = new Vector3 {
                X = Distance.AuToMt(-0.77473184638034), Y = Distance.AuToMt(0.967145228951685)
            };
            Vector3 currentVelocity_m = new Vector3 {
                Y = Distance.KmToM(40)
            };
            double nonNewtSpeed_m = Distance.KmToM(283.018);

            Vector3 targetObjPosition = new Vector3 {
                X = Distance.AuToMt(0.149246434443459), Y = Distance.AuToMt(-0.712107888348067)
            };
            Vector3 targetObjVelocity = new Vector3 {
                Y = Distance.KmToM(35)
            };


            double sgp_m = OrbitMath.CalculateStandardGravityParameterInM3S2(myMass, parentMass);
            //KeplerElements ke = OrbitMath.KeplerFromVelocityAndPosition(sgp, targetObjPosition, targetObjVelocity);

            var currentDateTime = new DateTime(2000, 1, 1);

            OrbitDB targetOrbit = OrbitDB.FromVector(parentEntity, myMass, parentMass, sgp_m, targetObjPosition, targetObjVelocity, currentDateTime);



            var intercept_m = OrbitMath.GetInterceptPosition_m(currentPos_m, nonNewtSpeed_m, targetOrbit, currentDateTime);

            var futurePos1_m = OrbitProcessor.GetAbsolutePosition_m(targetOrbit, intercept_m.Item2);

            var futurePos2_m = intercept_m.Item1;



            Assert.AreEqual(futurePos1_m.Length(), futurePos2_m.Length(), 0.01);
            Assert.AreEqual(futurePos1_m.X, futurePos2_m.X, 0.01);
            Assert.AreEqual(futurePos1_m.Y, futurePos2_m.Y, 0.01);
            Assert.AreEqual(futurePos1_m.Z, futurePos2_m.Z, 0.01);
            var time       = intercept_m.Item2 - currentDateTime;
            var distance_m = (currentPos_m - intercept_m.Item1).Length();
            var speed      = distance_m / time.TotalSeconds;
            var distb_m    = nonNewtSpeed_m * time.TotalSeconds;

            var timeb = distance_m / nonNewtSpeed_m;

            Assert.AreEqual(nonNewtSpeed_m, speed, 1.0e-4);

            var dif = distance_m - distb_m;

            Assert.AreEqual(distance_m, distb_m, 100.0, "Out by a difference of " + dif + " meters");
        }
Beispiel #24
0
        internal void CreatePointArray()
        {
            _dv = _newtonMoveDB.DeltaVForManuver_m.Length();
            Vector3 vel          = Distance.MToAU(_newtonMoveDB.CurrentVector_ms);
            Vector3 pos          = myPosDB.RelativePosition_AU;
            Vector3 eccentVector = OrbitMath.EccentricityVector(_sgp, pos, vel);
            double  e            = eccentVector.Length();
            double  r            = pos.Length();
            double  v            = vel.Length();
            double  a            = 1 / (2 / r - Math.Pow(v, 2) / _sgp); //semiMajor Axis
            double  b            = -a *Math.Sqrt(Math.Pow(e, 2) - 1);   //semiMinor Axis

            double linierEccentricity = e * a;
            double soi = OrbitProcessor.GetSOI_AU(_newtonMoveDB.SOIParent);

            //longditudeOfPeriapsis;
            double _lop = Math.Atan2(eccentVector.Y, eccentVector.X);

            if (Vector3.Cross(pos, vel).Z < 0) //anti clockwise orbit
            {
                _lop = Math.PI * 2 - _lop;
            }

            double p = EllipseMath.SemiLatusRectum(a, e);
            double angleToSOIPoint = Math.Abs(OrbitMath.AngleAtRadus(soi, p, e));
            //double thetaMax = angleToSOIPoint;

            double maxX = soi * Math.Cos(angleToSOIPoint);
            //maxX = maxX - a + linierEccentricity;
            double foo      = maxX / a;
            double thetaMax = Math.Log(foo + Math.Sqrt(foo * foo - 1));


            if (_numberOfPoints % 2 == 0)
            {
                _numberOfPoints += 1;
            }
            int    ctrIndex = _numberOfPoints / 2;
            double dtheta   = thetaMax / (ctrIndex - 1);
            double fooA     = Math.Cosh(dtheta);
            double fooB     = (a / b) * Math.Sinh(dtheta);
            double fooC     = (b / a) * Math.Sinh(dtheta);
            double xn       = a;
            double yn       = 0;

            var points = new PointD[ctrIndex + 1];

            points[0] = new PointD()
            {
                X = xn, Y = yn
            };
            for (int i = 1; i < ctrIndex + 1; i++)
            {
                var lastx = xn;
                var lasty = yn;
                xn        = fooA * lastx + fooB * lasty;
                yn        = fooC * lastx + fooA * lasty;
                points[i] = new PointD()
                {
                    X = xn, Y = yn
                };
            }


            _points           = new PointD[_numberOfPoints];
            _points[ctrIndex] = new PointD()
            {
                X = ((points[0].X - linierEccentricity) * Math.Cos(_lop)) - (points[0].Y * Math.Sin(_lop)),
                Y = ((points[0].X - linierEccentricity) * Math.Sin(_lop)) + (points[0].Y * Math.Cos(_lop))
            };
            for (int i = 1; i < ctrIndex + 1; i++)
            {
                double x   = points[i].X - linierEccentricity; //adjust for the focal point
                double ya  = points[i].Y;
                double yb  = -points[i].Y;
                double x2a = (x * Math.Cos(_lop)) - (ya * Math.Sin(_lop)); //rotate to loan
                double y2a = (x * Math.Sin(_lop)) + (ya * Math.Cos(_lop));
                double x2b = (x * Math.Cos(_lop)) - (yb * Math.Sin(_lop));
                double y2b = (x * Math.Sin(_lop)) + (yb * Math.Cos(_lop));
                _points[ctrIndex + i] = new PointD()
                {
                    X = x2a,
                    Y = y2a
                };

                _points[ctrIndex - i] = new PointD()
                {
                    X = x2b,
                    Y = y2b
                };
            }
        }
Beispiel #25
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();
                }
            }
        }