Best strategy for handling camera-mesh collision detection

I’m facing a challenge with the camera.onCollide event and could use some guidance. I’m trying to implement logic that triggers whenever the camera collides with any mesh in my scene. The catch is that I don’t know the identity of the “other” mesh involved in the collision.

While some meshes, like the ground, are static and don’t require collision event handling, the performance of my application suffers as the mesh count increases. I’ve already implemented a collider for the more complex meshes, but I’m still experiencing poor performance.

I was wondering if anyone could suggest a better strategy for efficiently detecting the onCollide event? I’m open to any ideas or techniques that could improve the performance of this process.

Thanks in advance for your help!

:exclamation: :person_raising_hand: IMPORTANT! PLEASE REPRODUCE YOUR ISSUE ON THE Playground FIRST! :person_raising_hand: :exclamation:

:thinking: Before asking a question

  • :mag_right: Search about your issue on the documentation. Maybe the problem you have is just a matter of understanding how the API works?
  • :mag_right: Search about your issue on the forum. Maybe someone else already had the same problem?

:question: How to ask a good question

  • :thought_balloon: Write a title that summarizes the specific problem: A good title will help others identify what are you talking about quickly.

    • Bad example: Shadow problem
    • Good example: Imported meshes on my scene don’t cast shadows
    • Bad example: Issue with GUI
    • Good example: GUI Button isn’t displayed on VR Session
  • :building_construction: Reproduce your issue on the Playground: This is the MOST IMPORTANT STEP! If we can see what you have tried on the Playground, and what the result of this is, we are much more likely to get what’s going on! And even if your problem is not happening on the Playground, but only on your environment, this already can give us clues what’s going on.

  • :person_in_lotus_position: Be patient: The team and forum users are all on different time zones, so your question might take a few hours to be seen. We try to answer everything as fast as we can, but remember we’re not robots :robot:

:package: Resources

Hello!

You might want to look into octrees: Optimizing With Octrees | Babylon.js Documentation (babylonjs.com), they can optimize the collision handling by only checking for objects that are closer to the camera :slight_smile: