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

Moving to Unity Physics from PhysX

Discussion in 'Physics for ECS' started by Kleptine, Oct 30, 2021.

  1. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    We're considering moving to Unity Physics from PhysX, and I wanted to check in before I spend more time in this direction, considering the package is still in preview. Our use case is an Oculus Quest VR game about building deterministic machines out of your own clones.

    We've been using PhysX and it's actually shockingly deterministic for us, except for collision resolution. Unity Physics seems to be a better approach.

    We need:
    - determinism: Only per-device. This seems solid.
    - repeatability: Within the same physics world, objects should collide in the same way, regardless of other objects in the world. (Think a pachinko game where every ball should always bounce in the exact same way.) PhysX is failing us here.
    - performance: We're running on mobile, with maybe a couple hundred rigidbodies at 72fps. I'm a little concerned about lack of sleeping, though we don't have that many bodies, and no stacking. We've got plenty of headroom with the current PhysX approach.

    Do you think Unity Physics would be a good fit here? Or perhaps Havok, even? I've read most of this forum at one point or another, so I'm very familiar with the high level approach and drawbacks. No need to explain the basics, but I just wanted to get a little wisdom before diving down this hole.

    (And in case you're familiar, you don't happen to know a way to ensure PhysX collision resolution becomes deterministic?)

    Thanks!
     
    Last edited: Oct 30, 2021
  2. marijnz

    marijnz

    Joined:
    Sep 20, 2012
    Posts:
    67
  3. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    That works for us, we're on 2020.3 LTS for other reasons. So sounds like no issues there.
     
    marijnz likes this.
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I haven't used Physx in a while but how sure are you that it's not as deterministic as Unity.Physics? Unity could be doing something in their abstractions that make it not, but Physx itself should be deterministic same platform/compiler. I'd just be leary that what you are seeing as not deterministic isn't actually you, ie you might get the same behaviour on Unity.Physics. Might be worth testing that in isolation before porting.
     
  5. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    To be clear, PhysX is perfectly deterministic when you replay the same exact inputs on the same machine. What I'm talking about is *repeatability*, or local determinism, when the same action is repeated multiple times in the same game session.

    See this section from the PhysX documentation. PhysX offers limited determinism, which means the same sequence of events may be non-deterministic, if the rest of the world is changed around it.

    What I'm wanting to know is whether Unity Physics offers full determinism, such that a collision is still deterministic, even if objects are added and removed in the scene around it.


    Footnote: Our game code is 100% deterministic. We have an incredibly extensive test suite which runs the game multiple times and verifies that they all produces byte-for-byte equal output. I've spent months of my time debugging and fixing determinism bugs, so I'm very familiar with these issues.

    Collision resolution in PhysX is inherently non-deterministic because of the way the broad-phase groups objects to be collided. Adding other objects in the scene can affect how these collision groups are organized.
     
  6. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    862
    Unity.Physics is stateless. But is still has a build physics world. Not sure what you are trying to do. You can save old physics worlds and reuse them.
     
  7. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    PhysX does offer something that sounds like what you're looking for. From that docs page,

    "PhysX provides a mechanism to overcome the issue of divergent behavior in existing configurations as a result of additional actors being added or actors being removed from the scene that do not interact with the other actors in the scene. This mechanism can be enabled by raising PxSceneFlag::eENABLE_ENHANCED_DETERMINISM on PxSceneDesc::flags prior to creating the scene."

    However, I don't know if this option is exposed by Unity's PhysX integration, and I haven't tried it myself so I don't know exactly what the conditions for it to work are. Aside from this special mode in PhysX, I don't know of any physics engine that guarantees determinism with anything short of completely identical inputs.

    One way that you might be able to achieve what you want would be to create multiple separate PhysicsWorlds. So bodies in different worlds can't interact, but what happens in one world will not affect the determinism of another.
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    That option is already in Physx under Enhanced Determinism:
    upload_2021-11-3_15-4-45.png
     
  9. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    274
    Yep, and having read that page, I've already tried this. More than once actually in the last 2 years. It doesn't improve determinism for collision resolution, sadly.

    I'm also already very aware Unity.Physics is stateless. Being stateless doesn't mean collisions are deterministic if the world around them changes.

    @steveeHavok Any chance you know the answer to my local-determinism question off the top of your head?
     
  10. TheOtherMonarch

    TheOtherMonarch

    Joined:
    Jul 28, 2012
    Posts:
    862
    I think you are using the term determinism differently then most game devs use that term. Determinism in game engines is generally defined as same input results in the same output. In other words deterministic code.

    You are using the term determinism in the Newtonian sense of obeying Newtonian laws. Physic engines in games often have short cut and are often not true physics sims. If you are making a game I would recommend that you follow game dev logic, otherwise you should probably find a scientifically correct physics sim; that does not use euler integration, everything is 64 bit etc.
     
    Last edited: Nov 5, 2021
    sumit-ajnalens likes this.