How can i get all the faces/triangles with a specific distance?

PhysicsWorld.CalculateDistance can calculate the vertices of rigid body with a specific distance.
Sometime my input point is very close to a large face,but PhysicsWorld.CalculateDistance always return false.
Is there any method like UnityEngine.Physics.OverlapSphere to find all rigidbodies in a specific distance to face/triangle?

I don’t think OverlapSphere exists, yet. I managed to eke out a functioning OverlapAabb though, included below (it was cobbled together, as I tried to piece together the reference, so it may not be the correct way to do things):

TestComponentSystem.cs

using Unity.Entities;
using Unity.NetCode;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using Unity.Collections;
using Unity.Physics.Systems;
using Unity.Physics.Extensions;

public class TestComponentSystem : ComponentSystem
{
    BuildPhysicsWorld physicsWorldSystem;

    protected override void OnCreate()
    {
        physicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
        base.OnCreate();
    }

    protected void TestOverlapAabb()
    {
        Entities.ForEach((Entity entity, ref Translation trans) =>
        {
            float radius = 3;
            float3 radius3 = new float3(radius, radius, radius);

            Aabb aabb = new Aabb()
            {
                Max = trans.Value + radius3,
                Min = trans.Value - radius3
            };

            OverlapAabbInput input = new OverlapAabbInput()
            {
                Aabb = aabb,
                Filter = CollisionFilter.Default
            };

            NativeList<int> hits = new NativeList<int>(Allocator.Temp);

            CollisionWorld collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld;
            collisionWorld.OverlapAabb(input, ref hits);

            PhysicsWorld world = physicsWorldSystem.PhysicsWorld;

            foreach (int index in hits)
            {
                RigidBody r = world.Bodies[index];
                UnityEngine.Debug.Log(r);
            }
        });
    }

    protected override void OnUpdate()
    {
        TestOverlapAabb();
    }
}

Hello, have you tried setting PointDistanceInput.MaxDistance / ColliderDistanceInput.MaxDistance? By default they are zero, which means that the query will only return hits if the point / collider intersects a shape in the world. But if you increase the MaxDistance, then the query will return hits as long as the distance from the point / collider to the shape in the world is no greater than MaxDistance. Note that increasing MaxDistance will make queries more expensive since they have to search further away for hits, so don’t make it larger than you have to.

1 Like

Yes,but PointDistanceInput.MaxDistance mean distance to vertices,I want a distance to rigidbody surfaces …

CalculateDistance returns the distance to the closest point on the hit body, whether it’s on a vertex, edge, or face of the body’s shape, and MaxDistance filters the hits based on that distance. If you found a case where CalculateDistance is returning the distance to a vertex when there is a closer point somewhere else on the rigid body, that would be a bug. Could you please share it so we can reproduce the problem? Thanks!

1 Like