Search Unity

Question Fully Deterministic Physics

Discussion in 'Burst' started by MrBigly, Feb 27, 2023.

  1. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Is Burst going to provide a fully deterministic physics engine using fixed point or something that is just as solid?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    Burst is a compiler. It translates C# ILM code to optimized machine code (assembly).

    The Entities package allows you to use the new DOTS-enabled physics engine which is deterministic. To use it, you must subscribe to the Entities programming paradigm however.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    To be clear, the existing internal 2D/3D physics engines are deterministic. The only indeterminism is floating-point handling which the DOTS physics engine suffers from too.

    So deterministic yes but not fully determinisic. :)
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    But it wasn't always fully deterministic, right?

    Or maybe that Project Settings flag continuous to mislead devs since it implies that even with the flag checked, it won't be 100% deterministic:

    upload_2023-2-27_13-48-39.png

    "Enhanced" means "better but not perfect". ;)
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    The main problem is that determism is discussed without qualfying it. The physics engine doesn't add random elements so if you do the exact same thing on the device then it will always be the same next time.

    The problem here is that devs might do things like destroy an object then create it and move it and expect it to react the same. It'll react the same as you did during the last time you started the game but it won't necessarily react the same as the previous object you destroyed. All this is because of state. Different order in the broadphase, order of contact processing etc. You can improve determinism to handle these kinds of cases.

    It's not misleading, it's using the word determinism without context. In most cases, when it's discussed, it's referring to deterministic across devices.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    To clarify something. One thing that the internal 2D/3D physics engines do are fully destroy the physics worlds when a new scene is loaded, if there are no objects left (DontDestroyOnLoad). This means that the new scene will get loaded the same (order-wise) and everything will always be the same with no side-effects of an existing broadphase or any other dynamic state.

    You could say this improves determinsim. :)
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Personally when I discuss determinism I'm ususally referring to float point arithmetic. Which can be different across devices.
    This difference (in evaluating calculations with floating point values) can often be found across the range of devices, which is why we would then refer to something being 'not deterministic'.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    That's what I was referring to.
     
  9. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    So how would you interpret this when we're talking about multiple different sets of hardware?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I don't understand what you're asking me, I'm not interpreting anything. I was merely stating that you can improve local determinism. It's not all or nothing was my point.

    I stated above that often, when devs say "determinism", they are referring to the same results on multiple devices. Right now, the biggest issue is FP handling.

    That has not been solved yet via Burst for HPC# so all existing physics engines from Unity are not 100% deterministic across devices. That is not to say they are not deterministic on the same device given the same set of inputs.
     
    SF_FrankvHoof likes this.
  11. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    This was what I was looking for.. I think that that was what @CodeSmile was also referring to with 'fully deterministic'.
     
    MelvMay likes this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Yes, fully deterministic. The thing is, even that has interpretations as I alluded to above regarding game-play changes.

    You can write a (say) fixed-point physics engine and still not get determinism as you interpret it because things behave differently during the gameplay because of order of operation changes. They'll always react like that from session to session but during gameplay, two things might act differently. You can improve/solve this to stop order of operations being able to cause changes. Often this is at cost, mostly sorting etc. You can get this too with multithreading but again, there are ways to mitigate this, hopefully at a zero/low cost.

    100% fully deterministic that can have improved determinism added to it. Sounds crazy! :)
     
  13. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    I had to read your post more than once to understand what you wanted to say...

    So not deterministic.




    Are you saying here that it is literally impossible with the unity game engine to develop a fully deterministic game due to the unity engine itself introducing random order of processing? (I mean even if we turn off the physics engine and use fixed point math ourselves?)
     
    Last edited: Feb 27, 2023
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    No, I didn't say that. :) I also don't want this thread to become asking me about all the ins and outs of software determinism because there's plenty of great material online about it including lots and lots of posts about Unity doing a better descriptive job than I ever could! I think this one is a great article because it's concise and clear but obviously there are lots of others.

    But ... given a fixed set of inputs, Unity (not simply physics) should produce the same results on the same device. There are obvious exceptions such as multithreading which requires you to be careful about handling data order to give consistent results.The Job system can help with this if you're simply dishing out segments of an array to be handled on different threads as the data order isn't changed but other ways of handling data where it's collated at the end of the job requires specific steps such as sorting.

    It's been said before but anything that doesn't adhere to this should be considered a tentative bug, for instance, objects being activated in a different order when loading a scene can have consequences to determinism and other stuff. Unity should not do that kind of thing!

    It's a complex subject that is hillariously condensed down to a single word "determinism". :)
     
    MrBigly likes this.