/// <summary> /// Returns the planet that has the most influential gravity well in the given world point. /// The most influential gravity well is defined as the planet that has the highest gravity in the point and /// if no such planet is found, it returns the planet, whose gravity well is the closest to the given point. /// </summary> /// <param name="worldPosition">Position to test for the strongest gravity well</param> /// <returns>Planet that has the most influential gravity well in the given world point</returns> public static MyPlanet GetStrongestGravityWell(Vector3D worldPosition) { MyPlanet nearestPlanet = null; double maxMetricValue = double.MinValue; foreach (IMyGravityProvider gravityProvider in m_planetGenerators) { MyPlanet planet = gravityProvider as MyPlanet; if (planet == null) { continue; } double planetDistance = (planet.PositionComp.GetPosition() - worldPosition).Length(); double metricValue = double.MinValue; if (planetDistance <= planet.GravityLimit) { metricValue = (double)planet.GetGravityMultiplier(worldPosition); } else { // Outside of the gravity well, just invert the distance, so that we can use only one value // for comparison of planets which affect us and which don't metricValue = planet.GravityLimit - planetDistance; } if (metricValue > maxMetricValue) { nearestPlanet = planet; maxMetricValue = metricValue; } } return(nearestPlanet); }