Search Unity

Bug Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider

Discussion in 'Physics' started by namcap, Oct 24, 2021.

  1. namcap

    namcap

    Joined:
    Jan 8, 2016
    Posts:
    49
    I see the following error occur when I make a Physics.Raycast call:

    Code (CSharp):
    1. Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider. Scene hierarchy path "Units/Player1/BodyCollider"
    2. UnityEngine.StackTraceUtility:ExtractStackTrace () (at C:/buildslave/unity/build/Runtime/Export/Scripting/StackTrace.cs:37)
    3. UnityEngine.PhysicsScene:Internal_Raycast (UnityEngine.PhysicsScene,UnityEngine.Ray,single,UnityEngine.RaycastHit&,int,UnityEngine.QueryTriggerInteraction)
    4. UnityEngine.PhysicsScene:Raycast (UnityEngine.Vector3,UnityEngine.Vector3,UnityEngine.RaycastHit&,single,int,UnityEngine.QueryTriggerInteraction) (at C:/buildslave/unity/build/Modules/Physics/ScriptBindings/Dynamics.bindings.cs:705)
    5. UnityEngine.Physics:Raycast (UnityEngine.Vector3,UnityEngine.Vector3,UnityEngine.RaycastHit&,single,int) (at C:/buildslave/unity/build/Modules/Physics/ScriptBindings/Dynamics.bindings.cs:1279)
    The specific raycast call I make is:

    Code (CSharp):
    1. Physics.Raycast(particle.position, Vector3.down, out RaycastHit hit, 10, groundLayerMask)
    Is this a bug with Unity's Physics.Raycast call? The stacktrace points exactly to this line in my code. I don't have reliable steps to reproduce this, and the "BodyCollider" that is mentioned is not on the same layer that the raycast is looking at.
     
  2. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    I'm getting the same issue with OverlapBoxNonAlloc. The wild thing is that the scene heirarchy path it gives is to a collider that is excluded by the layermask!
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    You can get this for anything (not just physics, not just this call). If you're passing in a nan value in a single, vector etc. Have you verified you're not doing that?
     
  4. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Yep.

    upload_2022-11-17_18-33-1.png

    upload_2022-11-17_18-33-23.png

    Note again that the scene hierarchy path provided points to a collider that is excluded by the layerMask. I assume the fact that it's a trigger collider is tripping up the physics system somehow.
     
  5. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    It's times like this that I lament how Unity remains the only major commercial game engine that doesn't make the source available, so I have no way to actually understand what's going on and why this error might be happening, much less contribute a fix. I am at the mercy of the Unity people, so help me God.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Note that I'm not in the 3D physics team but from what I know, the above isn't related to the query at all. I would guess that you have AutoSyncTransforms on which is bad for performance because what it does is check if any Transforms have changed (by the user) and if so, perform all the appropriate updates of Rigidbody/Collider before any read operations occurs such as reading a physics property or performing a query.

    This is why, when given the message, you're saying the "path" isn't related to the query. It doesn't have to be related to the query. TBH you shouldn't be modifying the Transform really and/or not using AutoSyncTransforms.

    As to what the message is saying, it's indicating that a Transform has gone bad. This can occur if something like a Joint simulation has "exploded" and caused NaNs to be written via the Rigidbody to the Transform or other reasons.

    If you get this message, it's indicating the specific thing that had the problem so you could inspect that. You'd get an identical thing if you were to manually call "Physics.SyncTransforms".

    More detail is that when a SyncTransform occurs on a GameObject with a Collider, the collider has to calculate a Matrix that contains the relative transformation from the Rigidbody to which it's attached. It's here that the calculation has gone wrong.

    This doesn't exist anymore in 2023.1 but FYI:
    Code (CSharp):
    1. bool Collider::GetRelativePositionAndRotation(const Transform& parent, Matrix4x4f& matrix)
    2. {
    3.     Matrix4x4f childMatrix;
    4.     GetPositionAndRotation(GetComponent<Transform>(), childMatrix);
    5.     Matrix4x4f parentMatrix = parent.GetWorldToLocalMatrixNoScale();
    6.     MultiplyMatrices4x4(&parentMatrix, &childMatrix, &matrix);
    7.     if (!IsFinite(matrix))
    8.     {
    9.         TEMP_STRING msg = "Infinity or NaN floating point numbers appear when calculating the transform matrix for a Collider. "
    10.             + GetSceneHierarchyPathDescriptive(this);
    11.         ErrorStringObject(msg.c_str(), this);
    12.         return false;
    13.     }
    14.     return true;
    15. }
     
    Zimbres and cxode like this.
  7. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Thank you very much for the detailed explanation of the error, and for the peek at the code!

    You were totally correct that my problem was AutoSyncTransforms. This is an older project, first created in Unity 2017.1, so AutoSyncTransforms was enabled by default. I had no idea that this setting existed or that I was supposed to turn it off. Turning it off prevented the error from occurring and seems to have only broken a few minor things that will be easy to fix with manual calls to Physics.SyncTransforms.

    I'm very curious, what is changing about physics in 2023.1 to delete that code?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    Honestly, I have no idea of the details. The refactoring removed that, maybe there's something similar to what's in 2D where if a Transform has any kind of NaN then it's reset to sensible values. The physics was simply telling you that a Transform is bad, not that there's a bug in the physics engine. Often these kinds of warnings end up being removed because they are either no longer needed or that they are just loathed because of the console spam. No idea what's the reason here.
     
    cxode likes this.