Physics with height map?

Trying to add a physics aggregate to a height map, but failing with No valid mesh was provided for mesh or convex hull shape parameter. Please provide a mesh with valid geometry (number of vertices greater than 0).

If I comment out the aggregate creation, the heightmap renders.

I found some working examples that use the deprecated BABYLON.Mesh.CreateGroundFromHeightMap. That one uses a callback, but the MeshBuilder version seems to by sync?

I believe something like this should work:

const ground = BABYLON.MeshBuilder.CreateGroundFromHeightMap("gdhm", "textures/heightMap.png", {
    width: 5,
    height: 5,
    subdivisions: 10,
    maxHeight: 1,

    onReady: (mesh, heightBuffer) => {
        const aggregate = new BABYLON.PhysicsAggregate(mesh, BABYLON.PhysicsShapeType.MESH);
        console.log('aggregate:', aggregate);
    },

    // passHeightBufferInCallback: true // if you need to use `heightBuffer` in the callback above
});
1 Like

PG from the documentation : https://playground.babylonjs.com/#I37D8G#18

2 Likes

I missed that one. Google keeps bringing up the Mesh PGs.

Strange API… It returns the mesh immediately, but it’s not ready. IMHO, it should return a promise instead of the onReady callback.