Havok: Creating a new PhysicsAggregate of type MESH fails once a mesh has been disposed

Hi there,

I have troubles with Havok. If I create a PhysicsAggregate of type MESH, then dispose both the aggregate and the mesh later, the next time I try to create a PhysicsAggregate from a brand new mesh it fails.

The error is :

Error: 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).

… but if I inspect the mesh, everything is ok, even the number of vertices (even mesh.getVerticesData( VertexBuffer.PositionKind ) is correct).

I’m using the MeshBuilder (CreateBox, CreatePolyhedron, etc) for the mesh creation, not an imported file.

The creation code :

const dieMesh = MeshBuilder.CreateBox( 'D6' , { size: this.dieSize , faceUV , wrap: true } , this.scene ) ;
dieMesh.material = dieMaterial ;

const dieAggregate = new PhysicsAggregate(
    dieMesh ,
    PhysicsShapeType.MESH ,
    this.diePhysicsParams ,
    this.scene
) ;

The dispose code :

dieAggregate.dispose() ;
dieMesh.dispose() ;

Nothing fancy here.

I also tried to delay the new PhysicsAggregate() or the mesh.dispose(). It works with type PhysicsShapeType.BOX but since I also create polyhedrons (it’s for a physical dice roller project, so there are tetrahedron, octahedron, dodecahedron and icosahedron so far). Also the type PhysicsShapeType.CONVEX_HULL is not working even from the start.

I’m on the latest Babylon.js and Havok from npm : @babylonjs/core v9.11.0 and @babylonjs/havok v1.3.12. Am I using some unstable version ?

Also it’s inside a Tauri/Vue.js/Vite stack, not sure if that’s important…

I would consider shifting from PhysicsAggregate to PhysicsShape and PhysicsBody. See the end of this post on how to do that. This might help if the problem is with PhysicsAggregate itself. PhysicsAggregate was meant as a step stone from PhysicsV1 to V2.

Cannot reproduce as described: https://playground.babylonjs.com/#Z8HTUN#1340

But: https://playground.babylonjs.com/#Z8HTUN#1341 So double check (mesh.isDisposed()) that you feed in the correct mesh? My bet would be that the same error will occur without PhysicsAggregate.

I changed the playground a bit, simply adding a setTimeout(), so the dispose does not happen in the same tick, and also adding a plane so the cube could collide with it.

As you can see, the second cube produces an error in the console, and as a result, pass directly through the plane, as if there was no geometry at all.

Finally, the workaround for me was using directly new PhysicsShapeMesh() and pass it to new PhysicsAggregate() instead of the PhysicsShapeType.MESH constant.

The workaround worked for me, but I’m 100% positive that the original code should work and that there is a bug.

disposing the aggregate is fine but disposing the mesh causes the issue. Let me take a look.