#Correct way of updating Collision Filter at runtime

1 messages ยท Page 1 of 1 (latest)

next jungle
#

SystemAPI.GetComponent<PhysicsCollider>(entityToUpdate).Value.Value.SetCollisionFilter(new CollisionFilter());

Is this the correct way to do it ?

Because in my usecase I have Something like that :

PrefabLoadResult prefabBeltResult = SystemAPI.GetComponent<PrefabLoadResult>(prefab);
Entity mainEntity = state.EntityManager.Instantiate(prefabResult.PrefabRoot);

When i'm doing the update it seems that it's updating all colliders made from that prefab ...

cobalt bobcat
#

Yeah that is an issue which we are addressing at the moment as a matter of fact.

#

Two things: generally colliders are shared by default. So unless you make them "force unique", which is currently only possible via the PhysicsShape and we are looking into allowing this also for built-in colliders, modifying one will modify all the other ones as well.

#

Collider blobs are shared if they are identical. Say the same geometric parameters, material and collision filter.

#

In a future version you will be able to make these unique easily in code using a new function. When you do this before you modify anything, you are sure that this one specific collider blob is now unique and can be safely modified.

#

Currently, for the same effect you would need to clone the collider blob and reassign it the the PhysicsCollider.Value property.

next jungle
#

Even with Force unique

#

It's still doing the same thing ๐Ÿ™‚

cobalt bobcat
#

BUT, when you do this, the cloned collider will also need to be disposed when the entity is deleted, which is cumbersome to manage. In a future version, via that "make unique" function, this will all be handled for you under the hood.

#

So, now the second problem. Prefabs ๐Ÿ˜…

#

Prefabs, as it the case for you, are generally just copied using a blit memory copy operation when they are instantiated in "entity land".

#

So, even though you want a unique collider, all the entity prefab INSTANCES will all obtain the exact same collider blob

#

This is also now fixed already in an internal version of the package which will be released eventually. I can not say exactly when though.

next jungle
#

An another thing btw : Plane Physics shapes are shared even between entities

Example : cf screenshot

cobalt bobcat
#

So, right now, the only thing you can do is always clone the blob after your created a prefab instance

next jungle
#

For bot Sockets :

cobalt bobcat
#

Because by design they will NOT be unique despite you saying so in the Physics Shape within that prefab

next jungle
#

Same code but with plane as physics shape :

cobalt bobcat
#

So, this is a major limitation which we have overcome in a new internal version of the package. For now, as I said, when dealing with prefabs and wanting to modify the collider, you must clone it before modifying it and reassign it to the PhysicsCollider.Value property

next jungle
#

Noice good job for fixing it ๐Ÿ™‚ No ETA at all ?

#

When yo usay : Clone --> what do you mean ? Cloning the game Object ? or the actual entity from the prefab ? --> collider blob sorry Gonna check google on how to do that

next jungle
#

This don't seem to do the trick :

Entity conveyorInput = SystemAPI.GetBuffer<InputSocketsBuffer>(_mainConveyorEntity)[0].Ref;
PhysicsCollider inputCollider = SystemAPI.GetComponent<PhysicsCollider>(conveyorInput);
BlobAssetReference<Collider> valueInput = inputCollider.Value.Value.Clone();
valueInput.Value.SetCollisionFilter(_socketsCollisionFilter);
inputCollider.Value = valueInput;
state.EntityManager.SetComponentData(conveyorInput, inputCollider);
SystemAPI.GetComponent<PhysicsCollider>(conveyorInput).Value.Value.SetCollisionFilter(_socketsCollisionFilter);

cobalt bobcat
#

You need to clone the Collider.

#

If you have a PhysicsCollider component, take the "Value" blob asset property, access the collider inside the blob asset (component.Value.Value) and then clone it using component.Value.Value.Clone();

#

This gives you a new BlobAssetReference<Collider> which you can then reassign to the component: component.Value = ...Clone();

#

