Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Looking at optimising multiple force interaction

Discussion in 'Scripting' started by Exsert, Dec 30, 2015.

  1. Exsert

    Exsert

    Joined:
    Oct 22, 2015
    Posts:
    42
    Right so i'm working on a solar system simulation

    It works fine at the moment planets interact with each other just fine and everything, it just feels sluggish and a bit slow.

    For instance i wanted to show how accretion would work, but sadly around 150 objects it starts to delay, for obvious reasons ofcourse.

    So how it works is you have one planet, it iterates through all the other bodies and finally sums a direction vector with force.

    But if it's 150 planets each planet loops 149 times.
    that's 22,350 calculations done in a frame...

    without having a range limit or range for amount of mass i'm struggling to optimise this.

    I would do enumeration but that would lead to inaccuracy's in the code.

    Please any resources or advice to overcome this issue would be appreciated.
     
  2. kietus

    kietus

    Joined:
    Jun 4, 2013
    Posts:
    54
    Hello,
    Maybe you can use a datastructure to store your planets.
    If each planet need data from all the other (not only the nearest ones) and that's not possible to cache or average some data, you can use multi threading to speed up the process.
     
    Exsert likes this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    When you say it iterates through all the other objects, does it have to? As I understand it forces in space are measured by mass (gravity) and distance. So each object would have a relative sphere of influence, and only those within those spheres would be influenced?

    Furthermore it may be an idea to have a manager which passes the data out to all the objects, rather than the objects pushing out their own Update() function. As the Update function itself uses more processing than a single manager calling a script on all it's children.

    I don't want to sound condescending as you may already have all this in, but do you have any mass based sphere's of influence between objects.

    Lastly it may be an idea to run from your own custom simplified physics engine, if you want code to run really simple :).

    Just some random ideas for you.
     
    Exsert likes this.
  4. Exsert

    Exsert

    Joined:
    Oct 22, 2015
    Posts:
    42
    Hello Kietus, i was thinking about using an Octree earlier today, i was worried that it might either loose a lot of accuracy or that it will have to be subdivided so many times it might as well be using my current method.

    I will be testing out using Octree to test the accuracy differences over the next few days.

    I will definitely have to investigate using multi-threading as if i get it working correctly that should definitely speed it up, i just heard horror stories about multi-threading in unity.

    Thank you for your advice i will definitely test it out :)

    No questions are condescending as i haven't exactly described how it's working properly.

    So, i have a manager, it contains pointers to each planet, and for each planet it needs to work out the net force acting on that planet then applies it to the planet.

    I did have a sphere of influence working for mass of an object for a while but i realised that it causes a few issues in 3D space where everything is moving at different velocities, and if one planet looses all influences on itself it would continue to move at the velocity forever.

    And the physics in there is my physics, i've not used any of unity with that part, i'm using unity for the rendering and tools.

    Thank you for the ideas though, i do appreciate the help.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    The largest-massed object would have an infinite-sized sphere of influence. (However, it is worth noting that 'escape velocity' is a real thing, and if you go fast enough away from something you just won't ever come back to it.)

    Have you played and/or seen Kerbal Space Program? It has solved a number of these problems (as well as problems that you are presumably going to come across later in development, like floating point accuracy), and it may be a good idea to familiarize yourself with those solutions.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Several options to consider solutions. One is to only use the most influential body. This is how Kerball manages it. Space is pretty empty. But you don't see the wobble effect this way.

    You could build a vector field. Effective if you need to apply gravity to a large number of small objects. Like combat in an asteroid belt.