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:
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.
@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!
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.
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.
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.
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)?
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).
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.