What are the best practices for physics glTF models?

I simply want to make the glTF model fall using a physics engine.
My current code is like below. The current example is the Physics API V1+ammo.js example.
Physics seems to work correctly, but the eraser label and CD boombox display are displayed backwards.

BABYLON.SceneLoader.ImportMesh("", "<path>", "Eraser.glb", scene, function (meshes) {
    const mesh = meshes[1];
    mesh.parent = null;
    mesh.position.y = 3;
    
    mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 1, restitution: 0.6}, scene);

});

Eraser physics result:
image

Boombox physics result:
image

I think it’s probably because I set the root node to null, but I don’t think the physics calculations will work if I don’t do that.
What is the best way to do both physics and display correctly?

I tried porting to Physics API V2 + Havok.
However, this time it started penetrating through the floor.

// NG : Go through the floor. Resetting the scaling makes physics work.
BABYLON.SceneLoader.ImportMesh("", "https://cx20.github.io/generated-model-sandbox/models/gltf/Eraser/glTF-Binary/", "Eraser.glb", scene, function (meshes) {
    const mesh = meshes[1];
    mesh.parent = null;
    mesh.position.y = 3;
    
    //mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, {mass: 1, restitution: 0.6}, scene);
    new BABYLON.PhysicsAggregate(mesh, BABYLON.PhysicsShapeType.BOX, {mass: 1, friction: 0.5, restitution: 0.6}, scene);
});

Apparently the physics engine doesn’t seem to recognize the thickness of the eraser.
image

As a test, I reset the scaling and it worked. It seems that the problem of label orientation has also been improved.
However, I don’t know if this is the correct method.

mesh.scaling = new BABYLON.Vector3(1, 1, 1);

image

Let me add @carolhmj @Cedric and @bghgary to the thread. Almost the whole physics/gltf family

1 Like

Hello! Yeah, the scaling was the issue here, this is something we should take into account in the aggregate size calculations. I’ve opened a PR here: Fix aggregate extent calculation when scaling is negative by carolhmj · Pull Request #14230 · BabylonJS/Babylon.js (github.com)
With havok, you also don’t need to null the parent anymore, we’re taking the parent transforms into account :smiley: once the PR is merged, you can just do it like this: Test to drop glTF model with physics engine | Babylon.js Playground (babylonjs.com)

4 Likes

Thank you for the corrections. I believe my coding has become much more refined and smart.

image

image

2 Likes

I wanted to accomplish this using the glTF model.

image

BTW, I was a little concerned that the rotation did not seem to work properly unless the rotation was initialized first. Is this a necessary step?

mesh.rotation = new BABYLON.Vector3(0, 0, 0);

Very cool scene! :smiley:

I checked the shogi pieces model, and it has a rotationQuaternion set, so it needed to be nulled first before cloning, or the clones would get the rotation from the quaternion too. And when you set mesh.rotation, it does exactly that.

1 Like

I see. I don’t think it was in quaternion mode when the model was created, but it may have been converted to quaternion mode when exported in glTF format.

Now that we know the cause of the problem, we can move on to the next step. thank you very much.

1 Like

That’s correct, glTF only expresses rotations in quaternions so if you have euler rotations they will always be converted :slight_smile:

1 Like

Thank you for teaching me about quaternions. I think my understanding has deepened.

BTW, the layout was done in Excel. By using the angle information of the arranged autoshapes, it could be used as a map editor.

4 Likes

Very cool trick! :open_mouth: @PirateJC bet that would have saved you some time on the Playroom dominoes :rofl:

1 Like