How to check collision using onCollideObservable()

Hi there,
from the Docs i’ve read that collisions are menaged using intersections. But if i understand right we must specify the mesh to be checked for intersection.
Using the onCollideObservable() seems not be necessary to know the target mesh:

An event triggered when this mesh collides with another one

I’ve tried enabling collisions and check for collisions using that Observable but nothing happend.
This is my PG:

Where do i wrong? Am i supposed to add also some triggers?


Well, you are almost there :slight_smile:

Test collision | Babylon.js Playground

You need to use moveWithCollisions if you want collisions to be checked on mesh-to-mesh

Thank you for your quick reply.
In my application i need to check for collisions during dragging with mouse. The purpose is: if i drag a mesh over another one do something.
Is it possible?

Yeah, it is possible :slight_smile:
You will need to use the intersection actions (and not the collision system directly). It is documented here - Actions | Babylon.js Documentation and here - Collisions & Triggers | Babylon.js Documentation

Yes, as i said i’ve read that doc but it seems i’ve to specify the target (tell me if i’m wrong). In my project i don’t know which could be the target. I’ve different meshes,that can interact with each other and when a mesh hit another i want to do stuff. But i don’t know before what could be the mesh to check for collision until the user pick a mesh, drag it and then collide with another.

Sorry, should have read the initial message again. Yes, intersections do require a target, because of how expensive they are. If you want to achieve something like that you will have to write your own collision detection mechanism (you can use collidesMesh for that). Just know that this might slow down the experience if you check for too many meshes

1 Like

Hi @RaananW,
maybe i’ve found a solution that could be good for my application.
I thought that i didn’t necessary have to use collisions, i’ve only to know on which mesh i’m dragging to. So when i pick the mesh to move i temporary set isPickable = false and on onPointerUp event i check if there’s a mesh that overlap and then do stuff and reset isPickable = true.
Maybe is not so nice, but it seems to work.

1 Like

Not everything has to be nice. It does have to be functional and scalable! And if it is, it is a viable solution.
Love it, it seems to do the trick! And if this is the functionality you were after, this seems like a very efficient way of achieving this.

1 Like