How to make physical objects physically transparent to some other physical objects

I am trying to simulate the shotgun shot (8 pellets). I spawn all 8 pellets at one point (or very close to each other) and pellets start to interact with each other during shot. This makes the proper shooting and pellets spread mechanics impossible, because pellets affect each other too much.

While I am working on alternative approaches: like make pellets smaller or spawn them far enough from each other or spawning them with some time delay), I am wondering if it’s possible to make physicsImpostor not sensitive to collisions with other physicsImpostor. In other words, is there some collision exclusion list or something like that available under the physicsImpostor/cannon mechanics?

As a last measure I can write my own kinematics, particularly I can progress each pellet on every game tick constantly adding pellet velocity and gravity vector. Then I can do something like this to each pellet:

const EXCLUDED_COLLISION_OBJECTS = ["pellet0", "pellet1", "pellet2", "pellet3", "pellet4", "pellet5", "pellet6", "pellet7", "other"];
function meshIntersectsAnyOnScene (mesh, colliders) {
        for (let k = colliders.length - 1; k >= 0; k--) {
            if (colliders[k] != mesh
                && constants.EXCLUDED_COLLISION_OBJECTS.indexOf(colliders[k].name) === -1
                && colliders[k].intersectsMesh(mesh, false)) {
                return true;
            }
        }
        return false;
    }

But in this case I will lose all the nice mechanics that are already inside of physicsImpostor/cannon. For example, ricochets and ballistic shock when pellet hits any other physicsImpostor. So I would really like to keep the physicsImpostor/cannon while making a shot.

I will be even more specific: can I cancel physics computation if some physicsImpostor touched some other specific physicsImpostor without affecting all other collisions?

Reference playground: https://playground.babylonjs.com/#U7AJPW#2. Shot is made automatically after 3 seconds delay. The function handleHit prints each physicsImpostor collison to console. I would like to suppress physics computation on each ‘pellet => pellet’ collision.

Well, I think that spawning pellets with some offset in space works for me. When the ratio (spawn position spread) : (pellet size) is 3:1. It works OK. We still can get some pellet collisions occasionally, but they don’t affect the whole package much, because the number of collisions is pretty low. It can be even better with higher ratio. Actually, the shotgun pellet spread in modern shooters is much wider than my example.

PG: https://playground.babylonjs.com/#9XMM7R.
And we don’t actually need to use ACCURACY anymore (it’s set to 0 in my example). The pellets spawn at slightly different positions, so they already get slightly different velocity vectors (in terms of direction) according to how the velocity vector is calculated in my algorithm. So we can remove ACCURACY const at all or use it as initial spawn position spread in order to not create additional const.

But my initial question is still somewhat valid. I wonder if there is a possibility to omit the physics calculations on some particular collisions of physics impostors aka collision exclusion list in physicsImpostor/cannon?

cc @Cedric

1 Like

With ammojs, it’s possible to set collision group and mask.
mask is applied to other collided impostor to check it can collide with.
I did this quick PG to illustrate:

Collision between sphere and group is disabled. Set mask to 3 to enable collisions again.

2 Likes

I see. Would be cool to have something like that in the cannon as well. However, I am not sure that I fully understand how masks work.
Also, it looks like the PhysicsImpostor and PhysicsImpostorParameters API docs are pretty outdated. Some fields are missing (including the mask). And many fields are not explained. For example, setDeltaPosition that we used as workaround in one of my other topics. I don’t have even approximate idea what it does.