Search Unity

Resolved Kinematic parent-child physics

Discussion in 'Physics for ECS' started by Krooq, Apr 17, 2022.

  1. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    I have what is quickly becoming a very complicated problem.
    My setup is a movable platform with destructible objects on top, so authoring roughly like this.

    - platform (kinematic)
    -- destructible object (no physics, just a transform)
    --- object fragment (kinematic but should become dynamic when "activated")

    The problem I'm having is that every object fragment is unparented during conversion so when I move the platform, the object fragments don't move with it.
    Ideal behaviour is that the fragments act as though they are plain ol children until they are activated at which point they can become their own dynamic physics objects and detach from the platform.
    My questions are
    1. Why do kinematic physics objects "unparent" during conversion?
    2. How can I achieve what I'm after?

    I should also note that I'm using Netcode and that the platform is a ghost.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,508
    What's exactly "conversion" in this case? If you mean instancing the object fragments, then you can specify the parent object in the same call to Object.Instantiate.
    EDIT: Ok, discard my reply. I didn't realized I was in the DOTS forum, sorry. :oops:
     
    Last edited: Apr 17, 2022
  3. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    This is the way Unity decided to architect this package. Without unparenting, for example, child's collider will get backed into platform's one. Nothing we can do here.

    It sounds like the easiest way would be simply re-parenting those entities post conversion

    Code (CSharp):
    1.  
    2. [UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
    3. public class PostConvSys : GameObjectConversionSystem
    4. {
    5.     protected override void OnUpdate()
    6.     {    
    7.         Entities
    8.         .ForEach(
    9.             (_your_auth_component_ yac) =>
    10.             {
    11.                 Entity e = GetPrimaryEntity(yac);
    12. // do stuff
    13.             }
    14.         );
    15.     }
    16. }
    17.  
     
  4. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    That seems too easy...
    Do you know if its a physics conversion system that handles the unparenting or whether it is a runtime system that would undo these efforts?
    It looks like the conversion system also adds a PhysicsWorldIndex component only if the physics body has no physics body in its parents.
     
  5. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    I hadn't touched 0.50, so can't tell anything regarding PhysicsWorldIndex. In 0.17 unparenting is part of the regular PhysicsBody conversion, removal of <Parent>/<Child> components, to be precise.

    In my case children have no physics, so the following line in parent's authoring is enough:
    Code (CSharp):
    1. dstManager.AddBuffer<Child>(entity);
    But if your use case is more complex, the generic post conversion should do the trick. I use it all over the place to clean-up after default conversion.
     
    Krooq likes this.
  6. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    Thanks @WildMaN.
    I tried this and it does parent the objects correctly however there was a bunch of other issues with the parenting approach that I couldn't work around, e.g. collider baking into a parent physics entity.

    I think fixed joints might be the right way to go, I'll try that next.

    If the DOTS physics team is watching this, I'd like to say that the hierarchy resolution on the authoring side of things definitely doesn't serve my "simulation" use case.
    I'm sure you guys are aware of this, I just want to add my feedback with the hope that maybe some better options for "composite assemblies" can be included.
    Prefabs only have a single hierarchy so having one way of resolving the physics for a hierarchy is very restrictive.
     
    Last edited: Apr 28, 2022
  7. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    I tried the fixed joint solution but I don't think it works for kinematic objects, which I should have figured since thats sorta the whole point of being kinematic.

    Back to the drawing board, I tried the reparenting approach again with manually created colliders to work around the compound collider authoring issue.
    Everything is hooked up correctly, parent assigned, local to parent added, physics velocity and mass (kinematic) added, collider is there, so far so good.
    However, even with all this set up, the local to world is not changing to follow the parent as it was before.

    At this point I'm thinking I'll just create a system that copies over the "parent-but-not-parent" physics velocity to each fragment while they are inactive.
     
    Last edited: Apr 29, 2022
  8. Krooq

    Krooq

    Joined:
    Jan 30, 2013
    Posts:
    194
    OK, I've doubled back again.

    The whole issue is basically resolved by treating the children fragments as collider only objects. i.e. no physics velocity or mass.
    The compound collider baking issue is resolved by this demo

    Essentially you need to traverse the collider hierarchy from the parent physics body to find the actual entity that authored the collider mesh instead of the physics body.

    Its really messy and painful but it works.
    I can't begin to understand all the decisions as to why it was done this way but I'm sure there are valid reasons.
    I just wish it was included in the documentation because this was hard to figure out.
     
  9. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128
    Have you tried the postconverse system? Should work like a charm - recreate parent-child components and voila. There is no black magic in parenting, it's just a bunch of systems operating over a couple of components/buffers.