I am having issues adding physics to my mesh

I just started using babylon js and I was trying to add physics to my 3d glb model but I get these errors
Tried to create a MeshImpostor for an object without vertices. This will fail.
It was not possible to create a physics body for this object.
I created the model in blender and exported it as a glb file
this is my code

async function add3d(path, addPhysics = false, mass = 0, friction = 0.1, restitution = 0.1) {
    return new Promise((resolve) => {
        var name = path.split("/").pop();
        var directory = path.replace(name, "");
        BABYLON.SceneLoader.ImportMesh("", directory, name, scene, (meshes) => {
            if (addPhysics) {
                meshes.forEach((mesh) => {
                    // Apply physics impostor to child meshes first
                    mesh.getChildMeshes().forEach((child) => {
                            child.setParent(null);
                        child.physicsImpostor = new BABYLON.PhysicsImpostor(child, BABYLON.PhysicsImpostor.MeshImpostor, {
                            mass: mass,
                            restitution: restitution,
                            friction: friction,
                        }, scene);
                    });
                    mesh.physicsImpostor = new BABYLON.PhysicsImpostor(mesh, BABYLON.PhysicsImpostor.MeshImpostor, {
                        mass: mass,
                        restitution: restitution,
                        friction: friction,
                    }, scene);
                });
            }
            resolve(meshes[0]);
        }, undefined, (error) => {
            console.error(`Error loading ${path}: ${error.message}`);
        });
    });
}

Hello, welcome!

Can you please create a PG and post it instead of your code?

Tip:

if you are inserting code here, you can format it like this:

  const isLookingNicer = true

1 Like

sure!

nvm its working on the playground but not my actual project, even though it still gives a warning

is it ok if I post the link to my replit project here?

Do you have a git hub repo? You can post the link, sure.

There may be a (gltf-) root node in the meshes array. Try to skip meshes[0].

I’m making a racer game here’s the link:

replit.com/ @MatteoMarotta1/racer

Instead of assuming only the root has no vertices, I use this:

// "importedObjects" is returned by SceneLoader.ImportMesh directly or 
// ImportMeshAsync through a Promise (in which case, the following is used as the last function in the ".then" Promise chain since it doesn't return anything)

importedObjects.meshes.filter((mfilter)=>
mfilter.getVerticesDataKinds().length >0).forEach((m)=>{
   // code to process each mesh "m"
}

This is useful when doing a MergeMeshes() as well (for my purpose here, I don’t care about materials, constraints, or separate meshes):

const OneMesh = BABYLON.Mesh.MergeMeshes(m.meshes.filter((mfilter)=>mfilter.getVerticesDataKinds().length >0))

I believe a better approach is to have a clean model file w/o any inconsistencies.

I would also opt for explicitly adding the impostors to a predefined set of meshes:

    const addImpostor = (mesh) => {
        //
    }

    const meshesWithImpostor = [
        "sphere",
        "sphere1"
    ]

    meshesWithImpostor.forEach(meshName => {
        const mesh = scene.getMeshByName(meshName)
        mesh && (addImpostor(mesh))
    })

Changed to BoxImpostor.

(racer - Replit)

I suggest you to switch away from Replit and develop with VS Code.

https://doc.babylonjs.com/setup/frameworkPackages/es6Support

That works when you control the model (as in OP). My use case is importing mesh files I don’t control, then cleaning them up as best I can.

Background detail:

Just playing around creating rollable dice from obj and stl files other people created. The four samples here are “pass the pigs” and “knuckle” from Thingiverse, “mini” and “teddy” I thought were from BABYLON examples (but I can’t seem to find them now). The pink d20 “Glimmergrove” has inset numbers that are difficult to discern with the current material.

Pass The Pigs by tmone is licensed under the Creative Commons - Attribution - Share Alike license.
Knucklebone Dice by MishaT is licensed under the Creative Commons - Attribution - Non-Commercial license.
Tales of The Glimmergrove d20 by Kazrak is licensed under the Creative Commons - Attribution - Share Alike license.

I got it working when I set the car imposter to a SphereImposter but I need it as a BoxImposter

I switched to ammo js and it works now thank you everyone

Have you tried usung Havok (Physics V2)?

https://doc.babylonjs.com/features/featuresDeepDive/physics/havokPlugin

“We strongly recommend using V2 over V1. A migration guide is here.”
https://doc.babylonjs.com/features/featuresDeepDive/physics/migrate

1 Like