That clone you can modify safely

#

We are doing this inside a demo. Let me dig it up for you.

next jungle
#
            //Getting physics Collider
            PhysicsCollider inputCollider = SystemAPI.GetComponent<PhysicsCollider>(conveyorInput);
            //Extract the BlobAsset & Clone it
            BlobAssetReference<Collider> valueInput = inputCollider.Value.Value.Clone();
            // Assign the new filter to the clone 
            valueInput.Value.SetCollisionFilter(_socketsCollisionFilter);
            // Assign the clone to the initial physic Collider 
            inputCollider.Value = valueInput;
            // Set the component to the original entity
            state.EntityManager.SetComponentData(conveyorInput, inputCollider);

Basically that

#

the example I think is in the explosion sample

#

// Make a unique collider for each spawned group
BlobAssetReference<Collider> colliderCopy = collider.Value.Value.Clone();

            // Set the GroupIndex to GroupId, which is negative
            // This ensures that the debris within a group don't collide
            colliderCopy.Value.SetCollisionFilter(new CollisionFilter
            {
                BelongsTo = oldFilter.BelongsTo,
                CollidesWith = oldFilter.CollidesWith,
                GroupIndex = GroupId,
            });

            PhysicsCollider newCollider = colliderCopy.AsComponent();
            GroupCollider = newCollider;
         EntityManager.SetComponentData(instance, GroupCollider);
cobalt bobcat
#

Oh yeah. You just found exactly that as well haha

next jungle
#

Yep Exactly what I sent ๐Ÿ˜„

cobalt bobcat
#

Sorry for the double post

next jungle
#

nah no worries

cobalt bobcat
#

I should have read what you wrote first.

next jungle
#

Just did a Ctrl F on the project on " .Value.Value.Clone()"

cobalt bobcat
#

Btw, if you want to clean up the blob asset clones correctly, you can use a ICleanUpComponentData component

next jungle
#

That is nice to know

#

I'm currently trying with my usecase to see if this work or if there are other problems, just want to be sure I did not fucked it before poking again

cobalt bobcat
#

You just create one of those that contains a member blob asset reference of type collider, assign the blob asset reference that you cloned to that member and then when your entity gets deleted, you can have a system that processes these clean up components and calls Dispose() on the blob asset reference. This is all explained with an example in that documentation.

#

Yeah, you can take care of the disposal later. First see that your approach works

next jungle
#

Yup because ultimately my conveyors can be deleted

cobalt bobcat
#

And I am sure other people on discord can also help you with that later in case of need

next jungle
#

so will need to be cleaned

#

Hopefully , last time I asked the question no one answered ๐Ÿ˜„

#

You are the first one giving me a proper explanation ๐Ÿ™‚

cobalt bobcat
#

But it's actually quite simple conceptually. These clean up components remain on deleted entities. So you can have a system that looks for the presence of these components BUT not the PhysicsCollider component (which would have been removed already).

#

This is how you can find the entities that need cleanup.

#

Once you are done with the cleanup (processing these components) you remove the component using the EntityManager or an entity command buffer, and when all cleanup components are removed from an entity, THEN the entity will be fully deleted

cobalt bobcat
#

Let's spread the word ๐Ÿ˜„

next jungle
#

yep ๐Ÿ™‚

But it's a very specific usecase too

i'm using the spline package to make spline then create mesh at runtime & then I need to update everything depending on the state of building a conveyor belt I am

So the socket are "activated" only when the belt is built , not before
And the mesh collider of the belt is assigned only once because it's expensive as hell

#

Just so you know :

It's working as I want it to so it's great

But with PhysicsShape Plane --> but the debug drawing of the plane collider don't work

onyx kite
next jungle
#

Will be more straightforward and user friendly as the api get improved I hope

#

It seems like it is very complex

onyx kite
#

Yup I agree. Blobs are great for truly static data, but if you ever want to modify one, it isn't nearly as straightforward as you'd expect.