Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Possible to add to compound collider in this way?

Discussion in 'Physics for ECS' started by RecursiveEclipse, Aug 6, 2020.

  1. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    At runtime, I want to add a collider to an entity with an existing compound collider. My questions are:

    1. If the collider I'm adding has a different collision filter will it still work when querying for that filter?

    2. What is the actual approach to adding to a compound collider?

    If adding to an already existing collider is tricky/costly, I could add the collider in a GameObjectConversionSystem to my prefabs. But in some cases, I would need to scale the collider at runtime, what is the way to do this?
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    1. Yes, individual colliders in the compound collider have their own individual filters, and can decide to collide/not collide on their own.
    2. Adding to a compound collider boils down to creating a new compound collider using all of the children of the old compound collider, plus this new one. There is no incremental add feature because the hierarchy (tree) needs to be rebuilt.

    Note: if you are adding just this one collider - you can also choose to make a new compound collider out of the old compound collider plus this new one (nesting of compound colliders). However, don't go too deep with this as it will eventually break because ColliderKey is there to identify children of composite colliders and it only has 32 bits.
     
  3. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Thanks, I only need one sphere collider to find certain types of object. Usually the radius can be the bounds of the renderer but there are cases where there isn't one and the radius depends on other data.

    For example, if I'm building a tree entity(an actual tree, not the data structure), I need to have an entity for the roots, and one for the "body". With my design a random body is chosen and the roots are added. For the body the collider is easy, but for the roots, the radius depends on the size of the canopy which is determined by a random scale and chosen mesh.

    Is there an example somewhere on how to properly add a collider to an existing compound and rebuild it? Thinking on this more, it looks like I need to do this for all my prefabs if I want variable radius when instantiating them.
     
    Last edited: Aug 7, 2020
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You have a CompoundCollider.Children array. Pick all the children, create a new array of ColliderBlobInstance (copy them over), add an extra collider to this new array, and give it to CompoundCollider.Create().

    Alternatively, just take the old CompoundCollider, create a ColliderBlobInstance from it, create a ColliderBlobInstance from your sphere collider, put these 2 instances in the array and provide it to CompoundCollider.Create().

    The layout in these two is slightly different, but functionality will be the same. Just don't do the second one many times in a row, because it will eventually break because of too much nesting. First one you can do indefinitely (well not really, but still a lot more).
     
    CristianOG and RecursiveEclipse like this.