Dear friends , I have a question about synchronizing the scaling of PhysicsShapes with a Mesh in the Havok physics engine. I’m animating the growth of a mesh, but while the mesh scales up, its physical shape remains unchanged. How can I synchronize the scaling of the physical shape with the mesh?
Thank you, HiGreg. That sounds like a reasonable suggestion. I’ll try updating it not on every frame of the render loop, but at some interval, and I’ll let you know how much it impacts performance. I’ll try using a multiplier and update the shape from the start to the end of the animation every 60th frame, for example. So, the shape will catch up with the mesh size once per second. Thanks, I’ll give it a try.
Actually, it works quite well. Here’s an example with the apple mesh, where I get and replace the shape every second. The apples grow properly. I simply call the function with the mesh as an argument and recreate the shape based on the mesh’s scaling. Of course, in the future, I’d like to have the ability to attach the shape to the mesh, allowing the shape to automatically follow the mesh’s scaling.
getShape(mesh: AbstractMesh, initialRadius: number = 0.12) {
const scaling = mesh.scaling.y
const calculatedRadius = scaling * initialRadius
const center = new Vector3(0, calculatedRadius, 0)
const shape = new PhysicsShapeSphere(center, calculatedRadius, mesh.getScene());
return shape;
}
There is a potential performance optimization if scaling a complicated or hard-to-regenerate shape. I note that “addChild()” on a SHAPE_CONTAINER includes a scaling vector. With this in mind, adding an existing shape to a container shape and using the scale parameter might perform better than regenerating the shape from the original mesh.
I don’t know if a container shape can be added to another container shape. If not, then my (possible) optimization is likely only useful for hull or mesh shapes. It might be prudent to save the original shape and scale always from there rather than a continuous up or down scaling which might introduce errors from floating point precision.
As a framework user, it would be convenient for me if there were a parameter like mesh.physicsBody.syncShapeWithMesh = true for physicsBody (or something like that). However, if I need to update the shape programmatically, it would probably be enough to have the ability to change the parameters of the existing shape without having to recreate it through dispose() and regenerate it again. Right now, when I use shape recreation, it’s hard to debug because I have to click the toggle back and forth in the Inspector to update the shape sizes.
The Inspector doesn’t refresh them on its own. That’s why an shape update would be more convenient than deleting and creating the shape again.