Search Unity

Bug Multiple raycast in IParallelForJob assertion error

Discussion in 'DOTS Physics' started by manpower13, Mar 25, 2020.

  1. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Hello everyone,

    First of all, I really like Dots physics! Api is simple and straightforward. Besides some fancy physics features missing, it is great to use!

    Now my issue: I get an assertion error, stacktrace:
    Code (CSharp):
    1. AssertionException: Assertion failure. Value was False
    2. Expected: True
    3. UnityEngine.Assertions.Assert.Fail (System.String message, System.String userMessage) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Assertions/Assert/AssertBase.cs:29)
    4. UnityEngine.Assertions.Assert.IsTrue (System.Boolean condition, System.String message) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Assertions/Assert/AssertBool.cs:20)
    5. UnityEngine.Assertions.Assert.IsTrue (System.Boolean condition) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Assertions/Assert/AssertBool.cs:13)
    6. Unity.Physics.AnyHitCollector`1[T].AddHit (T hit) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Collector.cs:53)
    7. Unity.Physics.RaycastQueries.RayCollider[T] (Unity.Physics.RaycastInput input, Unity.Physics.Collider* collider, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Raycast.cs:446)
    8. Unity.Physics.Collider.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Colliders/Physics_Collider.cs:321)
    9. Unity.Physics.RaycastQueries+RayCompoundLeafProcessor.RayLeaf[T] (Unity.Physics.RaycastInput input, System.Int32 leafData, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Raycast.cs:554)
    10. Unity.Physics.BoundingVolumeHierarchy.Raycast[TProcessor,TCollector] (Unity.Physics.RaycastInput input, TProcessor& leafProcessor, TCollector& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Geometry/BoundingVolumeHierarchy.cs:370)
    11. Unity.Physics.RaycastQueries.RayCompound[T] (Unity.Physics.RaycastInput input, Unity.Physics.CompoundCollider* compoundCollider, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Raycast.cs:572)
    12. Unity.Physics.RaycastQueries.RayCollider[T] (Unity.Physics.RaycastInput input, Unity.Physics.Collider* collider, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Raycast.cs:437)
    13. Unity.Physics.Collider.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Colliders/Physics_Collider.cs:321)
    14. Unity.Physics.RigidBody.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/RigidBody/RigidBody.cs:58)
    15. Unity.Physics.Broadphase+BvhLeafProcessor.RayLeaf[T] (Unity.Physics.RaycastInput input, System.Int32 rigidBodyIndex, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/World/Broadphase.cs:604)
    16. Unity.Physics.BoundingVolumeHierarchy.Raycast[TProcessor,TCollector] (Unity.Physics.RaycastInput input, TProcessor& leafProcessor, TCollector& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Geometry/BoundingVolumeHierarchy.cs:370)
    17. Unity.Physics.Broadphase.CastRay[T] (Unity.Physics.RaycastInput input, Unity.Collections.NativeSlice`1[T] rigidBodies, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/World/Broadphase.cs:506)
    18. Unity.Physics.CollisionWorld.CastRay[T] (Unity.Physics.RaycastInput input, T& collector) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/World/CollisionWorld.cs:201)
    19. Unity.Physics.QueryWrappers.RayCast[T] (T& target, Unity.Physics.RaycastInput input) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/Queries/Collidable.cs:99)
    20. Unity.Physics.CollisionWorld.CastRay (Unity.Physics.RaycastInput input) (at Library/PackageCache/com.unity.physics@0.3.0-preview.1/Unity.Physics/Collision/World/CollisionWorld.cs:196)
    21. VisionJob.Execute (System.Int32 index) (at Assets/Scripts/Physics/VisionJob.cs:152)
    22. Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at /Users/builduser/buildslave/unity/build/Runtime/Jobs/Managed/IJobParallelFor.cs:44)

    As you can see in the stacktrace, my setup is as follows:
    I have an IJobParallelFor job, inside the job I run a calculateDistance. The result of the calculateDistance are 10 hits (using my own collector I limit it to 10). For each of these hits, I do a raycast (
    CastRay)(using the same collisionWorld, same job, same index) to check whether any object is blocking the way.
    This second raycast triggers this assertion. I don't understand why. The second raycast is using the default Collector (AnyHitCollector).

    Thank you for reading! Hope someone might know why this is not working.

    Sincerely,
    Floris Weers
     
  2. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    I actually think it's a bug; not from my side. It doesn't make sense that I get an assertion error inside a Collector.
    That would mean that the Hit.Fraction is bigger or equal to the MaxFraction (which is 1, since I do a simple CastRay). I don't see how it is possible that a hit is added that is bigger or equal than 1.

    This is the exact code that causes the AssertionError:

    Code (CSharp):
    1. public bool AddHit(T hit)
    2.         {
    3.             Assert.IsTrue(hit.Fraction < MaxFraction);
    4.             return true;
    5.         }
    You can see in the debugger that the hit that will be added has a Fraction of 1:
    upload_2020-3-28_10-54-56.png

    MaxFraction is equal to 1.

    Why is this hit being added?
     
  3. Sima_Havok

    Sima_Havok

    Joined:
    Dec 16, 2019
    Posts:
    47
    Definitely there are inconsistencies in comparisons ("<" vs "<=") between asserts in different collectors and between asserts and conditions in AddHit invoking methods. We will fix those in one of the incoming releases.

    For the sake of your assert feel free to use <= in AnyHitCollector.AddHit
     
  4. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Thanks!
    Alright, then I'll use my own collector. This is the one from the physics package though :)

    In this case, I doubt the actual fraction value is equal to 1. I think it should be lower than 1, but I'm not sure how a hit can end up with the wrong fraction amount. I guess I need to debug that a little more.
     
  5. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    240
    is this the issue?

    Code (CSharp):
    1. /// <summary>
    2. /// Fraction in distance queries represents the actual distance where the hit occurred,
    3. /// NOT the percentage of max distance
    4. /// </summary>
    5. /// <value> Distance at which the hit occurred. </value>
    6. public float Fraction { get; set; }
     
    Baggers_ likes this.
  6. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Where did you read that?

    The only comment I can find:
    Code (CSharp):
    1. // For casts this is fraction of the query at which the hit occurred.
    2.         // For distance queries, this is a distance from the query object
    3.         float Fraction { get; }
    The second query I do gives the assertion error, which is a castRay. In that case it should be smaller than 1.
     
unityunity