Collision detection breaks on items when other meshes are disposed

I’m losing my mind over this bug I’ve encountered.

  1. When I haven’t killed/disposed an enemy mesh in my scene the collision detection/pick up works perfectly on the health pack and the energy pack I have placed in my scene. The code for enemy kill is this and it’s called when the enemy health reaches 0:


The collision detection between the player-camera and the objects in my scene is handled by this code :

This is the code that describes a collectible/healthpack/energypack :

And this is how they look in the scene description:


healthpack being a cube that acts like a collision mesh and the healthpackmesh that acts like the actual model.

  1. When I have killed an enemy in the scene I can pick the health pack but not the energy pack. It detects the collision the sense that the camera stops moving when I go towards it but it doesn’t actually remove it nor it prints the name of it via the console.log call in the above picture.

It’s very hard/impossible to actually provide a playground due to the way my scene is structured.

It’s been a long time since I encountered this problem and I have no idea where to look so ANY suggestions would be soooo appreciated.

Adding @Cedric to see if he can help

1 Like

Hi @HappyDev
To be sure that I understand correctly:

  • you have 1 health pack and 1 energy pack in your scene that you can pick up
  • after you killed an enemy, you can pick that exact same health pack but not the energy pack

is that correct?
What does happen when you have multiple health pack and energy pack? Can you only pick up health pack?
The energy and health pack only differ by the name? I mean, it’s the same Collectible class instanced for both types?
Can you show me the code that creates the Collectible impostor?

  • Yes currently I have 1 health pack and 1 energy pack that I can pick up
  • Yes, when I’ve killed an enemy I can’t pickup the energy pack

What does happen when you have multiple health pack and energy pack? Can you only pick up health pack?

Just tested putting another healthpack in the scene and it turns out when I kill an enemy now I can’t pick up ANYTHING.

The energy and health pack only differ by the name? I mean, it’s the same Collectible class instanced for both types?

Yes the same collectible class instanced

Can you show me the code that creates the Collectible impostor?

I removed that but , I created with blender a cube mesh that acted as the collision mesh and parent of the health pack / energy pack model. I removed it in my scene and it doesn’t make a difference I just enable the checkCollisions property since the models are low poly.

Thank you so much for doing this, I know the situation is tight cause of no playground use. I’m gonna setup my github right now and maybe the project can be checked as a whole. I suspect it has to do with the way I load the models or the way I use the registerBeforeRender methods.

This is the complete project since there is no way to properly create a playground.

I know my code is not up to par with many of the most advanced users here and I’m used to how things are done in c and I’m sooo inexperienced.

As per usual npm install and then npm start in the folder.

I guess the Collectible class creates a physics object? Can you show me that code?
Also, do you have a pool of objects that you recycle?

1 Like

Probably you are going to be sooo frustrated about my code but I thought it was worth a shot making it public.

I really want to improve too. So criticism is welcome.

I took a look at your code. I can find the impostor creation for the bullets, camera, ground…but not for the collectible. Did I miss something?

Ι use the cannon impostors only for these specific bullets.

Some warned me that there might be some conflicts with the normal collisions but apparently making the mass of the camera impostor 0 doesn’t affect anything. In addition disabling/commenting out any impostor/cannon reference in my code still doesn’t solve anything, so I don’t think that’s the problem.

Right now I just use the checkCollisions on the actual mesh.

You have to create an impostor for the collectible or you won’t get collisions. Like what you did for the camera/bullets.

Ι get what you are saying but I disabled every cannon/impostor reference in my code, since it was just an experiment with projectile based attacks which I can get by removing if it makes the normal collisions work again, and the problem still exists.

Can’t this be done with the regular collision check ? I mean it’s working alright BEFORE I destroy an enemy.

It seems the enemy mesh is disposed 2 times

Can you also try to clone the mesh for collectible instead of getting it from the scene? Only 1 collectible mesh then call clone in the constructor.

1 Like

It isn’t actually disposed 2 times it just didn’t use the class built in destroy() method, fixed it though to do so.

Like this I suppose !

Still doesn’t work properly :frowning:

I think the issue is in the destruction of the enemy. When it’s killed, don’t call the destructor of the enemy and see if it works (move the enemy far away for example).
Otherwise, it can be the spawning of the collectible. To test that in a second phase, bind a key to spawn a collectible randomly in the world and see if each one is working.

1 Like

For the first suggestion , doing something like this:

Really doesn’t change anything.

I need to work on that but really I’m losing hope :frowning:

You are so great almost spoonfeeding me the solution but I really can’t think of anything that will cause this problem.

It feels to me like there is something with the way the enemy is disposed but how else am I gonna do this. It’s sooo mind boggling.

To add to that even doing something like this

That means, removing the usage of the collectible class still breaks the collision when an enemy is disposed.

Is there any possibility there is any internal bug in Babylon itself ?

There might be a bug in Babylon but we have to understand what’s going on before doing anything.
So, you removed the dispose and also nothing is done in the addcollectible and you have the same issue?
Do you think you can copy and paste your code in a playground?
The idea is to remove the maximum of code to get the bad behavior so it’s easier to understand.

1 Like

Of course I understand that didn’t want to sound rude or anything.

Sadly yes.

That seems very very hard for me since everything is connected and whatnot through classes.

I will give it a go though since it’s not like I can continue working with the current code base.

It will be simple probably without all the classes just a continuous function though…

So I will need some time to do this so I will report back asap.

1 Like

Yes, just a bunch of code that highlights the issue is perfect!

1 Like

This is what I can come up with right now.

Upon clicking on the right you enter the game and your mouse is captured.

I haven’t put a cross hair right now cause I don’t know how to do it but a ray is coming from the camera so when you click a mesh it is destroyed.

Everything is working great apparently but there are so many things missing from this. Like the classes and everything.

All this makes me feel so bad cause it means that what I’m trying to do generally works but my code is so shitty that I can’t even find what’s causing it and I’m too bad at this to even know where to look and I’ve, with your help in the previous replies of course, exhausted my options.

The only option that remains is to rewrite everything and I don’t know if I can handle it.

Maybe it’s my bad usage of classes or events or whatever.

You were a total bro along the way though, helpful to the bone.

[EDIT] In general though, something is broken with the way the enemies/meshes are disposed. Cause I tried many things and dispose directly the meshes with no use of their class methods and still the same problem.

And it doesn’t have to do anything with the registerBeforeRender additions cause I’ve tried removing their usage completely and still get the same problem.




instead of


Seems to fix the problem but probably that isn’t right but has a similar effect of making the mesh “disappear” from the scene. Really weird the way dispose() causes the problem no matter how I call it, directly on the mesh from wherever or indirectly through its class method.

Also please warn me if there are any dangers of using the setEnabled(false) method as a bandaid.

Staying with that for now.

1 Like