Here’s a quick test I did:
(git repo here: GitHub - 0lento/ContactModSampleUnity: Simple test project for PhysX contact modification support in Unity. Requires Unity 2021.2.0a12 or newer.)
Basically there are few things happening here:
-
There are no forces/velocities added directly to these two balls in the scene. Instead the cubes at the bottom are acting as boost pads through contact modification (they just make the ball contact points think that the pads actually are moving at certain velocity).
-
Second ball has additional contact modification hack to check if the contact pair has positive separation. If it finds separation bigger than 0, it will ignore the contact point. This will prevent the ball from bouncing on collider seams in this example. The other ball doesn’t have this fix and you can see it getting airborne almost immediately. There are other (and better) ways to deal with this case, it merely acts as example here.
-
Lastly, that transparent blue cube on the left side acts here as a directional valve, it’s setup via contact modification to ignore any contact pairs whose contact normal is facing right world axis, basically this allows the balls to pass through initially but not into opposite direction.
Don’t take this as proper example on how to fix these presented cases, it’s just more of a thing to show how one can use the contact modification API. Worth noting that I’ve set the different collider indices into NativeHashMap for quick search for the contact modification effect we want to apply for that collider (using collider integer index as key). This part could be expanded to pass even more data to the contact modification stage, for example one could prefill a custom struct for the hash map instead of just the contact mod type.
As for the API itself, I like the current structure. I’d really love to be able to access the required GameObject data directly from contact modification callback but I do get it’s not trivial to make it threadsafe (and that threadsafe gameobjects are not a priority for Unity). Considering how niche this feature ultimately is, I’m fine by using the manually cached data for this.