Creating a Box Physics Body for an External Mesh (Transform Node)

Hello Community,

I’m currently working on a project where I’m loading an external mesh. My goal is to create a physics body for this mesh. I’m wondering if there’s a straightforward method to encapsulate the entire hierarchy of the transform node within a physics shape box body.

Additionally, I would like to know how to scale this box-shaped physics body.

To give you an idea of what I’m looking for, here’s an example similar to my requirement, but instead of using a mesh, I want to use a box:

const shape = new PhysicsShape(
  {
    type: PhysicsShapeType.MESH,
    parameters: { mesh: instance, includeChildMeshes: true },
  },
  this._scene
);

Any insights or suggestions on how to achieve this with a box shape would be greatly appreciated!

Thank you!

Hello!

Here’s an example of how this could be done with one of our example meshes: Available Mesh Buggy | Babylon.js Playground (babylonjs.com)

Things to note:

  1. When we import a GLTF mesh, a root node is created to encapsulate the handedness conversion. To avoid touching this conversion with physics, I create a new node to parent the root in. That’s not necessary if you don’t mind losing the conversion or if you aren’t using a GLTF.
  2. getHierarchyBoundingVectors gets the min and max of the mesh in world space, so take care to consider any transformations that might be applied to the body. In this example’s case, I only applied the 1000 y translation after getting the bounding vectors, otherwise I would have to subtract from my result.

Hope this is helpful and let me know if you have any more questions :slight_smile:

1 Like