Collision detection of large number of meshes

Hello all,

I’m trying to better understand what is the proper way to check collision between a large number of meshes. I have seen other questions but I can’t seem to understand the right solution.

Imagine a simple game, where you shoot at tens of possible meshes, which are also created dynamically.

From my basic research, all the collision mechanisms require a mesh starting point and a given target. I have checked the following:

  1. Physics engine collision - https://playground.babylonjs.com/#KEPY4J#8
  2. Action manager or render loop specific collision checks - Collisions & Triggers | Babylon.js Documentation

One more requirement that I have is to be able to enable collision on entities that move using animation, and not move with velocity vector.

This means that on every creation of a new mesh(a bullet) I will need to either create an action manager target between each given entity.

But - does it make sense? does it make sense to create so many observers for every mesh upon creation? assume that the game should work super fast. Checking collision on the render loop or creating tons of actions managers doesn’t seem like the right solution.

Do I miss anything?

Thanks for any help,
D.

1 Like

Adding @Cedric our physics GURU

I think instead of collision detection in the way you describe, you’ll instead want to use a cast ray to detect collisions between whatever projectiles are active and any mesh that intersects it’s path.

Look for ray casting in the docs for more details on it or ask any follow up Q’s and we’ll see what can be done :slightly_smiling_face::sunglasses:

Ed: the registerOnPhysicsCollide Doesn’t require a target mesh to check against, just the physicsImpostor of the object itself. So you’d create all these physics impostors but only register whatever you’re using for the projectile, missile, beam, whatever to fire observable on collisions. The event data contains info on the collision so you know which mesh was “hit”

HTH

1 Like

For best performance, do not create objects on the fly. Last time I did bullet/object collision detection, I used thin instances for bullets and sphere collision detection.
The thin instance array is preallocated and update each frame. Simple sphere collision detection basically is 2 for loops.
If your collision detection is simple (like my case), it’s quite efficient. If it’s more complex, then do a broadphase before and narrow down to more time consuming later in the algorithm.
Early rejection if key.

2 Likes

Hi guys and thank you both for the answers.

I guess that I missed that the registerOnPhysicsCollide does not need a target. I will dive into that.

Thanks again, this is a great community!
D.

3 Likes