Search Unity

Question Are physic colliders shared between instances?

Discussion in 'Physics for ECS' started by Cell-i-Zenit, Mar 19, 2021.

  1. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    EDIT: Scroll to the second post in the thread to the new question

    Hi,

    i already searched the forum and found some lines of code, but i run into some issues. Here is the error:

    Code (CSharp):
    1. The previously scheduled job Jobs:CheckStaticBodyChangesJob reads from the ComponentTypeHandle<Unity.Physics.PhysicsCollider> CheckStaticBodyChangesJob.JobData.PhysicsColliderType. You must call JobHandle.Complete() on the job Jobs:CheckStaticBodyChangesJob, before you can write to the ComponentTypeHandle<Unity.Physics.PhysicsCollider> safely.
    I already "Run()" my code so i thought this would introduce a sync point and iam done with it, but apparently this is not enough

    Code (CSharp):
    1. [UpdateInGroup(typeof(SimulationSystemGroup), OrderLast = true)]
    2. public class Bla: SystemBase
    3.  
    4. private const uint CollideWithEverything = ~ 0u;
    5. private const uint IgnoreAntiCollisionBlock = ~ 1u << 3;
    6.  
    7. protected override void OnStartRunning()
    8. {
    9.   Entities.ForEach((ref PhysicsCollider physicsCollider) =>
    10.     {
    11.       var collisionFilter = physicsCollider.GetCollisionFilter();
    12.       collisionFilter.CollidesWith = IgnoreAntiCollisionBlock;
    13.       EntityUtils.SetCollisionFilter(ref physicsCollider, collisionFilter);
    14.     })
    15.     .Run();
    16. }
    17.  
    18. protected override void OnStopRunning()
    19. {
    20.   Entities.ForEach((ref PhysicsCollider physicsCollider) =>
    21.     {
    22.       var collisionFilter = physicsCollider.GetCollisionFilter();
    23.       collisionFilter.CollidesWith = CollideWithEverything;
    24.       EntityUtils.SetCollisionFilter(ref physicsCollider, collisionFilter);
    25.     })
    26.     .Run();
    27. }
    28.  
    29. public static CollisionFilter GetCollisionFilter(this ref PhysicsCollider collider)
    30. {
    31.   var filter = CollisionFilter.Default;
    32.   unsafe
    33.   {
    34.     var colliderPtr = (ConvexCollider*) collider.ColliderPtr;
    35.     filter = colliderPtr->Filter;
    36.   }
    37.  
    38.   return filter;
    39. }
    40.  
    41. public static void SetCollisionFilter(ref PhysicsCollider collider, CollisionFilter newFilter)
    42. {
    43.   unsafe
    44.   {
    45.     var colliderPtr = (ConvexCollider*) collider.ColliderPtr;
    46.     colliderPtr->Filter = newFilter;
    47.   }
    48. }
     
    Last edited: Mar 20, 2021
  2. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    okay its not solved! :)

    My question is: are physicscollider shared somehow?

    I iterate over each object, then i remember the current collideWith value, then override the physicscollider.collidewith value to something else. Later i want to switch back to the original one.

    But apparently the values are shared between instances:

    Code (CSharp):
    1. //I do this for every object once
    2. var temp = physicsCollider.Value.Value.Filter;
    3.  
    4. storedData.TempCollidesWith = physicsCollider.Value.Value.Filter.CollidesWith;
    5.  
    6. //temp.CollidesWith = 0u;
    7. physicsCollider.Value.Value.Filter = temp;
    if i uncomment the value, then i store a single time the correct value, and then i store only 0s to the storedData entity...

    I tried setting the entity to "force unique", but this doesnt change anything.

    Did i do something wrong?
     
  3. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    force unique appears to be broken to me...

    In general colliders can be shared if they have the same input yes
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    "Force unique" checkbox in "Physics Shape" component should control creation of separate colliders for bodies (leaving it OFF will make colliders be shared).
    Could you please provide more details around when you're trying to change the filter and what you want to achieve? Have you tried using UnityEngine.Debug.Log or asserts to print the values at the end and make sure you really performed the changes?