Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:24:12+00:00 2026-06-17T20:24:12+00:00

Given a set of n particles in 2-dimensional space, where each particle experiences an

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:24:13+00:00Added an answer on June 17, 2026 at 8:24 pm
    1. First up, the force is symmetric so you only need n squared divided by 2 calculations.

    2. Second up the force falls off with the distance squared, so you can approximate this by setting a threshold beyond which there is no need to calculate. You only need check this in 1 dimension. As in, if x – x’ < t and y – y’ < t calculate, otherwise no.

    3. You can use two occupancy trees to put the particles into little squares, or buckets, and only do k squared divided by 2 distance calculations for the pairs in each bucket in one tree. The other tree is translated up and to the left, to take care of any pairs on the bounds of adjacent squares. For the second tree just don’t repeat the distance calcs you did in the first tree.

    4. Between updates it is possible that some particles may not change their position, therefor you do not needn’t calculate again any forces that involve two particles stationary since te last update.

    5. One may assume the masses also contribute. A further optimization is if the mass is too small then the threshold t beyond which to not calculate can shrink, too.

    6. You may also want to quantize the translations of the particles so that any move below the minimum is not registered as a change in coordinates .

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a set of phrases, i would like to filter the set of all
Is there any way of automating a site publish with a given set of
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
This is one of the solution of getting true or false from given set
Given a set of questions that have linked survey and category id: > db.questions.find().toArray();
Given a set Paris New York London New York Paris Paris I'm trying to
Given a set of real numbers drawn from a unknown continuous univariate distribution (let's
Given a set of java regular expression patterns separated by an OR (i.e |
For a given entity set, say, WHO_TYPES, I want to limit the columns to
First the practical application that led me to the problem: Given a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.