Adding rigid body to meshes imported from .glb files at Babylon.js
Okay, I’m now happy that I can use Babylon.js at React Native macOS, but still more things are needed for completing a video game. one of those is in-game graphic objects, so I made a human boy mesh with VRoid Studio and imported to Babylon.js.
But…
When I added rigid body to the boy mesh, he acted like it has something under himself, and his head doesn’t collide with anything at all!
I tested it with box mesh(which made defaultly by Blender) instead of boy mesh, but that wasn’t acting weird like boy mesh.
Get Jihoo Byeon’s stories in your inbox
Join Medium for free to get updates from this writer.
So I searched Babylon.js forum and finally found someone suffered with same symptom with me!
It seemes to rigid body has made on lower position than his mesh, since root of mesh was under his feet in my case.
Solution
As Cedric Guillemet of Babylon.js team mentioned at that forum thread, I had to reset mesh’s pivot point to its center. after I did that…
OK! He now stands on floor correctly and ball is colliding with his head!
Sample Code
getPivotOfMesh(m: AbstractMesh) {
return (m.getBoundingInfo().boundingBox.maximum.y + m.getBoundingInfo().boundingBox.minimum.y) / 2;
}
const scene = new Scene(this.props.engine);
scene.enablePhysics(new Vector3(0, -1, 0), new OimoJSPlugin(undefined, 16, OIMO));
scene.createDefaultCamera(true);
scene.activeCamera!.position = new Vector3(0, 0.1, 8);
const light = new HemisphericLight(‘light’, new Vector3(0, 1, 0), scene);
const ground = MeshBuilder.CreateGround(‘ground’, { width: 100, height: 100 }, scene);
ground.position = new Vector3(0, 0, 0);
const boy = await SceneLoader.ImportMeshAsync(‘’, ‘file:///path/to/your/folder’, ‘boy.glb’, scene);
ground.setPivotPoint(new Vector3(0, 0, 0));
boy.meshes[0].setPivotPoint(new Vector3(0, -getPivotOfMesh(boy.meshes[8]), 0)); // meshes[0] is root mesh in VRoid; meshes[8] is body mesh.
boy.meshes[0].position = new Vector3(0, getPivotOfMesh(boy.meshes[8]), 0);
ground.physicsImpostor = new PhysicsImpostor(ground, PhysicsImpostor.NoImpostor, undefined, scene);
boy.meshes[0].getChildMeshes().forEach(m => m.physicsImpostor = new PhysicsImpostor(m, PhysicsImpostor.NoImpostor, { mass: 1 }, scene));
boy.meshes[0].physicsImpostor = new PhysicsImpostor(boy.meshes[0], PhysicsImpostor.MeshImpostor, { mass: 1 }, scene); // You must set PhysicsImposter(rigid body) of child meshes first then set PhysicsImposter of root mesh.





