Height Field shape is nullptr

Hello everyone!

I’m currently working on a 3D game. I’m using PhysX 3.3, built on Visual Studio 2015. I’m coding on VS2015 for the Windows 32 bit platform. I’m trying to generate terrain from height field data. However, no matter how much I’ve simplified my code, I can’t seem to get the PhysX height field to work. I’ve looked EVERYWHERE for a solution, and I’ve replicated several different methods of creating the height field, but nothing seems to work (I get the same error every time).

This is the code that creates the height field (10 x 10 for simplicity).

const physx::PxU32 HF_SIZE = 100;

physx::PxHeightFieldSample* hfSamples = new physx::PxHeightFieldSample[HF_SIZE];
for (physx::PxU32 i = 0; i < HF_SIZE; i++)
	hfSamples[i].height = 10; // Arbitrary
	hfSamples[i].materialIndex0 = 2; // ?
	hfSamples[i].materialIndex1 = 2; // ?

physx::PxHeightFieldDesc hfDesc;
hfDesc.format = physx::PxHeightFieldFormat::eS16_TM;
hfDesc.nbColumns = 10;
hfDesc.nbRows = 10;
hfDesc.thickness = -10;
hfDesc.convexEdgeThreshold = 3;
hfDesc.samples.data = hfSamples;
hfDesc.samples.stride = sizeof(physx::PxHeightFieldSample);

physx::PxHeightField* heightField = Global::pxCooking->createHeightField(
	hfDesc, Global::pxPhysics->getPhysicsInsertionCallback());
physx::PxHeightFieldGeometry hfGeom(heightField, physx::PxMeshGeometryFlags(),
	1.0f, 1.0f, 1.0f);

PxShape* shape = Global::pxPhysics->createShape(hfGeom, *Global::pxMaterial);
physx::PxTransform transform(Global::glmToPX(Global::zero3));
physx::PxRigidStatic* actor = Global::pxPhysics->createRigidStatic(transform);

The Global namespace contains all the PxPhysics and PxScene objects, which are properly initialized (everything else works just fine, even triangle meshes).

The problem is that the call to Global::pxPhysics->createShape(…) returns NULL, so the call to actor->attachShape(…) fails.

Do the material indices have something to do with it? I tried using an existing material’s getReferenceCount(), and it didn’t work either. Not sure I understand how material indices work, as they don’t seem to be used in PhysX3. Any help would be greatly appreciated. Thanks!

I suspect that the problem is the value of materialIndex0 and materialIndex1. If you have a single material then both of these have to be set to be zero.

I recommend running in debug or checked config and taking a look at the error stream. This will tell you if you have passed illegal data to physx.