Given a set of n particles in 2-dimensional space, where each particle experiences an attractive force towards all other particles in the set, proportional to the inverse square of the distances between them, can you imagine any clever methods of calculating the net force experienced by each particle, without performing n^2 distance calculations?
From the Particle class of my program:
public void calculateAttractions(List<Particle> entities)
{
foreach (Particle p in entities)
{
if (!p.Equals(this))
{
Vector2 dx = this.Position - p.Position;
this.ApplyForce(-dx / dx.LengthSquared());
}
}
}
The brute-force method can only simulate ~150 particles in my program (using the XNA framework and Farseer physics engine) before running into performance issues.
If there is no other way, how might this code be optimized, or else how might I cheat? I've tried randomizing the call to calculateAttractions() on a particle-to-particle basis, such that at any given time only around 1/3 of the particles are actually being updated. This resulted in some improvement in performance, but at the cost of accuracy, of course.
What I'm trying to do is similar to gravitational force calculations. Someone in this thread suggested working with complex numbers and using the equation:
(z-z')/abs(z-z')**3
However, I don't know what z refers to and I cant find reference to this equation anywhere else.
EDIT: Though I've accepted an answer below, I ended up using a solution that nobody mentioned: a look-up table. Basically, I pre-calculate the force exerted on one particle by another particle at all distances (with a resolution of 1 pixel) up to 2000 pixels away in any direction. Forces can then be determined without any runtime distance calculations. Though it's not an exact solution, the distortion is not perceivable and it has allowed me to increase the number of simulation particles three-fold (to the point where the number is now limited by the physics engine and not by the n-body problem.)