Query for OnIntersectionEnterTrigger

Hi All,

I am trying to detect collision in scene. we are placing custom meshes one by one and when we do this, we have to check if placing any mesh collides with any on existing mesh(which are already placed)
I am using

scene.meshes.forEach((mesh) => {
 
             console.log(mesh.name)
 
             mesh.actionManager = new BABYLON.ActionManager();
 
             mesh.actionManager.registerAction(
 
                 new BABYLON.ExecuteCodeAction(
 
                     {
 
                         trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger,
 
                         parameter: cylinder1,
 
                     },
 
                     (event) => {
                         console.log("collision occured")
                     }
                 )
 
             );
 
         });

However using this i can only check collision with only one mesh at a time, i need to check if this collides with any other mesh in the scene

is this the right approach?

The action manager is meant as a trigger between two meshes. You can add many other triggers to the same action manager, but if you have a lot of meshes added and removed from the scene it might be a hard task to maintain those actions.

Any mesh has an “intersectsMesh” function that checks against a specific mesh. Persormance-wise i would not recommend to check every mesh against any other mesh (even if you optimize it), but if you want to check if one mesh intersects with all other meshes you could do something like this:

const mesh - getYourMeshHere();
scene.meshes.forEach(m => {
  if(mesh.intersectsMesh(m) {
    // do your thing here
  }
});
1 Like

Thank you @RaananW

1 Like