Search Unity

Resolved CalculateDistance hits return strange positions.

Discussion in 'Physics for ECS' started by TonyG_Plexsys, Dec 24, 2020.

  1. TonyG_Plexsys

    TonyG_Plexsys

    Joined:
    Jan 24, 2020
    Posts:
    4
    I am running into a strange issue when I try to use Unity.Physics.CalculateDistance in my program. I have a very simple setup, one entity at (0,0,100) and create a distance query like:

    Code (CSharp):
    1. var input= new PointDistanceInput{
    2.   Position = new float3(0,0,100),
    3.   MaxDistance = 1
    4. };
    5.  
    6. var hits = new NativeList<DistanceHit>(Allocator.Temp);
    7. World.CalculateDistance(colliderDistanceInput, ref hits);
    8.  
    hits comes back with 1, which is correct. The strange part is that if I display the hit.position, it shows something like (18, .003, 100) and when I move my camera, it changes the X and Y of the hit position. Seemingly changing to smaller values the closer the camera gets to the entity.

    What is the world is happening here? Any help would be greatly appreciated.
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @TonyG_Plexsys , did you maybe unintentionally use a wrong var in World.CalculateDistance call?
    I see you pasted:
    Code (CSharp):
    1. World.CalculateDistance(colliderDistanceInput, ref hits);
    Looking at the first part of your code snippet, I'd expect:
    Code (CSharp):
    1. World.CalculateDistance(input, ref hits);
    I'd suggest having a look at 3. Query\AllHitsDistanceTest scene, especially the UnityPhysicsSamples\Assets\Demos\3. Query\Scripts\QueryTester.cs script.
     
  3. TonyG_Plexsys

    TonyG_Plexsys

    Joined:
    Jan 24, 2020
    Posts:
    4
    Thank you very much for the reply.

    The code I gave above was just from recollection of the code I'm running, a more accurate version is the following:

    Code (CSharp):
    1. BuildPhysicsWorld physicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
    2.  
    3. CollisionWorld collisionWorld = physicsWorld.PhysicsWorld.CollisionWorld;
    4.  
    5. var filter = new CollisionFilter {
    6.   BelongsTo = ~ou,
    7.   CollidesWith = ~0u,
    8.   GroupIndex = 0
    9. };
    10.  
    11. var pointInput = new PointDistanceInput {
    12.   Position = new float3(0,0,100),
    13.   Filter = filter,
    14.   MaxDistance = 1
    15. };
    16. var hits = new Unity.Collections.NativeList<DistanceHit>(Unity.Collections.Allocator Temp);
    17. collisionWorld .CalculateDistance(pointInput, ref hits);
    I ultimately am trying to do a marquee selection feature and am using the dots physics for picking already. My idea was to use a ColliderDistance call and provide a frustum mesh that I create to match the marquee rectangle. To get things rolling I wanted to do some testing and found that the calculate distance function was giving strange values back.

    I have no doubt I'm doing something wrong somewhere, it's just that I'm at a loss as to where to look since the functionality seems straight forward and I'm using the same physics world type code for my picking algorithm and that seems to work well.
     
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hmmm, not sure, I don't see anything obviously wrong there.

    Could you please try reproducing the problem on 3. Query\AllHitsDistanceTest scene?
    I just tried it and didn't see any diff in hit.Position when moving the camera.

    I added an empty game object to the scene and a "Query Tester" script to it, playing with different script param values. Just keep Collider Mesh at "None", so that CalculateDistance from point is used (the point will be game object position, set in Transform component).
     
    TonyG_Plexsys likes this.
  5. TonyG_Plexsys

    TonyG_Plexsys

    Joined:
    Jan 24, 2020
    Posts:
    4
    Thanks again for your help :-D

    Apologies but I didn't test with the sample code, however I did try using collider input instead of point distance input. The collider is a sphere and the transform is the location I was testing against.

    Using the collider input seemed to have solved my issue of the positions/distances changing based on the camera distance. I don't understand exactly why this works over the other way but am very happy that my results are making sense now.
     
    petarmHavok likes this.