Question about HavokPlugin.prototype.getShape

(edited to add playground link)

This is a question for someone who knows about the Havok plugin code

I was trying to dispose of a bunch of Havok physics bodies and shapes, and I stepped in to the babylon.js code to see what was happening behind the scenes. I have code like this:


mesh.physicsBody.shape does a property get call to Havok.prototype.getShape(), which ends up allocating and returning a new PhysicsShape(). So I think that when it immediately calls dispose(), it disposes the new one and not the one that’s been part of the body. I don’t know if that’s causing a leak or if I’m doing it wrong or what. Maybe I’m supposed to hold on to the originally allocated shape and dispose it that way?

I guess I’d like to know the right way to dispose of physics stuff when I’m trying to completely get rid of a physics-enabled mesh.

If you add debug.html to the playground link, it will use the non-minified versions.

@Cedric @RaananW @carolhmj maybe will know without a playground.

Thank you, I added a playground link to the initial message. I couldn’t figure out how to step through the code in browser dev tools but the playground link should exhibit the oddness I described above

Hello! Thank you very much for the PG! The call to physicsBody.shape doesn’t allocate a new Shape in the Havok simulation, only in the Javascript side. However, I think this object creation can lead to problems, because the pluginData information will only be nulled in the object where dispose was initially called, not in any previously created ones with the same pluginData information: Babylon.js/havokPlugin.ts at master · BabylonJS/Babylon.js ( If someone ends up using one of these objects to create another body, this leads to an error: shape disposal | Babylon.js Playground (

@Cedric instead of allocating a new object, we could have a map of shapeId → shape, like the one for bodies: Babylon.js/havokPlugin.ts at master · BabylonJS/Babylon.js (, and get the shape from there, what do you think?

There is also the reference counting. A shape can be used by multiple bodies. Disposing one of these bodies shall not destroy the shape. But yes, I shape shapes should be managed like bodies. A list in the engine with a refcount. only refcount <= 0 will dispose the shape in the plugin. Is there some mecanism available with TS/JS? is if possible to let the JS engine do the final disposing and have an event/observer associated?

Small note - We now offer full sourcemaps to the playground, so there is no need to use the debug.html endpoint. it is still there though :slight_smile:

I think we don’t need the refcount because we don’t automatically dispose of the shape when we dispose the body. We should only need to guarantee that, once shape.dispose is called, every JS object that “points to” that shape id has the id wiped out.

It’s a disguised refcount. when a shape is disposed, you can check if some bodies are still using it and display a warning/error. On the other side, when all bodies using a shape are disposed, you can also dispose the shape automatically (with a flag that enable the behavior?).