private static void ApplyGravityZ(Moon moon1, Moon moon2) { if (moon1.Position.Z > moon2.Position.Z) { moon1.Velocity.Z--; moon2.Velocity.Z++; return; } if (moon1.Position.Z < moon2.Position.Z) { moon1.Velocity.Z++; moon2.Velocity.Z--; return; } }
private static void ApplyGravityX(Moon moon1, Moon moon2) { if (moon1.Position.X > moon2.Position.X) { moon1.Velocity.X--; moon2.Velocity.X++; return; } if (moon1.Position.X < moon2.Position.X) { moon1.Velocity.X++; moon2.Velocity.X--; return; } }
private static void ApplyGravityY(Moon moon1, Moon moon2) { if (moon1.Position.Y > moon2.Position.Y) { moon1.Velocity.Y--; moon2.Velocity.Y++; return; } if (moon1.Position.Y < moon2.Position.Y) { moon1.Velocity.Y++; moon2.Velocity.Y--; return; } }
private static void Step(List <Moon> moons) { for (int i = 0; i < moons.Count; i++) { // Only apply to moons that are ahead on the list for (int j = i + 1; j < moons.Count; j++) { Moon moon1 = moons[i]; Moon moon2 = moons[j]; ApplyGravityX(moon1, moon2); ApplyGravityY(moon1, moon2); ApplyGravityZ(moon1, moon2); } } ApplyVelocity(moons); }