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

2 Likes

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.

3 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

Instead of check if a mesh (A) collided with other meshes (B, C, D), we can check if other meshes (B, C, D) collided with a mesh (A):

  • Instead 1:N
  • you can use: N:1

This way you don’t have to deal with registerOnPhysicsCollide.

In the code below, if a non permanent mesh (B, C, D) detect intersection with the mesh A, the mesh will disappear.

If anyone knows a better way to do this without using Physics, let me know :]

// detect collision with all meshes except permanent ones
const permanentMeshNames = ["player", "ground"];
const player = scene.getMeshByName("player")

scene.meshes
	// use only non permanent meshes
	.filter((mesh) => !permanentMeshNames.includes(mesh.name))
	.forEach((mesh) => {
		mesh.actionManager = new BABYLON.ActionManager();

		mesh.actionManager.registerAction(
			new BABYLON.ExecuteCodeAction(
				{
					trigger: BABYLON.ActionManager.OnIntersectionEnterTrigger,
					parameter: player,
				},
				(event) => {
					// discard this mesh when it collides with the player mesh
					mesh.dispose();
				}
			)
		);
	});

Example:

2 Likes