Search Unity

Running physics simulation in background, possible?

Discussion in 'Scripting' started by unikum, Jun 25, 2012.

  1. unikum

    unikum

    Joined:
    Oct 14, 2009
    Posts:
    58
    Hi,

    I wondering if it is possible to somehow run physics simulations, speeded up, in the background? The use case is to run a bunch of simulations of a ball being hit through an environment to figure out the best way to hit it. Since the ball can hit stuff and it has a lot of other things affecting it (like drag, wind, magnus effect etc) I thought it might be easier to just run a few quick simulations to find a fairly good result. It doesn't have to be perfect, just good enough for the ball to end up within a specific area. So basically I just want to run the same function to hit the ball that I already have, but sped up quickly and adjust a little after every try and pick out the best result.

    Is this possible to do in Unity? I can't just speed up the world physics, because there will be a lot of other things going on at the same time that needs the world physics to be constant.

    Any ideas are appreciated!
     
  2. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    That's what I was wondering too. So far I haven't found a way and it would be a shame if that cannot be done. Being able to adjust timescale only for selected objects would be cool.
     
  3. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    I think you've answered yourself in your question.

    Since we are limited to a single physics simulation (AFAIK) you can't run one in the background speeded up as it would affect the world.

    However one idea, though it very much depends on the complexity of your project would be just prior to running your 'speeded up simulation' you cache the entire worlds physic state, then de-activate all physical entities that are not important to your simulation. You can now set up new physic simulation and run it as fast as is feasible. Once you've got the information you want, you can remove you simulation and restore the worlds physics state.

    Now I have no idea if its possible or advisable to cache and restore the physics state of objects, never tried it. It should also be pointed out if the physics engine is not deterministic (which I don't think it is) then you can't be 100% that performing the same simulation will result in the same result.
     
  4. unikum

    unikum

    Joined:
    Oct 14, 2009
    Posts:
    58
    @Noisecrime: Yeah I was afraid that it wasn't possible. Your idea sounds interesting and I might try it out a bit and see how it works, thanks.