intersectsMesh not working for imported mesh

I have this study project: GitHub - Alecell/catrun

On this file I’m generating meshes automatically and checking if player intersects with the mesh right here, when I use babylon generated meshes with MeshBuilder it works as expected, but when I use imported glb meshes it don’t.

I’ve read a lot of questions about that kind of issue but no solutions works on my case, I’ve tried:

  • A simple cube created on blender (the default when open blender app)
  • Check the pivot of the mesh
  • moveWithCollision
  • Apply collision on the meshes:
    myMesh.meshes[0].checkCollisions = true;
    myMesh.meshes.forEach(mesh => {
      mesh.checkCollisions = true;
    })

Does anyone know whats happening?

And also is there something like a “box collider” just like unity does to simplify complex collision detection?

Hello! :smiley: Would you be able to provide a Playground ( Babylon.js Playground (babylonjs.com)) repro, to make it easier for us to look into your scene?

About the box collider question, intersectsMesh already uses the bounding boxes of the objects, just like a box collider: Babylon.js/abstractMesh.ts at master · BabylonJS/Babylon.js (github.com)

If you want physics, then you can use the BoxImpostor Using A Physics Engine | Babylon.js Documentation (babylonjs.com)

Hi @carolhmj, here is the playground https://playground.babylonjs.com/#2DIDW2#16

Btw I notice that the whole scene animation is a lot faster than on my local project even Im using the animationRatio multiplier, but here is much slower

While I create the playground I notice that if I replace the cat player with a babylon mesh it also works, but why Im facing issue to detect intersection between two imported meshes?

Hello! I saw your example used physics, so I tried making an even simpler version with the cat mesh: Test physics | Babylon.js Playground (babylonjs.com)
To make the physics work, I had to remove the root node from the imported GLB and add the impostor to that node without the root. @RaananW this is something we always have to do with imported meshes? I seem to remember doing this before for another imported GLB, but I think that’s expected behavior.

intersectsMesh seems to work fine with the cat too: Test physics | Babylon.js Playground (babylonjs.com)

2 Likes

Here I just changed your PG to pass the cube mesh to intersectsMesh (instead of passing its root mesh) and it seems to be working. I also removed the false parameter to dispose() so that the cube mesh is disposed too, not just its root mesh.

EDIT: Well if you end up removing the root meshes for physics then this answer wont be relevant anymore but I’ll leave it up anyway in case it helps. :slight_smile:

2 Likes

Sorry for the late response I became without internet for the last few days :sweat:

Thank you @carolhmj and @Blake, both solutions it works! But it leads me to a couple of another questions :sweat_smile:

@Blake why do we need the getChildMeshes? I thought that when I import the mesh I’m importing the root mesh, but it don’t seem to be the case, is there any docs about this?

@carolhmj what is the difference between the meshes[0] and the meshes[1]? Is there any docs about this?
Also I notice that on @carolhmj solution the player isn’t exactly placed on the ground, is like that its box collider is bigger than the mesh and even removing the normalizeToUnitCube(); its kind of floating in the air.
Another thing on your solution @carolhmj is that when I try to jump with spacebar it don’t work anymore, do you know what could be happening? I’m using impulse to do the jump with applyImpulse.
(btw for some reason the jump never works on playground, its only testable locally)

If you guys could answer my questions would be awesome! More than get my things solved I want to understand why the solutions works to improve my babylonjs skills :smile:

But again thank you very much!! :grin:

1 Like

Here’s the documentation about the root node that gets added when importing gltf models. Some things require you to pass the mesh with the geometry and won’t work properly if you pass its root node instead since it has no geometry of its own.

So you can use newMeshes[1] to get your actual cat mesh or you can also use newMeshes[0].getChildMeshes()[0] since the root node is its parent and its the first and only child. :slight_smile:

2 Likes