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

Discussion in 'Physics for ECS' started by linfuqing, Jan 15, 2020.

1. ### linfuqing

Joined:
May 11, 2015
Posts:
166
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?

2. ### edalbeci

Joined:
Jan 21, 2018
Posts:
36
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):

Code (CSharp):
1. using Unity.Entities;
2. using Unity.NetCode;
3. using Unity.Mathematics;
4. using Unity.Physics;
5. using Unity.Transforms;
6. using Unity.Collections;
7. using Unity.Physics.Systems;
8. using Unity.Physics.Extensions;
9.
10. public class TestComponentSystem : ComponentSystem
11. {
12.     BuildPhysicsWorld physicsWorldSystem;
13.
14.     protected override void OnCreate()
15.     {
16.         physicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
17.         base.OnCreate();
18.     }
19.
20.     protected void TestOverlapAabb()
21.     {
22.         Entities.ForEach((Entity entity, ref Translation trans) =>
23.         {
26.
27.             Aabb aabb = new Aabb()
28.             {
29.                 Max = trans.Value + radius3,
30.                 Min = trans.Value - radius3
31.             };
32.
33.             OverlapAabbInput input = new OverlapAabbInput()
34.             {
35.                 Aabb = aabb,
36.                 Filter = CollisionFilter.Default
37.             };
38.
39.             NativeList<int> hits = new NativeList<int>(Allocator.Temp);
40.
41.             CollisionWorld collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld;
42.             collisionWorld.OverlapAabb(input, ref hits);
43.
44.             PhysicsWorld world = physicsWorldSystem.PhysicsWorld;
45.
46.             foreach (int index in hits)
47.             {
48.                 RigidBody r = world.Bodies[index];
49.                 UnityEngine.Debug.Log(r);
50.             }
51.         });
52.     }
53.
54.     protected override void OnUpdate()
55.     {
56.         TestOverlapAabb();
57.     }
58. }
59.

3. ### MaxAbernethy

Joined:
Mar 16, 2019
Posts:
53
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.

Baggers_ likes this.
4. ### linfuqing

Joined:
May 11, 2015
Posts:
166
Yes,but PointDistanceInput.MaxDistance mean distance to vertices,I want a distance to rigidbody surfaces ...

5. ### MaxAbernethy

Joined:
Mar 16, 2019
Posts:
53
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!

Baggers_ likes this.