Search Unity

[SOLVED] CalculateDistance() error because it calculates an infinite distance

Discussion in 'DOTS Physics' started by PhilSA, Dec 12, 2019.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,732


    I found a situation where PhysicsWorld.CalculateDistance() returns an infinite distance. More precisely, the part that fails is the "ConvexConvex()" function in ConvexConvexDistance.cs, here at line 558:


    The white capsule in the top picture tries to find collisions with PhysicsWorld.CalculateDistance() and that's where the problem happens (when we've reached the end of the tunnel). The capsule is a capsule collider, and the two walls + floor are box colliders.

    Video: https://i.gyazo.com/3260e81a7548dd3863fd6e3595660603.mp4

    and here's the code used to calculate distance:
    Code (CSharp):
    1. ColliderDistanceInput distanceInput = new ColliderDistanceInput
    2.     {
    3.         Collider = physicsCollider.ColliderPtr,
    4.         MaxDistance = 0f,
    5.         Transform = new RigidTransform(rotation.Value, translation.Value),
    6.     };
    7.  
    8.     characterDistanceHitsBuffer.Clear();
    9.     DistanceHitsDynamicBufferCollector distanceHitsCollector = new DistanceHitsDynamicBufferCollector(0f, ref characterDistanceHitsBuffer);
    10.  
    11.     PhysicsWorld.CalculateDistance(distanceInput, ref distanceHitsCollector);
     
    Last edited: Dec 12, 2019
  2. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    50
    Hello Phil, it's probably a NaN or divide by zero somewhere. If you're able to reproduce the problem, could you share the inputs to ConvexConvex() -- both float arrays and the aFromB transform?

    I'm not sure if it's possible to do this in the C# debugger, but if it is, could you reinterpret cast the input values as unsigned int and share those? The reason is that when the debugger displays float values, it truncates them to some number of digits, so if I copy those into code they're not exactly the same and the bug might not repro.
     
  3. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,732
    I wasn't super sure how to get that info since I don't have the Unity.Physics code in my VS solution, so I went with debug logs. aFromB.Translation seems to be the culprit, but let me know if you need other values after this:

     
    Last edited: Dec 12, 2019
  4. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    50
    Yes, aFromB is definitely the problem there :) Since the issue isn't in ConvexConvex() itself, can you add more debug logs in the calling function in order to track it down to the origin? The NaNs might come from the ColliderDistanceInput, or from one of the colliders in the world.
     
  5. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,732
    uh oh, false alarm

    I went up the call stack and the error happens on my side. It's my original translation that's NaN

    sorry about that!
     
    steveeHavok likes this.
  6. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    480
    Out of interest, do you ever copy the Unity Physics package over to be a local package so that you can debug the code in VS?
     
  7. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,732
    Yes! That's how I ended up doing it
     
    steveeHavok likes this.
unityunity