Search Unity

NaN values when using physicsMass.InverseInertia = 0

Discussion in 'Physics for ECS' started by kro11, May 3, 2021.

  1. kro11

    kro11

    Joined:
    Sep 23, 2019
    Posts:
    105
    To freeze a rotation axis I use this code:

    Code (CSharp):
    1. [UpdateBefore(typeof(EndFramePhysicsSystem))]
    2.  
    3. //..
    4.  
    5. Entities.
    6.             ForEach((ref PhysicsMass physicsMass) =>
    7.             {
    8.                 physicsMass.InverseInertia.x = 0;
    9.                 physicsMass.InverseInertia.z = 0;
    10.             }).ScheduleParallel();
    11.  
    12. //..
    13.  
    It creates NaN values in PhysicsVelocity, LocalToWorld, Translation, Rotation Components right after StepPhysicsWorld system. I check all values using math.isnan and math.isfinite before that system. This is reproduced when a rigidbody has many collisions and changes position using translation.Value. If wait until all collisions subside then it works fine.
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    A small test project would be really handy to see the problem in action. What is you're use-case? I assume it is related to this question. There you mentioned controlling the collision response, though setting translation.Value fights against the simulation rather than working with it.

    In lieu of a sample to test, you are updating inertia before EndFramePhysicsSystem, but do you also update it after ExportPhysicsWorld? If you are updating the ECS data somewhere after BuildPhysicsWorld (which reads in the ECS data) and before ExportPhysicsWorld (which writes out the changes to the ECS data), you could be losing integrity leading to the NaN's.
     
    kro11 likes this.
  3. kro11

    kro11

    Joined:
    Sep 23, 2019
    Posts:
    105
    It is about 10k creatures as capsules that collides between each other when they go. I haven’t used your previous advice yet, as I’m doing other things right now. But thanks, I will use it in the future. Right now I'm trying to just freeze 2 axes so they don't shake on collision.

    With after ExportPhysicsWorld it works much better, but the NaN error still occurs sometimes.
     
  4. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    Can you try something like:


    Code (CSharp):
    1. [UpdateAfter(typeof(ExportPhysicsWorld))]
    2. [UpdateBefore(typeof(EndFramePhysicsSystem))]
    3. [UpdateInGroup(typeof((FixedStepSimulationSystemGroup))]
    4. YourSystem : SystemBase
    5. {
    6.     void OnUpdate()
    7.     {
    8.         Dependency = JobHandle.CombineDependencies(m_ExportPhysicsWorld.GetOutPutDependency(), Dependency);
    9.  
    10.         // Schedule your jobs here
    11.     }
    12. }
     
  5. kro11

    kro11

    Joined:
    Sep 23, 2019
    Posts:
    105
    I am using Entites 0.9.1 and Physics 0.3.1. If I can't get results on the old version, then I'll try your way after update, thanks.
     
  6. caseyc

    caseyc

    Joined:
    Jul 2, 2012
    Posts:
    18
    I'm having the same exact issue, I tried the proposed solution but it didn't help anything. I wasn't having this issue at all before updating to the most recent physics version, so I think it might be a regression. If I figure it out I'll be happy to post the solution here, but if anyone has any other suggestions I would be happy to hear them. Thanks!
     
    steveeHavok likes this.
  7. caseyc

    caseyc

    Joined:
    Jul 2, 2012
    Posts:
    18
    I fixed it by just setting the value to something low (0.0001f) instead of 0. I think there is some divide by zero going on under the hood in certain circumstances. I make sure the rotation and velocity of each thing is constrained each frame in another system already, so that combined with this is enough to keep things in place. Hope that helps anyone else having this issue.
     
  8. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Wow, Physics 0.3.1 hmm. I need to pull out the wayback machine :)
    I have vague recollections about NaNs arising when LocalToWorld transforms weren't initialized. IIRC the Vehicle sample needed extra code to make sure. See the first job in OnUpdate here. I can't fully remember everything about the details so possibly nothing to do with your own use-cases (I think interpolation smoothing was involved).

    If anyone has a repro project we can look at running with the latest version of Unity Physics that would be really useful.
     
    papopov likes this.