/// <summary> /// Creates on Orbit at current location from a given velocity /// </summary> /// <returns>The Orbit Does not attach the OrbitDB to the entity!</returns> /// <param name="parent">Parent.</param> /// <param name="entity">Entity.</param> /// <param name="velocityAU">Velocity.</param> public static OrbitDB FromVector(Entity parent, Entity entity, Vector3 velocityAU, DateTime atDateTime) { var parentMass = parent.GetDataBlob <MassVolumeDB>().Mass; var myMass = entity.GetDataBlob <MassVolumeDB>().Mass; var epoch1 = parent.Manager.ManagerSubpulses.StarSysDateTime; //getting epoch from here is incorrect as the local datetime doesn't change till after the subpulse. var parentPos = OrbitProcessor.GetAbsolutePosition_AU(parent.GetDataBlob <OrbitDB>(), atDateTime); //need to use the parent position at the epoch var posdb = entity.GetDataBlob <PositionDB>(); posdb.SetParent(parent); var ralitivePos = posdb.RelativePosition_AU;//entity.GetDataBlob<PositionDB>().AbsolutePosition_AU - parentPos; if (ralitivePos.Length() > OrbitProcessor.GetSOI(parent)) { throw new Exception("Entity not in target SOI"); } var sgp = GameConstants.Science.GravitationalConstant * (myMass + parentMass) / 3.347928976e33; var ke = OrbitMath.KeplerFromPositionAndVelocity(sgp, ralitivePos, velocityAU, atDateTime); var epoch = atDateTime;// - TimeSpan.FromSeconds(ke.Epoch); //ke.Epoch is seconds from periapsis. OrbitDB orbit = new OrbitDB(parent, parentMass, myMass, Math.Abs(ke.SemiMajorAxis), ke.Eccentricity, Angle.ToDegrees(ke.Inclination), Angle.ToDegrees(ke.LoAN), Angle.ToDegrees(ke.AoP), Angle.ToDegrees(ke.MeanAnomalyAtEpoch), epoch); var pos = OrbitProcessor.GetPosition_AU(orbit, atDateTime); var d = Distance.AuToKm(pos - ralitivePos).Length(); if (d > 1) { var e = new Event(atDateTime, "Positional difference of " + d + "Km when creating orbit from velocity"); e.Entity = entity; e.SystemGuid = entity.Manager.ManagerGuid; e.EventType = EventType.Opps; //e.Faction = entity.FactionOwner; StaticRefLib.EventLog.AddEvent(e); //other info: var keta = Angle.ToDegrees(ke.TrueAnomalyAtEpoch); var obta = Angle.ToDegrees(OrbitProcessor.GetTrueAnomaly(orbit, atDateTime)); var tadif = Angle.ToDegrees(Angle.DifferenceBetweenRadians(keta, obta)); var pos1 = OrbitProcessor.GetPosition_AU(orbit, atDateTime); var pos2 = OrbitProcessor.GetPosition_AU(orbit, ke.TrueAnomalyAtEpoch); var d2 = Distance.AuToKm(pos1 - pos2).Length(); } return(orbit); }
public static OrbitDB FromVectorKM(Entity parent, double myMass, double parentMass, double sgp, Vector3 position, Vector3 velocity, DateTime atDateTime) { if (Distance.KmToAU(position.Length()) > OrbitProcessor.GetSOI(parent)) { throw new Exception("Entity not in target SOI"); } //var sgp = GameConstants.Science.GravitationalConstant * (myMass + parentMass) / 3.347928976e33; var ke = OrbitMath.KeplerFromPositionAndVelocity(sgp, position, velocity, atDateTime); OrbitDB orbit = new OrbitDB(parent, parentMass, myMass, Distance.KmToAU(ke.SemiMajorAxis), ke.Eccentricity, Angle.ToDegrees(ke.Inclination), Angle.ToDegrees(ke.LoAN), Angle.ToDegrees(ke.AoP), Angle.ToDegrees(ke.MeanAnomalyAtEpoch), atDateTime);// - TimeSpan.FromSeconds(ke.Epoch)); var pos = OrbitProcessor.GetAbsolutePosition_AU(orbit, atDateTime); return(orbit); }
void SetOrbitHere(Entity entity, PropulsionAbilityDB propulsionDB, PositionDB positionDB, TranslateMoveDB moveDB, DateTime atDateTime) { propulsionDB.CurrentVectorMS = new Vector3(0, 0, 0); double targetSOI = OrbitProcessor.GetSOI(moveDB.TargetEntity); Entity targetEntity; if (moveDB.TargetEntity.GetDataBlob <PositionDB>().GetDistanceTo(positionDB) > targetSOI) { targetEntity = moveDB.TargetEntity.GetDataBlob <OrbitDB>().Parent; //TODO: it's concevable we could be in another SOI not the parent (ie we could be in a target's moon's SOI) } else { targetEntity = moveDB.TargetEntity; } OrbitDB targetOrbit = targetEntity.GetDataBlob <OrbitDB>(); var orbitalVector = OrbitProcessor.GetOrbitalVector(targetOrbit, atDateTime); var insertionVector2d = OrbitProcessor.GetOrbitalInsertionVector(moveDB.SavedNewtonionVector_AU, targetOrbit, atDateTime); Vector3 parentOrbitalVector = new Vector3(orbitalVector.X, orbitalVector.Y, 0); Vector3 insertionVector = new Vector3(insertionVector2d.X, insertionVector2d.Y, 0); insertionVector += moveDB.ExpendDeltaV_AU; //TODO: only use it if we have it. propulsionDB.RemainingDV_MS -= (float)Distance.AuToMt(moveDB.ExpendDeltaV_AU).Length(); OrbitDB newOrbit = OrbitDB.FromVector(targetEntity, entity, insertionVector, atDateTime); if (newOrbit.Periapsis > targetSOI) { //TODO: find who's SOI we're currently in and create an orbit for that; } if (newOrbit.Apoapsis > targetSOI) { //TODO: change orbit to new parent at SOI change } positionDB.SetParent(targetEntity); moveDB.IsAtTarget = true; entity.RemoveDataBlob <TranslateMoveDB>(); entity.SetDataBlob(newOrbit); newOrbit.SetParent(targetEntity); }
/// <summary> /// This was designed so that fast moving objects will get interpolated a lot more than slow moving objects /// so fast moving objects shouldn't loose positional acuracy when close to a planet, /// and slow moving objects won't have processor time wasted on them by calulcating too often. /// However this seems to be unstable and looses energy, unsure why. currently set it to just itterate/interpolate every second. /// so currently will be using more time to get through this than neccisary. /// </summary> /// <param name="entity">Entity.</param> /// <param name="deltaSeconds">Delta seconds.</param> public static void NewtonMove(Entity entity, int deltaSeconds) { NewtonMoveDB newtonMoveDB = entity.GetDataBlob <NewtonMoveDB>(); PositionDB positionDB = entity.GetDataBlob <PositionDB>(); double Mass_Kg = entity.GetDataBlob <MassVolumeDB>().Mass; double ParentMass_kg = newtonMoveDB.ParentMass; var manager = entity.Manager; DateTime dateTimeFrom = newtonMoveDB.LastProcessDateTime; DateTime dateTimeNow = manager.ManagerSubpulses.StarSysDateTime; DateTime dateTimeFuture = dateTimeNow + TimeSpan.FromSeconds(deltaSeconds); double deltaT = (dateTimeFuture - dateTimeFrom).TotalSeconds; double secondsToItterate = deltaT; while (secondsToItterate > 0) { double speed_kms = newtonMoveDB.CurrentVector_kms.Length(); //double timeStep = Math.Max(secondsToItterate / speed_kms, 1); //timeStep = Math.Min(timeStep, secondsToItterate); double timeStep = 1;//because the above seems unstable and looses energy. double distanceToParent_m = Distance.AuToMt(positionDB.GetDistanceTo(newtonMoveDB.SOIParent.GetDataBlob <PositionDB>())); distanceToParent_m = Math.Max(distanceToParent_m, 0.1); //don't let the distance be 0 (once collision is in this will likely never happen anyway) double gravForce = GameConstants.Science.GravitationalConstant * (Mass_Kg * ParentMass_kg / Math.Pow(distanceToParent_m, 2)); Vector3 gravForceVector = gravForce * -Vector3.Normalise(positionDB.RelativePosition_AU); double distance = Distance.AuToKm(positionDB.RelativePosition_AU).Length(); Vector3 totalForce = gravForceVector + newtonMoveDB.ThrustVector; Vector3 acceleration_mps = totalForce / Mass_Kg; Vector3 newVelocity = (acceleration_mps * timeStep * 0.001) + newtonMoveDB.CurrentVector_kms; newtonMoveDB.CurrentVector_kms = newVelocity; Vector3 deltaPos = (newtonMoveDB.CurrentVector_kms + newVelocity) / 2 * timeStep; //Vector4 deltaPos = newtonMoveDB.CurrentVector_kms * timeStep; positionDB.RelativePosition_AU += Distance.KmToAU(deltaPos); double sOIRadius_AU = OrbitProcessor.GetSOI(newtonMoveDB.SOIParent); if (positionDB.RelativePosition_AU.Length() >= sOIRadius_AU) { Entity newParent; Vector3 parentRalitiveVector; //if our parent is a regular kepler object (normaly this is the case) if (newtonMoveDB.SOIParent.HasDataBlob <OrbitDB>()) { var orbitDB = newtonMoveDB.SOIParent.GetDataBlob <OrbitDB>(); newParent = orbitDB.Parent; var parentVelocity = OrbitProcessor.InstantaneousOrbitalVelocityVector(orbitDB, entity.Manager.ManagerSubpulses.StarSysDateTime); parentRalitiveVector = Distance.KmToAU(newtonMoveDB.CurrentVector_kms) + parentVelocity; var pvlen = Distance.AuToKm(parentVelocity.Length()); var vlen = newtonMoveDB.CurrentVector_kms.Length(); var rvlen = Distance.AuToKm(parentRalitiveVector.Length()); } else //if (newtonMoveDB.SOIParent.HasDataBlob<NewtonMoveDB>()) { //this will pretty much never happen. newParent = newtonMoveDB.SOIParent.GetDataBlob <NewtonMoveDB>().SOIParent; var parentVelocity = newtonMoveDB.SOIParent.GetDataBlob <NewtonMoveDB>().CurrentVector_kms; parentRalitiveVector = Distance.KmToAU(newtonMoveDB.CurrentVector_kms + parentVelocity); } double newParentMass = newParent.GetDataBlob <MassVolumeDB>().Mass; double sgp = GameConstants.Science.GravitationalConstant * (newParentMass + Mass_Kg) / 3.347928976e33; Vector3 posRalitiveToNewParent = positionDB.AbsolutePosition_AU - newParent.GetDataBlob <PositionDB>().AbsolutePosition_AU; var dateTime = dateTimeNow + TimeSpan.FromSeconds(deltaSeconds - secondsToItterate); var kE = OrbitMath.KeplerFromPositionAndVelocity(sgp, posRalitiveToNewParent, parentRalitiveVector, dateTime); if (kE.Eccentricity < 1) //if we're going to end up in a regular orbit around our new parent { /* * var newOrbit = OrbitDB.FromKeplerElements( * newParent, * newParentMass, * Mass_Kg, * kE, * dateTime); */ var newOrbit = OrbitDB.FromVector(newParent, entity, parentRalitiveVector, dateTime); entity.RemoveDataBlob <NewtonMoveDB>(); entity.SetDataBlob(newOrbit); positionDB.SetParent(newParent); var currentPos = Distance.AuToKm(positionDB.RelativePosition_AU); var newPos = OrbitProcessor.GetPosition_AU(newOrbit, dateTime); var newPosKM = Distance.AuToKm(newPos); positionDB.RelativePosition_AU = newPos; break; } else //else we're in a hyperbolic trajectory around our new parent, so just coninue the newtonion move. { positionDB.SetParent(newParent); newtonMoveDB.ParentMass = newParentMass; newtonMoveDB.SOIParent = newParent; } } secondsToItterate -= timeStep; } newtonMoveDB.LastProcessDateTime = dateTimeFuture; }