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] CollisionWorld.Bodies[hit.RigidBodyIndex].Entity returns the same Entity for all hits

Discussion in 'Physics for ECS' started by Opeth001, Apr 22, 2020.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    Hello Everyone,

    i found out that all static Entities having a PhysicsShape and not PhysicsBody are returning the same Collided entity id wih this API
    Code (CSharp):
    1.        
    2.  // RayCast Input
    3.    RaycastInput raycastInput = new RaycastInput()
    4.    {
    5.            Start = translation.Value,
    6.             End = nextFramePosition,
    7.             Filter = CollisionFilter.Default
    8.     };
    9.  
    10.                
    11.      if (collisionWorld.CastRay(raycastInput, out var hit ))
    12.          {  
    13.                // The Physics Entity hit by the Projectile  
    14.                 var entityCollided = collisionWorld.Bodies[hit.RigidBodyIndex].Entity;
    15.                 Debug.Log($"entityCollided:{entityCollided}");
    16.            }        
    17.  
    18.  
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Can you check the hit.ColliderKey as well? The all the PhysicsShapes have likely been compounded into a single CompoundCollider under a single Entity. So the Entity id will be the same but the ColliderKey will represent the leaf Collider actually hit.
     
    Opeth001 likes this.
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Is there a built-in possibility of getting the ChildCollider or Entity from the ColliderKey or do we have to store the mapping ourselves?

    (I have the same issue where I have different colliders – armor plates of different thicknesses – connected and am wondering whether the idea is to all connect these via fixed joints … or get the ChildCollider or Entity somehow)
     
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,112
    thanks that was the error :)
     
    steveeHavok likes this.
  5. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Any hit result should already include the Entity along with the RigidBodyIndex & ColliderKey, so you shouldn't need to go to the Bodies array.
    We did consider adding the child collider directly to the results. Can I ask what you need from the child collider directly?
     
    florianhanke likes this.
  6. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    The situation I have is a tank with multiple armor plates of different sizes, thicknesses, and materials. This is modelled as a parent entity (tank) - child entities (armor plates) structure. This is cheap for the TransformationSystem when they just need to stay in place.

    I assumed this to be a standard scenario, so the expectation was that from the hit I'd get the armor plate's entity. But I was getting the parent entity, which was surprising. After a bit of looking into it I noticed that the colliders are combined into a compound one, forgetting the original entities.

    So what I am getting is the parent entity from hit.Entity, but with a different ColliderKey, depending on what it hit. That makes sense.

    So now my question above makes more sense I hope… is the way I need to go about this:
    1. Not make it a parent-child relationship and use fixed joints for the armor plate - tank chassis connection so the colliders are not combined?
    or
    2. Keep it as is and use a method to calculate the original armor plate Entity e.g. from the collider key?
    or
    3. Keep it as is and hook into the compound collider code to remember a collider key -> original entity mapping?

    Thanks for any help! :)
     
  7. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
  8. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    florianhanke likes this.
  9. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    No worries, I am very glad to have an answer that I can work with :)