Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What breaks determinancy in Unity?

Discussion in 'Multiplayer' started by Zolden, Nov 13, 2018.

  1. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    I'm trying to make my game run deterministically on connected machines.

    At start I set the same random seed. All objects in game are being controlled through Rigidbody methods, and the game can run deterministically for a couple of thousands frames. Then determinancy breaks by some reason. And I'd like to find the reason.

    Any tips?
     
  2. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    The hardware of course, if we are talking about desktop platforms. This is one of the reasons why sometimes developers distribute multiplayer games on multiple platforms but don't provide cross-play between them. Different CPU models and graphics cards are stumbling-stones of determinism.
     
    Last edited: Nov 13, 2018
  3. raidenfreeman

    raidenfreeman

    Joined:
    Sep 28, 2018
    Posts:
    1
    You can't really use Unity's physics and have determinism. Unity's physics uses floating point arithmetic, which is, to put it bluntly, unreliable. If you want to have deterministic physics, you need a physics simulation that relies on integer arithmetic. This means that you will have to find something else (not Rigidbody), or write it yourself. Unity allows you to calculate the next physics tick yourself, but it's quite a lot of work.
     
  4. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    If you want to make something deterministic in Unity currently, the best option is to use fixed point math ( https://en.wikipedia.org/wiki/Fixed-point_arithmetic ) and implement all the systems you need for your game by hand (physics, navmesh, etc.) using that.
     
    creepteks likes this.
  5. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    Currently I'm running 2 copies of the game on the same machine, so hardware difference might not be the case for nondeterministic cases, should be something in the code.

    Actually I tested pure physics alot and got no signs of nondeterministic behaviors. At least for PC-PC and Android-Android cases. Just no matter how many frames I waited and how many rigidbody+collider objects have the players spawned, I had equal simulation states. This kinda convinced me that unity physics are deterministic. I also googled it and found other people reported they found unity physics to be deterministic.

    So, what makes you guys think that unity physics alone is nondeterministic?

    Thank you, will do then.
     
    LaneFox likes this.
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Common misconception!

    https://forum.unity.com/threads/physics-and-determinism.542088/

    PhysX is deterministic. You'll hear more about physics and determinism in 2019. I think, largely, that people in general don't understand how to create deterministic systems.
     
  7. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    This is only true for the same build on the same hardware though. Even according to nvidia. Quote from PhysX 3.4 docs:

     
    SamuelGoldenbaum likes this.
  8. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    :confused:
     
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Yes, OP is running it on the same machine, though.

     
  10. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Okey sure, but... you can't build a deterministic game like a lockstep simulation on that fact.

    Basically, if you have some simulation/research/whatever type thing that's always going to run on the same hardware... then yes you can make it deterministic, otherwise... no
     
  11. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    If it's an IEEE 754 compliant processor (most) then it can be.
     
  12. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Yea, it's possible only with strict IEEE 754 compliant mode trading performance and restricting floating point operations in general.
     
  13. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Which you can't enable from C# afaik ;p
     
    nxrighthere likes this.
  14. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Yea, we are sitting on a different level for such stuff.
     
  15. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    AFAIK the IEEE 754 compliance is pretty well saturated among hardware developers for a good while now. The bigger problem with determinism breaking in software (my experience) is the fault of the software developer not guaranteeing digestion of the inputs in the same order with the same time deltas.

    I do agree that you cannot guarantee that Player One is not using a processor from 2007, but surely there could be some hardware requirements and testing to determine if this is the case or not - it should be particularly easy on non-desktop platforms since PS4/XBO/etc are fixed hardware.
     
    Last edited: Nov 13, 2018
    nxrighthere likes this.
  16. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    Some experiments are running on different machines, some are on the same one. Running tests on the same machine guarantees there's no hardware difference based nondeterminism.
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I would think the small variances in the timing of the physics update would build up to large differences in the simulation over longer play times. FixedUpdate and the physics update are called on a set schedule, but if something else is still running when FixedUpdate should run it doesn't interrupt to run on time. It just waits.
     
  18. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    It seems like replacing floats with fixed has eradicated nondeterminism in my game. Or radically reduced a chance of it.

    At least three long (30K frames) tests with intensive RTS battles (hundreds of units killed each other with thousands of missiles) stayed deterministic. It wasn't possible with the floats.

    Still I'm using unity physics, and it doesn't seem to be an issue for determinism. At least while playing on one machine.

    Thank you guys for help.
     
  19. cdr9042

    cdr9042

    Joined:
    Apr 22, 2018
    Posts:
    170
    By this, do you mean using integer for coordinate, vector? Can you please elaborate?
     
  20. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    @cdr9042 A fixed-point math library based on integers replaces floating-point math since floating-point is one of the sources of indeterminism. This one is an attempt to make it done right. Here's a discussion about the current state of fixed-point data types in Unity with additional info.
     
    cdr9042 and tobiass like this.
  21. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    I mean fixed math library. I used this one.

    Also, I had some rare occasions of indeterminism even after replacing all float variables with fixed. And that was fixed after I isolated the deterministic part of the game with its own random seed.
     
    cdr9042 likes this.