I am currently working on a two-dimensional space simulation game. Every object will have a gravitational force that pulls on everything else. The issue I have run into is finding an efficient way for every object to find the position of every other object so that its gravity may act on those that are within its gravitational pull. My first thought was to put all objects into an ArrayList and looping through all objects in the ArrayList for every object. Obviously this would be extremely slow, so I didn’t even try to do this. What would you guys suggest as an optimal way to do this?
In layman’s terms:
- Many objects with x and y position
- Each object needs to find x and y position of all other objects in order to check if their x and y coordinates are within a certain parameter