Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug [SOLVED] ConvertToEntity PhysicsShape.BelongsTo always sets first bit?

Discussion in 'Physics for ECS' started by lndcobra, Nov 5, 2020.

  1. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    I have a PhsyicsShape on a prefab which has ConvertToEntity with the following selected for BelongsTo

    upload_2020-11-5_0-55-38.png

    When trying to use a collision filter, in a raycast, the hit result of that object always has first bit set (irrespective of selection, making entry 0 useless?)

    Code (CSharp):
    1. var raycastInput = new RaycastInput
    2. {
    3.     Start = mouseRay.origin,
    4.     End =  mouseRay.GetPoint(_farClipPlane),
    5.     Filter = new CollisionFilter
    6.     {
    7.         CollidesWith = 1u << 0,
    8.         BelongsTo =    1u << 0,
    9.     }
    10. };
    11.  
    12. physicsWorld.CastRay(raycastInput, out var hit);
    The above gets set as 5 (0b00101) rather than what I would expect which is 4 (0b0100).

    Checking/Unchecking entry 0, looks like it makes no difference to the filter, no matter what bit 0 is always set.

    Not sure if this is expected behaviour and I just couldn't find it anywhere in the docs? (I am new to Unity)

    I am using Unity 2020.1.11f1 and Unity Physics 0.5.1-preview.2
     
    Last edited: Nov 5, 2020
  2. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    Did some more testing and in the following Categories

    upload_2020-11-5_1-18-18.png
    0: = 0b0001
    1: = 0b0011
    2: = 0b0101
    3: = 0b1001

    In another place I have a IConvertGameObjectToEntity which seems to be set correctly, 1,2,4,8 etc.

    Code (CSharp):
    1. public class PhysicsTerrainAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     [SerializeField] PhysicsCategoryTags belongsTo;
    4. }
     
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    This is really strange, especially since you have another place where things work fine. Do the default physics categories work fine for you (PhysicsCategoryNames)? Like the ones you would see by default in the samples project.

    Edit: to answer your question, it's normal to have values 1, 2, 4, 8, ... so it's definitely an issue if you have 0 always set.
     
  4. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    It works when I set it using a custom field, but doesn't work when I set it on the "PhysicsShape".

    This is broken (Set to 0b11 which is 3 in decimal):
    upload_2020-11-6_19-13-59.png

    But this works (Set to 0b1000 which is 8 in decimal)

    upload_2020-11-6_19-14-21.png

    That script is simply has

    Code (CSharp):
    1. [SerializeField] PhysicsCategoryTags belongsTo;
    2. [SerializeField] PhysicsCategoryTags collidesWith;
    3. [SerializeField] int groupIndex;
    Just made a sample project with a cube and will attach.

    I am guessing there is a bug in the Conversion workflow for PhysicsShape
     
    Last edited: Nov 6, 2020
  5. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    You will see the Cube has set 2, but in Console you will see Collider Filter has 3.
     

    Attached Files:

  6. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    I can also see the editor saves it correctly into the files at least:

    upload_2020-11-6_19-24-3.png
     
  7. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    I'll take a look and get back to you. Thanks for the repro!
     
  8. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The problem seems to be due to the fact that the Cube has both a legacy BoxCollider and a PhysicsShape component.

    This means for your Cube body, you are actually creating a CompoundCollider with 2 BoxColliders as children - one from the legacy BoxCollider and setting the CollisionFilter.BelongsTo based on the gameObject.layer, and the second from the PhysicsShape component with your own settings that you are expecting.

    At the top level the CompoundCollider combines the CollisionFilters of all the children into a single Filter for the compound, which is why you are seeing the first bit set (from the gameObject.layer) and the other bits from the BelongsToCatergories.

    Ditch the BoxCollider and things are fine.
     
    lndcobra and petarmHavok like this.
  9. lndcobra

    lndcobra

    Joined:
    Jan 28, 2020
    Posts:
    21
    Sorry, wasn't aware of that! Thanks!
     
  10. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Just ran into this and spent a few hours debugging it. Would be nice if there was some kind of warning message in the physics shape inspector letting you know that you're basically doubling up your collider and messing up your filters by having the legacy collider on there too
     
    Antypodish likes this.
  11. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Thanks for the suggestion, we'll definitely consider it.
     
  12. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Adding multiple colliders to one Body is a valid use-case though. If you get any of the children from the compound collider, they will have the expected filters on any single one of them. The problem is that the filter at the root is a combination of all the child filters. A warning could mention the mixing of legacy and dots colliders but it isn't actually a problem.