• Advertisement
Sign in to follow this  

Basic Terrain problem in PhysX

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm getting a problem with creating some terrain with PhysX. I have a function called GenerateTerrain, which will return correct vertices and indices. These are then passed into my Graphics code, and my Physics code (shown below). I know GenerateTerrain works fine because when I few the program in wireframe mode, the triangles are correctly drawn. However the problem I am having is with triMeshShape.meshData = m_physicsSDK->createTriangleMesh(MemoryReadBuffer(buf.data)); This compiles fine, but crashes here (the call stack points to PhysX code, but it's because of this line). Can anyone see the problem (MemoryWriteBuffer and MemoryReadBuffer are the same as the samples that come with PhysX).
void Physics::CreateTerrain(int numVertsPerRow, int numVertsPerCol, VECTOR3 center, std::vector<VECTOR3> vertices, std::vector<UINT> indices)
{
	UINT numVertices  = numVertsPerRow * numVertsPerCol;
	UINT numTriangles = (numVertsPerRow-1) * (numVertsPerCol-1) * 2;

	NxVec3* verts;
	UINT* faces;

	verts = new NxVec3[numVertices];
	faces = new UINT[numTriangles];

	for (unsigned int i = 0; i < numVertices; i++)
	{
		verts.x = vertices.x;
		verts.y = vertices.y;
		verts.z = vertices.z;
	}

	for (unsigned int i = 0; i < numTriangles; i++)
	{
		faces = indices;
	}

	NxTriangleMeshDesc triMesh;
	triMesh.numVertices	= numVertices;
	triMesh.numTriangles	= numTriangles;
	triMesh.pointStrideBytes	= sizeof(NxVec3);
	triMesh.triangleStrideBytes	= 3*sizeof(UINT);
	triMesh.points		= verts;
	triMesh.triangles		= faces;
	triMesh.flags		= NX_MF_HARDWARE_MESH;
	NxTriangleMeshShapeDesc triMeshShape;

	NxInitCooking();
	
	// Cooking from memory
	MemoryWriteBuffer buf;
	bool status = NxCookTriangleMesh(triMesh, buf);// Returns true
	if (!status) return;
	triMeshShape.meshData = m_physicsSDK->createTriangleMesh(MemoryReadBuffer(buf.data)); //PROBLEM HERE

	NxActorDesc actor;
	actor.shapes.pushBack(&triMeshShape);
	actor.globalPose.t = NxVec3(center.x, center.y, center.z);

	m_entityVector.push_back( m_scene->createActor(actor) );
}


Share this post


Link to post
Share on other sites
Advertisement
By the look of:
for (unsigned int i = 0; i < numTriangles; i++) {
faces = indices;
}



Your indices need to be in "0,1,2" (etc) format for each triangle.

then change to:
for (unsigned int i = 0; i < numTriangles * 3; i++) {
faces = indices;
}


Share this post


Link to post
Share on other sites
Oh wow, i feel like such a fool now. My graphics code has the *3.

Thanks ever so much.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement