Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Prewarming simulation – Speeding up simulation on load?

Discussion in 'General Discussion' started by aaro_murphy, Jul 28, 2020.

  1. aaro_murphy

    aaro_murphy

    Joined:
    Jul 5, 2020
    Posts:
    4
    Hi

    I am trying to find a way to speed up / prewarm my simulation at the start of the game.

    I want to make it seem as if the creatures in the game have been existing for a while before the viewer starts the game. The problem I encounter now is that everything "starts" at the beginning whereas I would like the player to encounter an already evolved ecosystem.

    I hope someone can help
     
  2. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    2,389
    I've thought about this before too. The only way I saw is to essentially let the simulation run for a significant period of time before you load the game - setting the timescale to 10 or 100 or something, then running with a black screen until some trigger occurs (some amount of time, something happens in the game world).

    If your simulation was more prescriptive you could maybe store different timepoints and load those. Or you could run a few times and find the general shape of the world after X days and generate that at the beginning instead.
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,067
    Reminds me of couple of games where you could be killed during post-load "fadein" effect. Half Life 2 was one of them.

    Jokes Aside...

    ----------

    What you're trying to do is similar to X series "Out of sector combat" and Dwarf Fortress world generation.

    In case of X series (spacesim), the universe is divided into sectors, and only sector where player is present is simulated in real time. The rest of the world is using simplified calculations which are nearly turn based (they're fixed step based). This has side effect of combat working differently based on whether player is in the sector or not. A ship that performs poorly in real-time combat can be a monster in "Out of sector" combat, for example.

    In case of dwarf fortress, after generating the world map, the game creates creatures within the world, and let them live and fight there, generating story events. This done at different speed compared to normal operation of the game.
    You can read about it here:
    https://dwarffortresswiki.org/index.php/DF2014:World_generation

    ----------

    In case of unity. I believe you cannot "fast forward" physics, as the game forces calculation of physics at certain point of time. You could accelerate time by factor of 100, temporarily, but that's pretty much it, unless you switch to custom physics implementation.

    Beyond that, to "prewarm" the world you'll need to create a set of rules comparable to dwarf fortress story generation mode for the purpose of fast-forwarding the game world.

    That's the rough idea of it.
     
  4. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,105
    Actually I think they added a feature for that a good while ago. Iirc it was pitched as a way to for example precalculate throwing arcs of physics based objects, so that you can visualize them in the UI.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,067
    Do you have a link to that?

    Precalculating arc and speeding up entirety of the world is not exactly the same thing.
     
  6. Martin_H

    Martin_H

    Joined:
    Jul 11, 2015
    Posts:
    4,105
    https://docs.unity3d.com/ScriptReference/Physics.Simulate.html

    It's possible I misunderstood something, but to me that sounded like it can be used to run the Physics simulation faster than the normal gameloop.
     
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,067
    Some confusing wording in there:
    Well, yeah you can accelerate physic simulation this way, but I'm not seeing how you could use this to precalculate grenade throw, as it will update EVERYTHING in the world. It is also possible that due to computationally expensive nature of the physics it still won't be possible to reach speeds faster than maximum timescale.
     
    frosted and Martin_H like this.
  8. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    3,822
    games that do significant simulation generally do calculations outside of engine. You model the effects in pure code, and run the sim there. If you're relying heavily on physics engine, this might not really be possible to do well.
     
  9. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    2,389
    The whole "ecosystem" thing implies to me that it's not really about physics. That was something we kind of brought up.

    *shameless plug* OP, you should try simulating the ecosystem with cellular automata and then "pre-warming" it there.
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,759
    I'm going to suggest more of a brute force approach. You could consider building a system where you actually run the simulation yourself for a period of time and then save the state. After it has run for an hour, or however long you want to run to pre-warm, you reload and do it again. Save whatever random seed you used to generate anything else in the game world with the state of everything you save. Create a custom build for doing this.

    If you ran this every evening overnight on your computer, and had like 8 instances of the build running at the same time, you'd have about 1,000 different pre-warmed states saved in about 2 weeks time. Then in your actual game instead of randomly generating the world, you pull from one of these 1,000 pre-warmed states (and use the saved seed to generate anything you don't actually need to save). It would still feel random with that many saved, and the outcome wouldn't feel any different than your regular simulation (since you used the real simulation instead of a simplified system to get to the saved state), and you can always add more later with updates to your game.
     
unityunity