private void UpdateDifficulty()
{
/* A new difficulty level means stronger/weaker gravity. For most world objects this effect
* is being handled in UpdateGravity(), but there are planets which circle in the orbit of other
* planets and their velocities need to be readjusted to prevent these planets from leaving the orbit.
* */
foreach (Planet planet in world.Planets)
{
if (planet.IsFlexible)
{
// find the planet that this one is most attracted to
// default values
float highestAcceleration = 0;
Planet mostAttractivePlanet = planet;
Vector2 distance = new Vector2(1, 1);
foreach (Planet planet2 in world.Planets)
{
Vector2 distanceToPlanet = new Vector2(planet2.Position.X - planet.Position.X, planet2.Position.Y - planet.Position.Y);
if (distanceToPlanet.Length() != 0)
{
float acceleration = ((float)(GameAssets.G * planet2.Mass / distanceToPlanet.LengthSquared()));
if (acceleration > highestAcceleration)
{
highestAcceleration = acceleration;
mostAttractivePlanet = planet2;
distance = distanceToPlanet;
}
}
}
// calculate the velocity needed for orbiting this planet with the given distance.
float planetVelocity = (float) Math.Sqrt(mostAttractivePlanet.Mass * GameAssets.G / distance.Length() / (GameAssets.N * 1000 * difficulty));
distance.Normalize();
Vector2 newVelocity = new Vector2(distance.Y, -distance.X);
planet.Velocity = Vector2.Multiply(newVelocity, planetVelocity);
}
}
}