GLTF Parsed Meshes With Physics

Yo @Deltakosh or @bghgary or @sebavan.

I got a problem with GLTF meshes and Physics Imposter us AMMO.JS

It seems all meshes are under a main node call root. If i getNodeByName to get a mesh and add a physics imposter… Nothing happens (I think its because the mesh is a child of root node).

If i set the mesh.parent = null and then add the imposter and then reset the mesh.parent = savedparent it kinda works except if your mesh is out of camera view … like mesh.position.y = 25.

I am expecting that mesh to fall to ground, but the meshes and any child meshed are not visible.

Its diffcult to make a playground showing problem but i will try.

Before i spend too much time on this… Are there any issues with the Physics Imposter on a mesh that is the child of the GLTF root object… Are my meshes no showing up when out of camera view because of resetting the mesh to NO PARENT, MAKING THE IMPOSTER and then RESETTING THE PARENT backk to saved parent node ???

How is everybody doing physics with a GLTF scene ???

Adding @trevordev as this should be straightforward.

Yo @Deltakosh , @sebavan and @bghgary

I found out issue with MISSING MESHES when crated out of camera view are with all meshes. Has Nothing to do with physics. I will try make playground for that :slight_smile:

But is still need to know if DE-PARENTING the mesh before i call

mesh.PhysicsImposter = new BABYLON.PhysicsImposter

Then RESETTING the parent back to the save parent node is the way to go when setting up physics on a child mesh of the GTLF root ???

Instead of mesh.parent = null you should use mesh.setParent(null)
Explanation:
https://doc.babylonjs.com/how_to/parenting

Were you looking at Use a Physics Engine - Babylon.js Documentation ? You can see a working playground/gltf here: Babylon.js Playground if you open the inspector you can see how the gltf is structured.

Yo @trevordev and @Deltakosh

Can you please look at this playground: https://playground.babylonjs.com/#66PS52#15

It more closes follows the scenario using exported GLTF content from the new Babylon Toolkit.

    // NOTE: Compound Root Parent Child Order Is Authored In Scene File
    // The Box and Sphere Colliders Are Already Children Of Compound Root
    // 1... The Compound Root position in world is authored at 0, 5, 0 (Starting Drop Point)
    // 2... The Sphere Collider local child position is authored at 0, 0.08, 0 (Like Skull Demo)
    // 3... The Box Collider local child position is authored at 0, -0.13, -0.13 (Like Skull Demo)
    // 4... The Box Collider get converted to 0, -0.13, 0.13 (Left To Right Handed Conversion) 
    // 5... GLTF Flips Everything. All geometry in inside __root__ with negative z scaling.

But the center of mass seems off plus how can i control the center of mass for already authored meshes. If you run it a few times you see it fall and LAND on the box part only. But if center of mass is ZERO that should NOT happen. The sphere should make it tip over.

Why don’t we just have a center of mass field or something like that so we don’t have to use the POSITION of the compound root in the world as the center of mass BEFORE the child nodes are added. It seems like it HAS TO temporarily FAKE the compound root position. Then add children then set position back to authored or what ever your desired position is. i think that is what is going on. But i still get funny result when using position ZERO.

Can you please take a look at what i am trying to do in a playground using a Test Physics Scene authored with my new Babylon Toolkit

    // PLEASE HELP WITH ISSUES:
    // 1... I MUST set the entity mesh parent to null before calling entity.physicsImpostor = new BABYLON.PhysicsImpostor or it wont work at all
    // 2... I MUST use the AUTHORED entities. These entities have metadata and scripts attached. I cannot create new ones at runtime. 
    // 3... The CENTER OF MASS seems wrong. I do not know if that is because how the items are serialized in GLTF Loading or what
    // 4... Need to be able to change CENTER OF MASS. Be able to specify the center of mass at top of head like your other skull demo

Pretty Please :slight_smile:

The center of mass is the compoundRoot’s position relative to it’s child meshes so in your playground you can offset the children from the root node (See https://playground.babylonjs.com/#66PS52#16 ). Also I believe the -z scale is causing issues with ammo’s physics. In that playground I specified using rightHanded which removes the -z and now the physics bounces look more accurate. I would either do that or try to remove the -z scale from the objects manually before adding physics.

That looks SWEET now… I thing… How come i have to set parent = null BEFORE i create the physics imposter:

Take a look at this function, if i remark out the entity.parent = null stuff NOTHING WORKS. objects do not fall:

// NOTE: You must set parent of mesh entity to null or the imposter wont work
var createPhysicsImposter = (scene, entity, imposter, options) => {
    if (entity != null) {
        const parent = entity.parent;
        entity.parent = null;
        entity.physicsImpostor = new BABYLON.PhysicsImpostor(entity, imposter, options, scene);
        entity.parent = parent;
    }
}

Can you help with that :slight_smile:

Thanks @trevordev … All your info has been a great help :slight_smile:

My guess is that it’s due to everything having a parent when imported as a gltf. In the ammo examples I created I was importing a gltf as a single mesh but in your usecase the gltf is being used as a scene file. I believe the physics plugins have undefined behavior when everything is parented to the same node and it happens to work in your example because the root node has an identity matrix. I would try to get rid of the parent node if possible but if it’s working fine how you have it set up it might be good.

Well… The playground you fixed up: https://playground.babylonjs.com/#66PS52#16

Works beautifully :slight_smile:

So i will keep using my Custom CreatePhysicsImposter code that simple set parent to null BEFORE creating the physics imposter and setting back to it original parent AFTER creating physics imposter :slight_smile:

Yo @trevordev

By they way… that center of mass offset subtracted from the child colliers… Fucking Brilliant, Just brillant… Now i can use authored compond roots and child colliders… Fucking Brilliant :slight_smile:

1 Like

Yo @trevordev or @Deltakosh … I got another one for you.

In this playground i have:

  • GroundPlane with a box imposter.

  • Cylinder1 with a cylinder imposter

  • Cylinder2 with a mesh imposter

  • Cylinder3 with a convex hull imposter

Notice how the physics contact and falling is WAY off for the Mesh and Convex Hull imposters…

The Cylinder Imposter on Cylinder1 looks good but the Mesh and Convex Hull on cylinders 2 and 3 are funky.

Can you please work your magic again and take a look at … Please :slight_smile:

I think there was a bug in the addHullVerts and addMeshVerts methods where they weren’t properly setting rotation. Based on this thread btBvhTriangleMeshShape questions - Real-Time Physics Simulation Forum ammo’s btBvhTriangleMeshShape should only be used for static bodies.

I will create a PR (fix rotation for meshes and fallback to convexMesh when mesh impostor… by TrevorDev · Pull Request #5978 · BabylonJS/Babylon.js · GitHub) to fix the rotation, I beleive it will make cylinder impostor and convex hull work as you expect. For mesh impostor, maybe I should check if mass is not 0 and create a convex hull instead in that case to get the expected behavior for that case.

Yo @trevordev … can you left me know when you got PR in. Then i can update my Babylon.js fork :slight_smile:

Yo @trevordev … the changes you made to addHullVerts and addMeshVerts … Work great now for Mesh and ConvexHull Impostors… Now all the Cylinders Fall and contact and react as excepted… thank bro :slight_smile:

2 Likes

I’m so glad we put convex hull impostors support in there … Along with my Automatic mesh convexsion you get when using the toolkit and you have a Mesh Collider with the convex checkbox enables Unity will auto convex your mesh for your physics engine … And now so do I … All the convex hull stuff works BEST when the mesh geometry is actually convex. Beautiful :+1:

1 Like