Search Unity

BuildPhysicsWorld.CastCollider does not accurately detect collisions.

Discussion in 'Physics for ECS' started by TaeRyong_Song, Dec 29, 2021.

  1. TaeRyong_Song

    TaeRyong_Song

    Joined:
    Feb 6, 2014
    Posts:
    6
    BuildPhysicsWorld.CastCollider does not accurately detect collisions.

    Hello, I'm checking the collision detection using DOTS.
    I think I'm using the API properly, but the results are not working as expected.Physics World at QueryTesters.cs in the example sample.Calculate Distance works perfectly. Is there anything I'm missing?

    The background modeling is a MeshCollider collision body, and the Cylinder modeling was converted into Convex as shown in the example and used as ColliderCastInput.
    The log captured on the console is the CastCollider result value.
    Background modeling has been converted to Entity.
    The diameter length of the cylinder is 3.3.

    Unity 2020.3.15f2
    It was tested on the Unity-TechnologiesEntityComponentSystemSamples sample.
    (https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/UnityPhysicsSamples)

    Code (CSharp):
    1.  
    2. public class ColldierCastSample : MonoBehaviour
    3. {
    4.     public Mesh originMesh;
    5.     public Transform target;
    6.     public CustomRenderer viewer;
    7.     public MeshRenderer cutRenderer;
    8.     public float radius = 0.5f;
    9.  
    10.     private bool preResult = false;
    11.     private Vector3 castDirection = new Vector3(0, -0.1f, 0);
    12.     BlobAssetReference<Collider> collider;
    13.     private unsafe Collider* colliderPtr;
    14.     private void Awake()
    15.     {
    16.         CreateConvexCollider();
    17.     }
    18.     private void OnDestroy()
    19.     {
    20.         if (collider.IsCreated)
    21.             collider.Dispose();
    22.     }
    23.     unsafe void CreateConvexCollider()
    24.     {
    25.         int count = originMesh.vertexCount;
    26.         var points = new NativeArray<float3>(count, Allocator.TempJob);
    27.         for (int i = 0; i < count; i++)
    28.             points[i] = originMesh.vertices[i];
    29.         collider = ConvexCollider.Create(points, ConvexHullGenerationParameters.Default);
    30.         colliderPtr = (Collider*)collider.GetUnsafePtr();
    31.         points.Dispose();
    32.     }
    33.     private void Update()
    34.     {
    35.         ColliderCast();
    36.     }
    37.  
    38.     unsafe void ColliderCast()
    39.     {
    40.         var world = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
    41.         var p = target.transform.position;
    42.    
    43.         bool result = world.CastCollider(new ColliderCastInput
    44.         {
    45.             Collider = colliderPtr,
    46.             Start = p,
    47.             End = p + castDirection,
    48.         });
    49.         print(result);
    50.         if(result != preResult)
    51.         {
    52.             cutRenderer.material.color = result ? Color.red : Color.green;
    53.             preResult = result;
    54.         }
    55.     }
    56. }
    57.  

     
    Last edited: Dec 29, 2021