PhysX3.2.4 Collision Callback does not work

I’m using PhysX.NET (C# wrapper for PhysX) and I am trying to get a notification of collision between two spheres using onContact in SimulationEventCallBack.

I have created a new subclass for SimulationEventCallback and overridden the OnContact method so that it will give me a message when collision happens. I have then set the simulationEventCallback of the scene to an instance of my subclass. This does not work even though the two spheres (rigid dynamic) obviously collide. Below is my code:

// Creating subclass

public class robotCollision : SimulationEventCallback
public override void OnContact(ContactPairHeader pairHeader, ContactPair pairs)
base.OnContact(pairHeader, pairs);
// Create scene

scene = engine.CreateScene(sceneDesc);

Is there something else that needs to be considered? Any flags to be set? I am sorry if this is a very naive question, but I have worked on this for the whole day for something that seems to be quite simple and I can’t wrap my head around it.

Thanks in advance

Hi there,

First thing to check.
In my case, I used the scene description to set the simulation event callback, but your way should work the same as long as you made sure the simulation was not already started while you set the callback (documentation says: “Do not set the callback while the simulation is running. Calls to this method while the simulation is running will be ignored”).

The second thing to check must be what is missing in your case.
The PxSimulationEventCallback::onContact documentation clearly states:
" The method will be called for a pair of actors if one of the colliding shape pairs requested contact notification. You request which events are reported using the filter shader/callback mechanism (see PxSimulationFilterShader, PxSimulationFilterCallback, PxPairFlag). "

So my guess is that you didn’t provided the physX engine with a filter shader callback mechanism . This mechanism should be given in the scene description:

physx::PxFilterFlags CollisionFilterShader(
	physx::PxFilterObjectAttributes /*attributes0*/, physx::PxFilterData /*filterData0*/,
	physx::PxFilterObjectAttributes /*attributes1*/, physx::PxFilterData /*filterData1*/,
	physx::PxPairFlags& retPairFlags, const void* /*constantBlock*/, PxU32 /*constantBlockSize*/)
	retPairFlags = PxPairFlag::eCONTACT_DEFAULT;
	return PxFilterFlag::eNOTIFY;
sceneDesc.filterShader = CollisionFilterShader;
sceneDesc.simulationEventCallback = myContactCallback;

And that now should work perfectly. Just let us know if this solves your case.

It works like a charm! Thank you so much! :). This made my day