For my project, I need to simulate more than 32 collision layers at once. Doing all simulation in parallel within one world would be ideal. I was excited when I saw the GroupIndex field of the Collision Filter which would make this possible. Each group of objects we want isolated to only collide amongst themselves we can call an “island”.
I set the group index of each island to a matching number with the following code.
public static PhysicsCollider SetCollisionFilter(PhysicsCollider physicsCollider, int groupIndex)
{
BlobAssetReference<Unity.Physics.Collider> colliderCopy = physicsCollider.Value.Value.Clone();
CollisionFilter filter = new CollisionFilter()
{
BelongsTo = 0xffffffff,//0xffffffff,
CollidesWith = 0x00000000,//0x00000000, //0xffffffff,
GroupIndex = groupIndex
};
colliderCopy.Value.SetCollisionFilter(filter);
PhysicsCollider newCollider = colliderCopy.AsComponent();
return newCollider;
}
In theory, based on the description of GroupIndex, this means the objects should only collide with other objects part of the same island.
>
> /// <summary>
> /// An optional override for the bit mask checks. If the value in both objects is equal and
> /// positive, the objects always collide. If the value in both objects is equal and negative, the
> /// objects never collide.
> /// </summary>
> public int GroupIndex;
However, that is not the case. I end up with very strange behavior. Sometimes the collisions are detected, sometimes not. I am confused how this is possible, because I thought the PhysicsSystemGroup was stateless and deterministic. But it’s behaving almost like there is a race condition.
Here is a video showing the objects bouncing then clipping through each other. All I did was take the ECS physics samples project and then add an authoring component which sets the groupindex of the collision filter of the entity I put it on.
You can see the sphere bounce off the top, clip through, then rapidly accelerate out the bottom.
Curious if anyone has any ideas on what could be causing this. Any thoughts or recommendations would be greatly appreciated. Also happy to share more code. Am I mistaken that greater than 32 collision layers is possible using this technique? Thanks!
