Search Unity

Help Wanted OverlapSphere like Unity.Physics?

Discussion in 'DOTS Physics' started by EagL-, Nov 29, 2020.

  1. EagL-

    EagL-

    Joined:
    Dec 9, 2016
    Posts:
    24
    I modified the SphereCast example from this: https://docs.unity3d.com/Packages/com.unity.physics@0.5/manual/collision_queries.html
    to return all hits. Basically just adding this:
    Code (CSharp):
    1. NativeList<ColliderCastHit> allHits = new NativeList<ColliderCastHit>(Allocator.Temp);
    2. bool haveHit = collisionWorld.CastCollider(input, ref allHits);
    The thing is that it's returning the same entity multiple times because it hits each entity multiple times. Is it just about removing duplicates or am I missing something? Goal is for it to work similar to Unity.Physics.OverlapSphere where it return all colliders (/entities) that are inside or touch the sphere.
     
    Last edited: Nov 29, 2020
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,027
    have you considered just using CalculateDistance instead of CastCollider?

    physicsWorld.CalculateDistance(PointDistanceInput input, ref NativeList<DistanceHit> allHits);
     
  3. EagL-

    EagL-

    Joined:
    Dec 9, 2016
    Posts:
    24
    That actually works better for my use case so thank you, but it still doesn't solve the problem I'm having with the same entity ending up multiple times in allHits for some reason.
     
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You want to check the ColliderKey of the hits as well. They should be different. The reason is that you are hitting multiple instances of the same CompoundCollider, represented by one entity (one body), but different collider keys representing different child colliders. Use CompoundCollider.GetChild() to get these colliders. You'll see they are different hits.
     
  5. EagL-

    EagL-

    Joined:
    Dec 9, 2016
    Posts:
    24
    Ok, that makes sense. So if I want a list of unique entities in allHits I have to remove the duplicates? There's no way around that?
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    No, there is no way around that. Also, I wouldn't remove duplicates unless you only need the actual entities that were hit.

    We had an ask once to only report one hit per entity. It's a reasonable thing to ask for, but we haven't found a performant way to achieve that due to the multithreaded nature of the physics solver. We'll keep looking.
     
  7. Endlesser

    Endlesser

    Joined:
    Nov 11, 2015
    Posts:
    44
    Hi, there might be an underlying performance lagging related to this, u see when using `CastCollider` with `allHits`, it gets a massive amount of `ColliderCastInput`, especially colliding with Mesh Collider.

    upload_2021-6-7_10-44-50.png
    upload_2021-6-7_10-45-4.png
    upload_2021-6-7_10-45-21.png

    `CastCollider` with no `ColliderCastHit`
    `CastCollider` with `closestHit`
    `CastCollider` with `allHits`

    They've been tested under an updating system at every frame, they don't make any bumping and all run smoothly when collided with simple colliders. But with mesh collider, hundreds of hits are generated and FPS is 10 lower, even I only asked for `closestHit` it's still 10 times slower than the 1st one(see pic).
     
unityunity