Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to apply child PhysicsVelocity to parent compound entity?

Discussion in 'Physics for ECS' started by hxhloveunity, May 10, 2024.

  1. hxhloveunity

    hxhloveunity

    Joined:
    May 27, 2022
    Posts:
    42
    I have a compound entity whose collider is composed of colliders from entities A, B, and C merged together. After merging, I removed the PhysicsWorldIndex from EntityA, B, and C. I want to continue applying the PhysicsVelocity.Linear of EntityA or B or C to the compound entity.

    How can I achieve this?
     
  2. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    326
    When you say compound entity, what do you mean by this?
    Note that dynamic rigid body entities in a hierarchy are always de-hierarchized (their parent/child relations split) and each rigid body has a LocalTransform component defining its world transformation and a PhysicsVelocity component defining its linear and angular velocity in world space.

    Note also that during baking collider authoring components within hierarchies of game objects are combined into a CompoundCollider blob asset assigned to the PhysicsCollider component of the nearest ancestor rigid body entity in the hierarchy on the path to the root.
    Any collider within a child game object will therefore always follow the ancestor rigid body.

    So, given all that, what is it exactly that you are doing and that you want to achieve?
     
  3. hxhloveunity

    hxhloveunity

    Joined:
    May 27, 2022
    Posts:
    42
    During runtime in my game, there's a weld tool that merges selected entities into a compound entity (creating a new Physics Collider using CompoundCollider.Create), and the PhysicsWorldIndex of the specified entities is removed. To ensure that the behavior of these entities follows the movement of the compound entity, I set the parent of the entities to be the compound entity.

    As a result, child entities are no longer involved in physics simulation. However, I still want certain child entities' physical properties, such as PhysicsVelocity, to continue affecting the compound entity.

    some case :

    upload_2024-5-10_13-59-58.png

    child a is a thruster, it's PhysicsVelocity.Linear = Forward * force
     
  4. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    326
    Physically speaking, you are creating a single rigid body.
    So your compound body can only have one linear and one angular velocity (that is one PhysicsVelocity), since that single body moves along one single trajectory at all times, as per the definition of a rigid body.

    You need to define more what the physical effect is you are trying to achieve here.
    How should the child entities still affect the entire compound body?

    My understanding based on the details provided above is this scenario:
    If, for example, you want the child to act like a thruster which applies an impulse (or force) at its center with the expected effect on the compound body, you can simply apply an impulse to the parent rigid body entity (the one that holds the PhysicsVelocity component) at the position of the child entity.
    This can be done using the PhysicsVelocity.ApplyImpulse() function.
     
    Last edited: May 10, 2024
  5. hxhloveunity

    hxhloveunity

    Joined:
    May 27, 2022
    Posts:
    42
    I tried using PhysicsVelocity.ApplyImpulse, but it resulted in accumulation every frame, eventually causing the PhysicsVelocity to become very large. I also attempted to limit the impulse based on the current PhysicsVelocity, but the simulation results were not satisfactory.

    here is some of my code :

    Code (CSharp):
    1. Entities
    2.     .WithStructuralChanges()
    3.     .ForEach((Entity entity, ref PhysicsVelocity physicsVelocity, in PhysicsMass physicsMass, in LocalTransform localTransform, in CompoundGraph compoundGraph) => {
    4.         foreach (var kv in compoundGraph.graph) {
    5.             Entity nodeEnt = kv.Key;
    6.             var nodeVelocity = EntityManager.GetComponentData<PhysicsVelocity>(nodeEnt);
    7.             var nodeMass = EntityManager.GetComponentData<PhysicsMass>(nodeEnt);
    8.  
    9.             var pos = math.transform(new RigidTransform(localTransform.Rotation, localTransform.Position), compoundGraph.childToCompound[nodeEnt].pos);
    10.             ApplyImpulse(ref physicsVelocity, physicsMass, localTransform.Position, localTransform.Rotation, nodeVelocity.Linear * math.rcp(nodeMass.InverseMass), pos);
    11.         }
    12.     }).Run();
    13.  
    14. public void ApplyImpulse(ref PhysicsVelocity pv,
    15.     in PhysicsMass pm,
    16.     in float3 t,
    17.     in quaternion r,
    18.     in float3 impulse,
    19.     in float3 point) {
    20.  
    21.     // Linear
    22.     var linearImpulse = impulse * pm.InverseMass;
    23.     pv.Linear = ApplyLimitImpulse(pv.Linear, linearImpulse);
    24.  
    25.     // Angular
    26.     {
    27.         // Calculate point impulse
    28.         var worldFromEntity = new RigidTransform(r, t);
    29.         var worldFromMotion = math.mul(worldFromEntity, pm.Transform);
    30.         float3 angularImpulseWorldSpace = math.cross(point - worldFromMotion.pos, impulse);
    31.         float3 angularImpulseInertiaSpace = math.rotate(math.inverse(worldFromMotion.rot), angularImpulseWorldSpace);
    32.         float3 angularImpulse = angularImpulseInertiaSpace * pm.InverseInertia;
    33.         pv.Angular = ApplyLimitImpulse(pv.Angular, angularImpulse);
    34.     }
    35. }
    36.  
    37. public float3 ApplyLimitImpulse(float3 velocity, float3 impulse) {
    38.     return new float3(
    39.         ApplyLimitImpulse(velocity.x, impulse.x),
    40.         ApplyLimitImpulse(velocity.y, impulse.y),
    41.         ApplyLimitImpulse(velocity.z, impulse.z)
    42.     );
    43. }
    44.  
    45. public float ApplyLimitImpulse(float velocity, float impulse) {
    46.     float signImpulse = math.sign(impulse);
    47.  
    48.     if (signImpulse > 0 ? velocity >= impulse : velocity <= impulse) {
    49.         return velocity;
    50.     }
    51.  
    52.     velocity += impulse;
    53.     float signVelocity = math.sign(velocity);
    54.  
    55.     if (signVelocity * signImpulse > 0) {
    56.         velocity = signVelocity * math.min(math.abs(velocity), math.abs(impulse));
    57.     }
    58.  
    59.     return velocity;
    60. }
     
  6. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    326
    You said you had removed the PhysicsWorldIndex. That would lead to accumulation if you apply the impulse on an Entity without it.
    Make sure that when merging these into a single Entity that you have all the necessary components (see this documentation section).

    For how to correctly apply impulses see also the "Motion / Impulse" sample scene in the PhysicsSamples project.