Physics imposter unique ids

Hi, I am having an issue with physics imposter unique ids and I am hoping someone can clarify if I am using them incorrectly.

If I create a physics imposter, it seems to be assigned a unique Id within the physicsEngine.addImpostor method based on the length of the physicsEngine._impostors array after the physics imposter is added to the end of it (physicsEngine.ts line 121).

When an imposter is removed, this array is spliced within the physicsEngine.removeImpostor method (physicsEngine.ts line 136).

This seems to mean that if I create 3 imposters, they will be assigned the uniqueIds: 1, 2, 3.
If I then remove imposter 2, the physicsEngine._impostors array will contain two imposters with uniqueIds: 1, 3.

If I then add a new imposter, wont the new one also be assigned a uniqueId of 3, meaning there now exists 3 imposters with the uniqueIds: 1, 3, 3.

I could be wrong about this, but this does seem to be occurring within my project and causes issues due to each uniqueId not being unique.
Thanks.

Well, if that’s the case it needs to be solved!

We have a uniqueId mechanism in the scene, we could just get the next available uniqueId and use it there as well. Could you reproduce that in the playground for us to be fully sure this is a good solution?

Here is a playground example (you need to look at the console log) https://playground.babylonjs.com/#2B84TV#35
Hope this shows the issue

@Raggar
Could this be related to the perf issues with non sleeping physics objects?

Without looking at the source, I don’t believe so. AmmoJS runs its own simulation, and as far as I’m aware, doesn’t know about the unique id. It’s only used by BabylonJS to distinguish between different impostors. I still believe the issue with sleeping comes down to a bug or missing feature in the transpiled library. I don’t have my own version anymore, but creating a local project and replacing the currently used AmmoJS version with one of the many public versions you can find should confirm this.

I don’t believe the unique ID influenced the actual engines (as far as I see it is not even being used in AmmoJS’ plugin), but it might have created issues with impostor naming when using Oimo and duplication of materials in cannon JS (though it really depends on the engine, whether or not they require unique names when creating those objects)

Here is a fix - Fix - Unique ID for physics impostor by RaananW · Pull Request #11759 · BabylonJS/Babylon.js (github.com)

2 Likes

Thanks for the fix. To confirm, I was using the uniqueIds within my own code and that is why I found the issue. I’m not sure it is important for the engine itself.

1 Like