Loading gltf and physics not working, what am I missing?

Here is my 2 cents…

I have been trying to tell folks for quite some time about GLTF meshes and Physics and PARENT containers. They are screwy… and in GLTF … everything has a parent … up to the root node.

Anyways… You need a function like this:

var createPhysicsImpostor = function(scene, entity, impostor, options, reparent) {
    if (entity == null) return;
    entity.checkCollisions = false;
    const parent = entity.parent;
    if (reparent === true) entity.parent = null;
    entity.physicsImpostor = new BABYLON.PhysicsImpostor(entity, impostor, options, scene);
    if (reparent === true) entity.parent = parent;
};

And dont do mesh.physicsImpsoter = new BABYLON.PhysicsImposter

call you function instead:

newMeshes.map(mesh => {
            if (mesh.name.includes("BoxCollider")) {
                console.log("Dodaj Box Collider/Impostor --> ",mesh.id);
                //mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, scene);
                createPhysicsImpostor(scene, mesh, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, restitution: 0.9 }, true);
                _boxColliders.push(mesh);
            }
        });

        newMeshes.map(mesh => {
            if (mesh.name == "Ball") { //  || mesh.name === "Sphere"
                console.log("Dodaj Ball Collider/Impostor --> ", mesh.name);
                //mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 0.5, friction: 0.5, restitution: 0 }, scene);
                createPhysicsImpostor(scene, mesh, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 0.5, friction: 0.5, restitution: 0 }, true);
                _playBall = mesh; // damo mesh v kolekcijo boxColliders
                _playBall.position.y = +10;
            }
        });

Try it out on this AMMOJS playground: https://playground.babylonjs.com/#97FMLC#2

And this CANNONJS playground as well: https://playground.babylonjs.com/#97FMLC#3

3 Likes