Search Unity

Question Convert not applying Translation to object with PhysicsBodyAuthoring component

Discussion in 'Physics for ECS' started by metageist, Feb 6, 2021.

  1. metageist

    metageist

    Joined:
    Mar 16, 2019
    Posts:
    27
    I have an IConvertGameObjectToEntity object with a custom Convert method. At runtime, a multitude of these are cloned and arranged in a grid by adding a LocalToWorld and Translation ComponentData to the resulting entity.

    However, I also need these object to have properties of a static physics body, so I gave the original a PhysicsBodyAuthoring component. Since then, all clones generate at the same position and each of their Translation values remain at zero.

    How do i force Convert to move my object before its Physics properties take over?
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    If there isn't a PhysicsBodyAuthoring component on it, then it will already default to being a static body.
    If there was no PhysicsBodyAuthoring component in the tree then you may have been creating a single static Body with a CompoundCollider (which could be the most optimal option). The StaticOptimizeEntity component is also considered in the conversion and could have ended up being the root Entity of holding a CompoundCollider.

    Certain sample Authoring components need the Physics info in place before they are converted. These components implement the IConvertGameObjectToEntity interface but don't extend the interface so they can be explicitly converted.
    IConvertGameObjectToEntity components are converted before the Physics conversion systems.
    I recommend making your own ConversionSystem to ensure the ordering of conversion before or after the Physics ConversionSystems. Check out the PhysicsSamplesConversionSystem in the samples, for reference.
     
  3. metageist

    metageist

    Joined:
    Mar 16, 2019
    Posts:
    27
    Apologies if I failed to express myself properly;

    I was looking for a method to use both a PhysicsBodyAuthoring component as well as a custom Convert method on the same GameObject, because i did not know how to add a PhysicsCollider component to an entity at runtime.

    The conflict arose from my LocalToWorld + Translation from the Convert to be ignored because the authoring component's physics properties overtook control of the entity's position and prevented the entity from being teleported using the translation.

    While I did not find a solution to make the two work in the order i wanted them, I instead finally figured out how to add a PhysicsCollider component to my entity at runtime. For those curious, here was my basic solution:
    Code (CSharp):
    1. void BuildCell(Entity cell)
    2. {
    3.  //{Add LocalToWorld and Translation components here}
    4.  
    5.    RenderMesh rm = EntityManager.GetSharedComponentData<RenderMesh>(cell);
    6.  
    7.    float3[] cVertices = Array.ConvertAll<Vector3, float3>(rm.mesh.vertices, (_ => (float3)_));
    8.  
    9.    NativeArray<float3> cVerticesArray= new NativeArray<float3>(cVertices, Allocator.Temp);
    10.  
    11.    BlobAssetReference<Unity.Physics.Collider> blob = ConvexCollider.Create(cVerticesArray, new ConvexHullGenerationParameters());
    12.  
    13.    PhysicsCollider pcoll = new PhysicsCollider() { Value = blob};
    14.    PostUpdateCommands.AddComponent<PhysicsCollider>(cell, pcoll);
    15.  
    16.    cVerticesArray.Dispose();
    17. }