Search Unity

How to create a compound collider through script.

Discussion in 'Physics for ECS' started by roryo, Jul 21, 2021.

  1. roryo

    roryo

    Joined:
    May 21, 2009
    Posts:
    1,479
    I can add a collider to an Entity through script, but I am hoping to find out how to create a compound collider via script.

    In this function, a reference for a single collider is being passed in. If I were to pass in an array of collider references, how could a compound collider be defined for this entity?

    Code (CSharp):
    1.  
    2.  
    3.    unsafe Entity InstantiateBlockAsEntity(Entity entity, RenderMesh _renderMesh, BlobAssetReference<Collider> _collider, float mass, Matrix4x4 m, PhysicsVelocity physicsVelocity)
    4.     {
    5.         // USE MATRIX TO GET POSITION AND ROTATION
    6.         // =======================================
    7.         var rigidTransform = Math.DecomposeRigidBodyTransform(m);
    8.  
    9.         // RENDER MESH
    10.         // ======================================
    11.         entityManager.AddSharedComponentData(entity, _renderMesh);
    12.         entityManager.SetComponentData      (entity, new RenderBounds     { Value = _renderMesh.mesh.bounds.ToAABB() });
    13.         entityManager.AddComponentData      (entity, new Translation      { Value = rigidTransform.pos });
    14.         entityManager.SetComponentData      (entity, new Rotation         { Value = rigidTransform.rot });
    15.  
    16.         // COLLIDER DATA
    17.         // ======================================
    18.         entityManager.SetComponentData      (entity, new PhysicsCollider  { Value = _collider });
    19.         Collider* colliderPtr = (Collider*)_collider.GetUnsafePtr();
    20.         entityManager.SetComponentData      (entity, PhysicsMass.CreateDynamic(colliderPtr->MassProperties, mass));
    21.         entityManager.SetComponentData      (entity, physicsVelocity);
    22.    
    23.         return entity;
    24.     }
    25.  
    26.  
    27.  
    Thanks for any help!
     
    Thygrrr likes this.
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    The CompoundDemo.cs script creates a CompoundCollider out of other child colliders.
     
    Zyblade likes this.
  3. roryo

    roryo

    Joined:
    May 21, 2009
    Posts:
    1,479
    Just what I needed! Thanks so much for pointing to this, @steveeHavok!