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. Dismiss Notice

Question Are deterministic physics for large networked synchronizations possible with Unity?

Discussion in 'Scripting' started by AidanofVT, May 1, 2021.

  1. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    I've spent the past month trying to implement the synchronized identical simulations doctrine made famous by the "1500 Archers" piece at Gamasutra. I failed: physics simulations never stayed the same for more than a few seconds. It's amazing how hard it is to find an answer to a question if you don't know how to ask it. I went on this forum and on the Photon forum and asked about several things related to deterministic simulations, but I didn't really know the term "deterministic simulation", nor did I know the term "synchronous engine", so I never asked about these things directly.

    Just today, I did learn those terms, and I found out that physics in Unity, out of the box, are non-deterministic: supplying a given start condition doesn't guarantee exactly the same outcome every time. So, the good news is I wasn't failing to achieve synchronized simulation because I was dumb. The bad news is that it seems like I was dumb to try in the first place.

    So, is it possible to do this in 2021? If so, what techniques, or modifications to the Unity, are needed? If not, what's a common game engine that does allow this? I'm aware that years ago someone tried to make an add-on for this purpose, but there's not really any documentation and it's out of date.
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    This is possible. It is also very difficult. I know the developer that is largely responsible for the Quantum Engine, which is essentially what you're talking about.

    You are required to implement your own almost everything, down to the Math libraries you use, since you'll want to be operating in fixed point instead of floating point (or software floats, but either way you're chucking a lot of CPU optimizations to do it). Fixed point is deterministic, but also brings a whole host of its own problems that must be solved. A lot of the work also went into making the custom stuff interface well into Unity, since you're throwing away most of the native Unity conveniences barring the render engine.

    However, there is hope on the horizon. Unity's DOTS is promising deterministic physics, though it's only in preview at the moment.
     
    Last edited: May 1, 2021
    AidanofVT likes this.
  3. AidanofVT

    AidanofVT

    Joined:
    Nov 10, 2019
    Posts:
    104
    The page says that DOTS physics offers "physics simulations that deliver exceptional performance compatible with modern networking needs." Does this mean that I might be able to adapt my project to use the current preview version and get truly (or nearly) deterministic physics? Immediately?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    In theory... sure.

    In practice... depends on your game.

    In theory my car can go 160mph... as to if I can drive it at 160mph depends completely on the conditions of when/where/how I'm driving it.
     
    Madgvox likes this.
  5. mite51

    mite51

    Joined:
    Jun 25, 2015
    Posts:
    23
    Deterministic physics using floating point math is technically possible, and eventually I see it being widely used just because it solves a lot of bandwidth issues. DOTS physics is supposed to be deterministic will rollback, but I'll believe that when I see it.
    Anyway, I was able to do it, with a few caveats. Needs to be lockstep, and all physics objects need to be instantiated and used in the same order.... *anything* that's in your physics scene has to be exactly the same... older machines will need to have their fpu state set. If you want to go further and add rollback, that's a whole other can of worms, but the short story is only spheres and capsules are deterministic and dynamic bodies. Box/mesh colliders will desync, multi-colliders on bodies will desync, joints will desync. But rollback is kind of necessary in my view unless you are ok with games where all players start at the same time, wait for the slowest connection and players cannot reconnect ( unless they re-sim the entire game up to present from the start).

    https://remotecontrolfungus.itch.io/undpwr
     
    Max-om likes this.
  6. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    486
    We have developed a ballistics engine that is using single precision floats and that is deterministic. So it's absolutely possible doing deterministic physics with single precision floats.

    When a player shoots in our game he sends a RPC to the server with a seed. The simultion is done on all clients to create sound effects and hit particles. But the damage is dealt on the server. This way we hold down network traffic to a minimum. Though if you hit a networked object like a player the projectile will desync a bit when it exists the player collider because the collider will never be exactly on the same place (we use custom lagcomp that is very exact on stable connections but there will always be a small desync). Since we only use the ballistics for feedback on the clients (hit effects, passing projectiles Sonic booms etc) we made the decision that small desync was ok.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    DOTS does not change how floating-point is handled by the CPU so it is no "more deterministic" than anything else. Yes, systems run in the same order and introduce no random elements but if you're talking about physics then Havok/Unity Physics is no more deterministic than 2D (Box2D) or 3D (PhysX); neither of them introduce random elements either.

    The main thing about the ECS portion of the DOTS eco-system is that it can be used in such as way as to contain 100% of the game state and if a system doesn't introduce caches (such as physics systems often do) then it's entirely possible to rely on the loading of the ECS state to represent the next frame. Indeed, Unity Physics can do this unlike Box2D/PhysX however not having caches comes at a price in that certain performance constraints are incurred and/or things have to be rebuilt on demand such as the broadphase.

    Despite what is stated, DOTS doesn't mean 100% determinism in relation to floating point handling until Burst can guarantee that as part of its feature set. Unless I've missed that being released in which case I take back the 100% determinism sentence. :p
     
  8. mite51

    mite51

    Joined:
    Jun 25, 2015
    Posts:
    23
    Any idea when DOTS physics will be 1.0?
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529