Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved CompoundCollider Children change indicies at runtime

Discussion in 'Physics Previews' started by NT_Ninetails, Apr 16, 2021.

  1. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    So I add children to a CompoundCollider manually, and when i go to access them later, the indices match.
    However, if i add more than 3 children, when i access them later, the indicies are all mixed up or offset.

    Anyone know why this is and how to prevent it?

    upload_2021-4-16_11-27-4.png
     

    Attached Files:

  2. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    so i found a way to fix it!

    So the indices do get mixed up but I made a thing that fixes it at runtime. I will mark this as a bug unless someone tells me why the indices get changed.
     
  3. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @NT_Ninetails , thanks for raising this! Are you using CompoundCollider.Create(NativeArray<ColliderBlobInstance> children) to create the compound? What exactly is wrong with the index (how did you determine child order was changed)?

    Read through CompoundCollider.Create and I didn't notice a bug, but would love to repro it and fix if something's broken. Could you please send a repro project or maybe a unit test that exposes the bug (something like the ones in com.unity.physics\Tests\PlayModeTests\Collision\Colliders\CompoundColliderTests.cs)?
     
  4. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    project example: https://github.com/Steven9Smith/PhysicsCompoundColliderIndexBug

    The code in the project focuses on the bug in question.

    - Yes
    on creation of the collider i printed all the RigidTransforms of the children and store them in some ComponentData struct. After the ECS Systems started up I used a EntityQuery to grab the entity with the collider I made. I then go through the children and check to see which position (from the previouly stored data) is closest to the current child. Since the stored data also has the index of the child that was set using that data i can directly compare if the indicies match. if they don't then the index was change.

    Though, now that i type this out, they may be a possibility that just the RigidTransform was swapped between the child colliderers since I verify indicies with those positions....
     
    Last edited: Apr 22, 2021
  5. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @NT_Ninetails , I think there's a bug in your code that matches compound children with physicsColliderFollowEntityBuffer items. Those operations (especially sign invesrion) on position looks suspicious (it shouldn't be needed).

    If you add the following code to AnimatedPhysicsColliderSystem.OnStartRunning(), just before looping through physicsColliderFollowEntityBuffer, you'll see that all children are at the places where you printed them before compound creation:
    Code (CSharp):
    1.                     for (int j = 0; j < compoundCollider->NumChildren; j++)
    2.                     {
    3.                         for (int i = 0; i < physicsColliderFollowEntityBuffer.Length; i++)
    4.                         {
    5.                             if (physicsColliderFollowEntityBuffer[i].CompoundColliderChildIndex == j)
    6.                             {
    7.                                 UnityEngine.Debug.Log($"child {j} animation {physicsColliderFollowEntityBuffer[i].AnimationDataIndex} offset {physicsColliderFollowEntityBuffer[i].Offset.Position}");
    8.                             }
    9.                         }
    10.                     }
    11.  
    To compare offset, you can print ltw.Position in AnimatedPhysicsColliderComponent.Convert.

    Please let me know if this helps.
     
  6. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    Hey @milos85miki The physicsColliderFollowEntityBuffer does what you say but when i output the position of the collider it doesn't match the offset (initial given position). The child collider positions are moved slightly so I used the OnStartRunning code to find the closest position to the "offset" value and changed the index accordingly. If i don't change the index then when I modify the position of the child it turns out I'm modifying the wrong child.

    Code (CSharp):
    1.  
    2.                                 UnityEngine.Debug.Log($"child {j} animation {physicsColliderFollowEntityBuffer[i].AnimationDataIndex} offset " +
    3.                                     $"{physicsColliderFollowEntityBuffer[i].Offset.Position} childPosition {compoundCollider->Children[j].CompoundFromChild.pos}");
    upload_2021-4-22_21-3-51.png
    example:
    i set the hip collider to be a cylinder so it's easier to see and added this line at the end of OnStartRunning

    that sets the hip collider's (buffer index 1) to position (0f,0.5f,0f) (under the butt)
    Code (CSharp):
    1.  
    2.  
    3.                     compoundCollider->Children[physicsColliderFollowEntityBuffer[1].CompoundColliderChildIndex].CompoundFromChild.pos = new float3(0, 0.5f, 0);
    upload_2021-4-22_21-26-33.png
     
  7. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @NT_Ninetails , I just finished debugging - the compound collider is changed by BuildCompoundCollidersConversionSystem, based on the objects hierarchy in the scene. So, it's not a bug and you can probably let it do the heavy lifting instead of writing custom code (of course, just make sure to understand what it's doing and tweak scene setup so it produces the right compound for you).
     
  8. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    Thanks for your help i'll keep this into account :D

    would you happen to know how to override the EnableMassFactors & EnableSurfaceVeclotiy on the colliders. I read the docs and Unity says it's hidden for now.
     
  9. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
  10. NT_Ninetails

    NT_Ninetails

    Joined:
    Jan 21, 2018
    Posts:
    195
    oh shoot, i forgot about that, thanks!
     
    petarmHavok likes this.