Exception multiManifold - Custom simulation on UnrealEngine using Immediate APIs

I’m developing a game and I need to customize the execution of physics simulation, to doing this I implemented a class that each tick executes the simulation using immediate mode APIs.

I’m developing under UnrealEngine where is created a scene that drive all game simulation. Each tick is execute the the function PxScene->simulate and I’ve replaced this with a function created by me that should simulate the physics using immediate mode.

As starting point to create the custom simulate function, I took the snippet written by you, and after some customization to be able to run it under Unreal I get the code ready to be compiled.

The problem is that when I start the game I get this error:

Exception thrown: read access violation.
multiManifold was nullptr.

The file where this error is thrown is: PhysX_3.4\Source\GeomUtils\src\pcm\GuPCMContactConvexMesh.cpp at line 124.

I’ve debugger the code but I cannot understand what is causing this exception, here is a picture of debugger
https://www.dropbox.com/s/ar6wwerzrwmnk6d/Untitled.png?dl=0

Into my function the exception starts from here:

......
......
if (!GenerateContacts(holder0, holder1, *dyn0, *actor1, *cacheAllocator, contactPoints, activeContactPairs, pair.idx0, pair.idx1, persistentData.cache))
		{
			//Contact generation run but no touches found so clear cached friction data
			persistentData.frictions = NULL;
.......
.......

and the function GenerateContacts is:

bool FPhysXRummy::GenerateContacts(PxGeometryHolder& geom0, PxGeometryHolder& geom1, PxRigidDynamic& actor0, PxRigidActor& actor1, PxCacheAllocator& cacheAllocator, TArray<Gu::ContactPoint>& contactPoints, TArray<ContactPair>& contactPairs, PxU32 idx0, PxU32 idx1, PxCache& cache)
{
	Gu::ContactBuffer buffer;

	PxTransform tr0 = actor0.getGlobalPose();
	PxTransform tr1 = actor1.getGlobalPose();

	TestContactRecorder recorder(contactPairs, contactPoints, actor0, actor1, idx0, idx1);

	const PxGeometry* pxGeom0 = &geom0.any();
	const PxGeometry* pxGeom1 = &geom1.any();

	physx::immediate::PxGenerateContacts(&pxGeom0, &pxGeom1, &tr0, &tr1, &cache, 1, recorder, gUnitScale*0.04f, gUnitScale*0.01f, gUnitScale, cacheAllocator);

	return recorder.hasContacts();
}

where physx::immediate::PxGenerateContacts is called.

If you check the debug the on the picture on tab Watch (Point 4)
you can see the problem could be caused by two parameters nNumManifolds and mNumTotalContact.

So who creates the parameter multiManifold? For sure the problem is on the code I’ve written, but I need to understand where this variable is created.

Please help me to get out from this problem, and If you need more information / code please let me know.

Thanks in advance.

There was an issue with meshes that was fixed in January. Depending on which version of UE4 you have, you may have the bug still. The fix will definitely be contained in UE4.16, which is available in pre-release form on GitHub now.

In NpImmediateMode.cpp, ~line 681:

if it looks like this::

if (cache.isMultiManifold())
{
multiManifold.fromBuffer(reinterpret_cast<PxU8*>(&cache.getMultipleManifold()));
cache.setManifold(&multiManifold);
}

it should instead look like this::

if (cache.isMultiManifold())
{
multiManifold.fromBuffer(reinterpret_cast<PxU8*>(&cache.getMultipleManifold()));
}
else
{
multiManifold.initialize();
}
cache.setMultiManifold(&multiManifold);

Assuming your code matches the latter, then there should be no circumstances in which your manifold can be null. You are responsible for allocating memory that the multi-manifold references from your PxCacheAllocator.

Hope this helps

Kier

I don’t know how to say you Thanks for this answer!!!

Yes, my code is look like the old version. So I’m downloading 4.16 to check if the problem is solved.

Thanks you alot!! I’ll update you soon. Thanks!

Perfect this problem is resolved!! Thanks you!