Problems with BJS parenting and Havok physics

Playground https://playground.babylonjs.com/#DELNSK#1

I’m having trouble getting Havok shapes and bodies working right when Babylon.js parenting is involved. I made the above playground to show the kinds of problems I’m having. There’s a root node with a box child. I move/rotate the root node somewhere. When I make the physics shape, I include the root node’s position/rotation in the shape parameters. This looks good at first, but after 3 seconds I change the body from static to dynamic and as it falls you can see the physics body is no longer in sync with the BJS mesh.

I think this is because Havok doesn’t know about BJS parenting and so it’s applying transforms to the child box.

I’m looking for a way to have a root node that can hold a bunch of child objects that have physics. If I’m looking at this the wrong way, please let me know.

@RaananW @Cedric @carolhmj Any ideas?

Instead of adding the physic body to the child, you can add it to the parent and use a PhysicsShapeContainer to add children shapes :smiley: phyics and parenting problems | Babylon.js Playground (babylonjs.com)
Physics compounds | Babylon.js Documentation (babylonjs.com)

1 Like

Thank you, I did see an example of that in the docs. As I understand the container method, the child shapes are basically welded together and move as one unit.
But for my case, I would need the children of the root to each have their own bodies and own shapes and be able to move independently of each other. Picture a vehicle with doors/antennas/panels/whatever that can move or flap open and shut realistically, and detach realistically when shot. It would be nice to have all the vehicle parts under one root rather than a collection of top-level nodes.
I know I can find a way to do it without a parent/child setup, but I’m just wondering if I’m missing anything else.

Hmmmmm @eoin do you see a way of doing that?

Parenting isn’t properly handled by the plugin right now, it seems. In HavokPlugin.syncTransform (Babylon.js/havokPlugin.ts at master · BabylonJS/Babylon.js · GitHub), the syncing code takes the world space transform from the physics engine and just applies that to the transform node associated with the body, which essentially causes the parent transform to be applied twice.

To handle hierarchies like this and have everything line up correctly, that code would need to multiply the physics transform by the inverse of the world transform of the parent node. I totally get the motivation for wanting to structure your scene like that, but the golden path in Babylon right now is definitely to ensure that the physics body’s transformNode has no parent.

4 Likes

After looking at .executeStep and .sync/.syncTransform in HavokPlugin I see what you mean. At first glance it looks like I could do this myself after executeStep is done. But consider this as a humble request to apply this inverse parent transform as an option in the future. Thank you

Requested and captured here [Physics] Apply inverse parent transformation to child bodies · Issue #13782 · BabylonJS/Babylon.js (github.com) :saluting_face:

1 Like