/// <summary> /// Generates a random orbit in as similar a manner to stock as possible. /// </summary> /// <returns>The orbit of a randomly selected member of the population.</returns> /// /// <exception cref="InvalidOperationException">Thrown if cannot produce stockalike /// orbits. The program will be in a consistent state in the event of an exception.</exception> public Orbit drawOrbit() { CelestialBody kerbin = FlightGlobals.Bodies.Find(body => body.isHomeWorld); CelestialBody dres = FlightGlobals.Bodies.Find(body => body.name.Equals("Dres")); if (dres != null && reachedBody(dres) && UnityEngine.Random.Range(0, 4) == 0) { // Drestroids double a = RandomDist.drawLogUniform(0.55, 0.65) * dres.sphereOfInfluence; double e = RandomDist.drawRayleigh(0.005); double i = RandomDist.drawRayleigh(0.005); // lAn takes care of negative inclinations double lAn = RandomDist.drawAngle(); double aPe = RandomDist.drawAngle(); double mEp = Math.PI / 180.0 * RandomDist.drawAngle(); double epoch = Planetarium.GetUniversalTime(); Debug.Log("[CustomAsteroids]: " + Localizer.Format("#autoLOC_CustomAsteroids_LogOrbit", a, e, i, aPe, lAn, mEp, epoch)); return(new Orbit(i, e, a, lAn, aPe, mEp, epoch, dres)); } if (kerbin != null) { // Kerbin interceptors double delay = RandomDist.drawUniform(12.5, 55.0); Debug.Log("[CustomAsteroids]: " + Localizer.Format("#autoLOC_CustomAsteroids_LogDefault", delay)); return(Orbit.CreateRandomOrbitFlyBy(kerbin, delay)); } throw new InvalidOperationException( Localizer.Format("#autoLOC_CustomAsteroids_ErrorDefaultNoKerbin")); }
/// <summary> /// Creates a hyperbolic orbit around the given celestial body. /// </summary> /// <returns>The newly created orbit, with state vectors anchored to its periapsis.</returns> /// <param name="body">The celestial body at the focus of the orbit.</param> /// <param name="periapsis">The periapsis (from the body center), in meters. Must not be /// negative.</param> /// <param name="vInf">The excess speed associated with the orbit, in meters per second. /// Must be positive.</param> /// <param name="utPeri">The absolute time of periapsis passage.</param> /// /// <exception cref="ArgumentException">Thrown if either <c>periapsis</c> or <c>vInf</c> /// are out of bounds.</exception> static Orbit createHyperbolicOrbit (CelestialBody body, double periapsis, double vInf, double utPeri) { if (vInf <= 0.0) { throw new ArgumentException ( Localizer.Format ("#autoLOC_CustomAsteroids_ErrorHyperBadVInf", vInf), nameof (vInf)); } if (periapsis < 0.0) { throw new ArgumentException ( Localizer.Format ("#autoLOC_CustomAsteroids_ErrorHyperBadPeri", periapsis), nameof (periapsis)); } double a = -body.gravParameter / (vInf * vInf); double e = 1.0 - periapsis / a; // Random orientation, to be consistent with CreateRandomOrbitFlyBy double i = RandomDist.drawIsotropic (); double lAn = RandomDist.drawAngle (); double aPe = RandomDist.drawAngle (); Debug.Log ($"[CustomAsteroids]: " + Localizer.Format ("#autoLOC_CustomAsteroids_LogHyperOrbit", body.bodyName, a, e, i, aPe, lAn)); return new Orbit (i, e, a, lAn, aPe, 0.0, utPeri, body); }