# How do I get my ContactModifyCallback to work?

**URL:** <https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [September 15, 2021, 3:05pm UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909 "2021-09-15T15:05:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tancen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tancen](https://forums.unrealengine.com/u/tancen)\
**Post date:** [September 15, 2021, 3:05pm UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/1 "2021-09-15T15:05:50Z")

</div>

I have a class MyContactModifyCallback, which inherits PxContactModifyCallback, I don’t know how to make it work in the ue4 project.

```auto
class MyContactModifyCallback : public PxContactModifyCallback
{
public:
	virtual void onContactModify(PxContactModifyPair* const pairs, PxU32 count)
	{
		UE_LOG(LogTemp, Error, TEXT("bbbbbbbbbbb"))
		for (PxU32 i = 0; i < count; i++)
		{
			auto p = pairs[i];
			for (PxU32 j = 0; j < p.contacts.size(); j++)
				p.contacts.ignore(j);
		}
	}
};

```

I tried to set it like this in GameMode.InitGame, but it didn’t work.

```auto
void AtestGameMode::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
	AGameModeBase::InitGame(MapName, Options, ErrorMessage);

	auto s = this->GetWorld()->GetPhysicsScene(); 
	printf("%p", s);
	s->GetPxScene()->setContactModifyCallback(new MyContactModifyCallback());
}

```

---

<div class="post-metadata">

**Author:** ![tancen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tancen](https://forums.unrealengine.com/u/tancen)\
**Post date:** [September 16, 2021, 8:22am UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/2 "2021-09-16T08:22:16Z")

</div>

Anybody help?

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [September 16, 2021, 9:57am UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/3 "2021-09-16T09:57:06Z")

</div>

Im not too familiar with PhysX, but docs say:

> You can enable the use of this contact modification callback by raising the flag [PxPairFlag::eMODIFY\_CONTACTS] in the filter shader/callback (see  
> PxSimulationFilterShader] for a pair of rigid body objects.

[PhysX doc](https://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/apireference/files/classPxContactModifyCallback.html)

---

<div class="post-metadata">

**Author:** ![tancen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tancen](https://forums.unrealengine.com/u/tancen)\
**Post date:** [September 16, 2021, 1:21pm UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/4 "2021-09-16T13:21:49Z")

</div>

Thanks, But I still don’t know how do I get it to work in UE4

---

<div class="post-metadata">

**Author:** ![tancen](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tancen](https://forums.unrealengine.com/u/tancen)\
**Post date:** [September 27, 2021, 1:40pm UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/5 "2021-09-27T13:40:59Z")

</div>

Finally, I find a way to make it worked.

```auto
// Fill out your copyright notice in the Description page of Project Settings.

#pragma optimize("", off)

#include "MyWorldSettings.h"
#include <PhysXPublicCore.h>
#include "Physics/PhysicsFiltering.h"

PxFilterFlags PxSimulationFilterShaderProc(PxFilterObjectAttributes attributes0, PxFilterData filterData0,
	PxFilterObjectAttributes attributes1, PxFilterData filterData1,
	PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
{
	pairFlags = physx::PxPairFlag::eCONTACT_DEFAULT;
	pairFlags |= physx::PxPairFlag::eDETECT_CCD_CONTACT;
	pairFlags |= physx::PxPairFlag::eNOTIFY_CONTACT_POINTS;
	pairFlags |= physx::PxPairFlag::eMODIFY_CONTACTS; //necessary

    PxFilterFlags ret = PxFilterFlag::eDEFAULT;
    return ret;
}

class MyContactModifyCallback : public FContactModifyCallback
{
public:
	void onContactModify(PxContactModifyPair* const pairs, PxU32 count)
	{
		for (PxU32 i = 0; i < count; i++)
		{
			PxContactModifyPair& pair = pairs[i];
			for (PxU32 j = 0; j < pair.contacts.size(); j++)
			{
				// code your filter codes
				//pair.contacts.ignore(j);
			}
		}
	}
};

class MyContactModifyCallbackFactory : public IContactModifyCallbackFactory
{
public:
	FContactModifyCallback* Create(FPhysScene_PhysX* PhysScene)
	{
		return new MyContactModifyCallback();
	}

	void Destroy(FContactModifyCallback* Callback)
	{
		delete Callback;
	}
};

AMyWorldSettings::AMyWorldSettings(const FObjectInitializer& ObjectInitializer)
	: AWorldSettings(ObjectInitializer)
{
	GSimulationFilterShader = PxSimulationFilterShaderProc;
	FPhysScene_PhysX::ContactModifyCallbackFactory = TSharedPtr<IContactModifyCallbackFactory>(new MyContactModifyCallbackFactory());
	
}

```

This example is taking effect only if the following conditions are reached.

1. ’ Edit → Project Settings → General Settings → Default Classes → World Settings Class ’ be set of ‘AMyWorldSettings’ .
2. ‘Simulate Physics’ be set of true.
3. Physics Collision enabled.
4. Add module ‘PhysicsCore’ in xxx.Build.cs.

---

<div class="post-metadata">

**Author:** ![HappyHappyPlay](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HappyHappyPlay](https://forums.unrealengine.com/u/HappyHappyPlay)\
**Post date:** [April 5, 2022, 12:44pm UTC](https://forums.unrealengine.com/t/how-do-i-get-my-contactmodifycallback-to-work/250909/6 "2022-04-05T12:44:59Z")

</div>

Thank you, this is great stuff!

I wanted to add that the function outlined here, `PxSimulationFilterShaderProc()`, which sets up the pair flags etc. should probably be an exact copy of `PhysXSimFilterShader()` in the engine (not just setting the 4 pair flags as outlined here) so collisions behave “normally”. Then, the “modify contacts” flag needs to be added of course.
