Search Unity

Bug Children meshes do not update position with a parent rigid body

Discussion in 'Physics for ECS' started by argibaltzi, Dec 19, 2022.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Hi

    I have a problem with creating a dynamic body via code. Everything seems to be working except that the rigid body has some mesh children, but their positions do not get updated.....

    Is there something wrong here or am missing something? My code used to work just fine in 0.50.

    I can also see in scene view(debug) that the rigid body is in another position (where it should be) but the children are reset at 0,0,0.

    It seems that the parent transform is not working or something.

    Thanks!
     
  2. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Code (CSharp):
    1. public static void CreateDynamicBody(Entity entity, float mass, float3 startAngularVelocity, float3 startLinearVelocity, float gravityFactor, float linearDamping, float angularDamping, RigidTransform previousTransform, ref PhysicsCollider colliderComponent, ref EntityCommandBuffer cmdBuffer, EntityManager entityManager)
    2.     {
    3.         PhysicsMass physicsMass = PhysicsMass.CreateDynamic(colliderComponent.MassProperties, mass);
    4.         float3 angularVelocityLocal = math.mul(math.inverse(colliderComponent.MassProperties.MassDistribution.Transform.rot), startAngularVelocity);
    5.         PhysicsVelocity velocityData = new PhysicsVelocity()
    6.         {
    7.             Linear = startLinearVelocity,
    8.             Angular = angularVelocityLocal
    9.         };
    10.  
    11.         PhysicsDamping damping = new PhysicsDamping()
    12.         {
    13.             Linear = linearDamping,
    14.             Angular = angularDamping
    15.         };
    16.  
    17.         if (gravityFactor != 1.0f)
    18.         {
    19.             PhysicsGravityFactor gravity = new PhysicsGravityFactor()
    20.             {
    21.                 Value = gravityFactor
    22.             };
    23.  
    24.             if (cmdBuffer.IsCreated)
    25.             {
    26.                 cmdBuffer.AddComponent(entity, gravity);
    27.             }
    28.             else
    29.             {
    30.                 entityManager.AddComponentData(entity, gravity);
    31.             }
    32.         }
    33.  
    34.  
    35.         PhysicsGraphicalSmoothing smoothing = new PhysicsGraphicalSmoothing() { ApplySmoothing = 0, CurrentVelocity = velocityData };
    36.         PhysicsGraphicalInterpolationBuffer interpolationBuffer = new PhysicsGraphicalInterpolationBuffer() { PreviousTransform = previousTransform, PreviousVelocity = new PhysicsVelocity() };
    37.  
    38.         if (cmdBuffer.IsCreated)
    39.         {
    40.             cmdBuffer.AddComponent<PhysicsGraphicalSmoothing>(entity, smoothing);
    41.             cmdBuffer.AddComponent<PhysicsGraphicalInterpolationBuffer>(entity, interpolationBuffer);
    42.             cmdBuffer.AddComponent(entity, physicsMass);
    43.             cmdBuffer.AddComponent(entity, velocityData);
    44.             cmdBuffer.AddComponent(entity, damping);
    45.  
    46.         }
    47.         else
    48.         {
    49.             entityManager.AddComponentData<PhysicsGraphicalSmoothing>(entity, smoothing);
    50.             entityManager.AddComponentData<PhysicsGraphicalInterpolationBuffer>(entity, interpolationBuffer);
    51.  
    52.             entityManager.AddComponentData(entity, physicsMass);
    53.             entityManager.AddComponentData(entity, velocityData);
    54.             entityManager.AddComponentData(entity, damping);
    55.         }
    56.     }

    Everything comes from bakers and works fine, but when i run this code to make the parent a rigid body (it also has a simple box collider on it), the children stop getting updated.

    What is wrong?