for (int i = 0; i < Entities.Count; i++)
{
Entity n = Entities[i];
n.Tick((float)e.Time);
for (int j = 0; j < Entities.Count; j++)
{
Entity a = Entities[j];
if (n != a)
{
Vector2 distance = a.Position - n.Position;
if (distance.LengthSquared <= (a.Size + n.Size) * (a.Size + n.Size))
{
Entities.RemoveAt(j);
j--;
n.Size = (float)Math.Pow(a.Mass + n.Mass, 1.0 / 3.0);
n.Mass += a.Mass;
n.Position = (n.Position * n.Mass + a.Position * a.Mass) / (n.Mass + a.Mass);
n.Velocity += a.Velocity;
}
else
{
n.Velocity = a.Mass * distance / distance.LengthSquared;
}
}
}
}