Is there an easy/more efficient way to set a Havok physics body box shape = mesh's bounding box?

Dear Babylon Community,

Dear @PirateJC,

I’m hoping to find an easy way to use set a physics body BOX shape = the BabylonJS computed bounding box for my mesh. The reason I want to do that is that I have a ground Mesh from blender that it turns out has some subtle slope deviations to it so that if I try and set physics body shape of the ground mesh to type MESH, the physics body ground mesh doesn’t behave like a flat surface (it has subtle “bumps”) which deflect my player physics off course during movement. Thus, I figured it would be best to just create a simple BOX shape. I initially tried to let the physicsAggregate helper automatically create a BOX physics body around my ground mesh (MESHID = 1) by:

physicsAggregate[1] = new BABYLON.PhysicsAggregate(scene.getMeshByID(1), BABYLON.PhysicsShapeType.BOX, {mass:0}, scene) 

but that didn’t seem to work (physics engine didn’t seem to create a physics box around the mesh when viewed with scene physics inspector).

Thus, I figured I should try and create my own physics shape box and attach it with the physicsAggregate function to my ground mesh. However, to do so, I would need to know at least the center of my mesh and its size. I figured I could determine such measurements the hard(er) way:

centerX = scene.getMeshByID(1).getBoundingInfo().boundingBox.centerWorld._x
maxX = scene.getMeshByID(1).getBoundingInfo().boundingBox.maximumWorld._x
sizeBoxX = 2 * (maxX - centerX)
centerY = ..., etc

But was wondering if instead there was an easier way to just set a BABYLON.PhysicsShape.BOX = mesh’s bounding box?

I appreciate any suggestions you might have on the matter.

Thanks! :blush:

It looks like the code you posted should work.

physicsAggregate[1] = new BABYLON.PhysicsAggregate(scene.getMeshByID(1), BABYLON.PhysicsShapeType.BOX, {mass:0}, scene)

Can you use the Blender mesh in a playground example that gets close to what you want? …that way I can take a look and see what’s not working.

Yes, thanks, @docEdub, I’ll work on doing that this week. The mesh is part of a much larger set of meshes, but I will just try and load the ground mesh into a playground and simplify the project to test it. :+1:

You can have more control than aggregates by creating body and shape(s) independently.

Check like 43. Box shape is instanciated and then it’s bound to a body.
boxshape parameters allow setting center, orientation and extends manually.

And some doc here : Babylon.js docs


Thanks, @Cedric, I’ll try that, too. :+1:

1 Like

@docEdub & @Cedric, I made a playground demonstrating the problem I was having. It turns out when I made the playground (I usually am working in microStudio environment embedded with Babylon.js which is a slightly different setup), that I could tell that actually something that looked like a physics box (using the scene inspector with physics enabled) was created but it wasn’t positioned around my ground mesh (as you’ll see in the playground it is floating above my playground). If there is a way to get the physics Box shape to properly fit around my imported ground mesh, I’d like to do that. Otherwise, I’ll try manual solutions such as Cedric suggested or trying to figure out the meshes bounding box coordinates as I initially proposed in the question: Babylon.js Playground [Note: the playground actually imports the entire scene with 33 meshes that I am working on but I only render and create the physics Aggregate on the ground mesh] Thanks!

I got this working by setting the ground mesh’s parent to undefined before creating the physics box for it.


The PhysicsAggregate constructor doesn’t account for mesh parents, yet, but for now you can just unset the parent and set it back to the original parent after the PhysicsAggregate is constructed.

1 Like

Thanks, @docEdub, that works perfectly!

I’m afraid though I don’t understand why it works. Why does the ground mesh need a parent (undefined or defined) at all? Does some how the physics body or physics aggregate become a “parent” to the ground mesh? I’d appreciate your helping me understand that when you get the chance.

Have a good week :blush:

mesh.setParent(undefined); quietly applies all the mesh’s accumulated parent scales, rotations, and positions to the mesh, which makes it so the PhysicsAggregate can see them.

1 Like

Thanks-- good to know!

Was this then just an unusual use case when needed to do that or would you expect that there were be other likely times when using Havok that would need to apply a parent to a mesh in order for the Physics Aggregate to see them (so that I and others using Havok might anticipate the need to use in other situations in the future-- I don’t remember the Babylon.js documentation discussing this)?

I’m not sure. cc @Cedric. Maybe this is an option that can be added?

Thanks, @docEdub. I realized from looking more into this that the ground mesh actually does have a parent (node?). So, I’m thinking that your code perhaps removed that parent node and its translations that might have affected the ground mesh so that the PhysicsAggregate could appropriately know where the ground mesh is in world space to attach a physics body to and then you reset the ground mesh back to its parent?

Interestingly, for the other non-ground meshes that I don’t care as much if they have “rough” edges and I use:

BABYLON.PhysicsShapeType.MESH (Instead of BABYLON.PhysicsShapeType.BOX)

I don’t seem to need to remove the mesh’s parent to set the physics shape correctly in world space.

(These are my assumptions but may be misunderstanding things).


Cc: @Cedric

setting parent to null will bake the parents matrices with mesh once.
It looks like there is an issue doing that in body creation.
setting parent to null shall not be mandatory.
I’ll take a look this week.

1 Like

Thank you!

@Cedric @docEdub


@Cedric, did any changes regarding not needing to sent the parent to null make it into the new Babylon release?


Sorry for the delay. I’m just back from vacation. I’ll get back to this in the coming days.

Thanks! No rush-- just checking back.

Have a good week.

1 Like