Search Unity

Bug CompoundCollider Children Not Working Correctly

Discussion in 'Physics for ECS' started by Jawsarn, Feb 7, 2020.

  1. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    245
    Iterating over a compound colliders children gives bad values e.g. saying collider is terrain and showing seemingly "random" values for filter. I've tried with both using foreach and for looping.

    Code (CSharp):
    1. var physicsColliderPtr = intCollider.ColliderPtr;
    2. var newFilter = new CollisionFilter()
    3. {
    4.     BelongsTo = filterInterior.belongsTo.Value << spaceShipData.crewGameID,
    5.     CollidesWith = filterInterior.collidesWith.Value << spaceShipData.crewGameID,
    6.     GroupIndex = 0
    7. };
    8.  
    9. if (physicsColliderPtr->Type == ColliderType.Compound)
    10. {
    11.     var compoundCollider = (CompoundCollider*)physicsColliderPtr;
    12.     compoundCollider->Filter = newFilter;
    13.     SetChildFilter(ref newFilter, ref compoundCollider);
    14. }
    15.  
    16. ...
    17.  
    18. unsafe void SetChildFilter(ref CollisionFilter collisionFilter, ref CompoundCollider* collider)
    19. {
    20.     var numChildren = collider->NumChildren;
    21.     for (int i = 0; i < numChildren; i++)
    22.     {
    23.         ref var child = ref collider->Children[i];
    24.        ...
    25.  
    26.     }
    27. }
     
    Next1on and LukasLyri like this.
  2. LukasLyri

    LukasLyri

    Joined:
    Oct 31, 2020
    Posts:
    1
    Same problem, please fix asap.
     
  3. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    245
    I solved my case by creating an GameObjectConversionSystem in GameObjectAfterConversionGroup update group that clones and store the colliders in a IBufferElementData that is later used to set the correct collider when needed. Hope it helps : )
    Code (CSharp):
    1.     [UpdateInGroup(typeof(GameObjectAfterConversionGroup ))]
    2.     public class MultiLayerPhysicsCollidersConversionSystem : GameObjectConversionSystem
    3.     {
    4.         protected override void OnUpdate()
    5.         {
    6.             Entities.ForEach((MultiLayerColliderAuthoring multiLayerColliderAuthoring) =>
    7.             {
    8.                 var entity = GetPrimaryEntity(multiLayerColliderAuthoring.gameObject);
    9.                 if (DstEntityManager.HasComponent<PhysicsCollider>(entity))
    10.                 {
    11.                     var collider = DstEntityManager.GetComponentData<PhysicsCollider>(entity);
    12.                     var multiLayerPhysicsColliderBuffer = DstEntityManager.GetBuffer<MultiLayerColliderElementData>(entity);
    13.                  
    14.                     // 0 is world default
    15.                     multiLayerPhysicsColliderBuffer.Add(new MultiLayerColliderElementData() {Value = collider.Value});
    16.                     var colSettings = CollisionSettings.GetOrCreate();
    17.                  
    18.                     // TODO should probably be checked vs the blobassetstore for similar blobassets
    19.                    
    20.                     // Create clone for each layer
    21.                     for (int i = 0; i < MultiLayerColliderAuthoring.numShips; i++)
    22.                     {
    23.                         var colliderBlobPtr = collider.Value.Value.Clone();
    24.                         multiLayerPhysicsColliderBuffer.Add(new MultiLayerColliderElementData() { Value = colliderBlobPtr});
    25.  
    26.                         colliderBlobPtr.Value.Filter = new CollisionFilter()
    27.                         {
    28.                             BelongsTo = colSettings.interiorStartCollisionTag.Value << (i),
    29.                             CollidesWith = colSettings.interiorStartCollisionTag.Value << (i),
    30.                             GroupIndex = 0,
    31.                         };
    32.                     }
    33.                 }
    34.             });
    35.         }
    36.     }
     
    kilo32 likes this.