Better way to give physics to imported GLTF/GLB scene meshes?

Hello, I have a question with a Playground link now!

I’m trying to add colliders (aka “impostors” in Babylon) with the default CannonJS implementation, and it’s messing up the locations and rotations of all my objects from the original scene.

Here is what my scene is supposed to look like:

Here is what my scene looks like after I assign impostors to all my meshes:

From what I understand, this is a side effect stemming from the impostor creation system only working on objects in local space, meaning they have to be unparented. (For context, every GLTF or GLB scene has all its mesh objects parented to a single __root__ node by default.)

I used this solution that unparents each mesh, adds the collider/impostor, and then reparents it. Unfortunately, unparenting the mesh from __root__ seems to make it lose its original placement, and I have to hard-code its location and rotation back into my scene.

Is there a more efficient way to implement physics on a multi-object .gltf/.glb scene? Maybe there’s a method to implementing a physics engine like Rapier or cannon-es that doesn’t have the local space-only requirement of Babylon impostor objects? Thanks for reading

1 Like

cc @Cedric and @carolhmj :slight_smile:

1 Like

Try using setParent instead of parent directly.

GLB Parenting and Babylon Impostors | Babylon.js Playground (babylonjs.com)

1 Like

That’s a step in the right direction, thank you so much!

Seems like the placements are almost there now…

The __root__ parent is only created to convert between GLTF and Babylon’s different handednesses, so you could alternatively just bake the transform in instead and not worry with parenting: Babylon.js Playground (babylonjs.com)

2 Likes

Another option is scene.useRightHandedSystem = true so that the glTF loader doesn’t need to convert handedness.

3 Likes

@carolhmj @bghgary Thank you for the great solutions! I love how thin the code gets from being able to bake or discard one of the two handedness systems.

I’m going to try implementing all the suggestions in this thread (while also switching to Ammo.js since it seems to have more active maintainers and Babylon features,) and I’ll post here again later (either with any more issues I run into, or the finished Playground of everything in this thread successfully put together)

1 Like

Great news! My Playground scene is working flawlessly now.

THE SOLUTIONS:

  • @bghgary: use .setParent() and .useRightHandedSystem = true
  • @carolhmj: bake the mesh transforms and discard __root__ (requires async function)
  • There were some remaining mesh positioning bugs, but switching to AmmoJS fixed them. Yay!

In conclusion: don’t use abandoned physics engines like poor CannonJS… (cannon-es is fine)

4 Likes

Regarding

    mesh.checkCollisions = false; // not sure why we're disabling this but trust it

Since we use physics collision system here we may disable the default mesh collision checking.

3 Likes