btHeightfieldTerrainShape is always flat as a pancake

Prot
Posts: 3
Joined: Thu Oct 30, 2008 7:25 pm

btHeightfieldTerrainShape is always flat as a pancake

Post by Prot »

Hello, everyone. I was wondering if one of you could help me with a small problem I'm having with Bullet.

I have a height field specified as an array of unsigned chars. I'm using that height field to successfully render terrain using my own functions. I also had a bvhTriangleMeshShape for collision, and it was working well, but since there are more than 100000 triangles there was a lot of overhead when creating the shape (due to function calls), so I decided to give the HeightfieldShape a try. However, it doesn't seem to like the data I'm sending it.

I tried everything. All the different modes, real maximum height or just a very high number, I checked the demo, read the tutorial, searched in google, everywhere I go I find that I seem to be doing everything correctly, but still the heightfield does not recognize the data. I can scale it, and I can change it into diamond subdivision mode just fine. My stuff collides with it when it hits it. But it's FLAT.

Code: Select all

		unsigned char* heightfield = new (unsigned char[mapa.getWidth() * mapa.getHeight()]);
		int highest = -999999, j = 0;
		for (i = 0; i < NUM_VERTS_X; i++)
			for (j = 0; j < NUM_VERTS_Z; j++) {
				heightfield[j*NUM_VERTS_X+i] = alturas[(i*NUM_VERTS_Z+j)*3];
				if (heightfield[j*NUM_VERTS_X+i] > highest)
					highest = heightfield[j*NUM_VERTS_X+i];
			}

		btHeightfieldTerrainShape* heightfieldShape = new btHeightfieldTerrainShape(mapa.getWidth(), mapa.getHeight(), heightfield, highest, 1, false, false);
		heightfieldShape->setLocalScaling(btVector3(TRIANGLE_SIZE,0,TRIANGLE_SIZE));
		btCollisionShape* trimeshShape = heightfieldShape;
			
		float mass = 0.f;
		btTransform	startTransform;
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(0,0,0));

		btRigidBody* staticBody= localCreateRigidBody(mass, startTransform,trimeshShape);
alturas contains the data in an array of unsigned chars straight from a PNG (mapa.getBytes()). mapa is the libpng wrapper, it's written by me but extensively tested and I already checked that "heightfield" does contain the heightfield after the copy cycles anyway.; mapa.getWidth() and mapa.getHeight() return exactly the same values as NUM_VERTS_X and NUM_VERTS_Z.

Any help would be much appreciated.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btHeightfieldTerrainShape is always flat as a pancake

Post by Erwin Coumans »

but since there are more than 100000 triangles there was a lot of overhead when creating the shape (due to function calls),
It should be possible to use the btBvhTriangleMeshShape. Are you using the btTriangleMesh? The ::addTriangle is very slow, which will be fixed. Have you tried to use the btTriangleIndexVertexArray instead? Also, you can serialize the BVH to speed up initialization, see Demos/ConcaveDemo.

Not sure what is happening with the btHeightfieldTerrainShape. There is some upcoming patch for btHeightfieldTerrainShape that might resolve the issue.
Which version of Bullet are you using?
Thanks,
Erwin
Prot
Posts: 3
Joined: Thu Oct 30, 2008 7:25 pm

Re: btHeightfieldTerrainShape is always flat as a pancake

Post by Prot »

2.72. It was indeed the addTriangle being incredibly slow. Let me try the vertex array instead...
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: btHeightfieldTerrainShape is always flat as a pancake

Post by Erwin Coumans »

You can try attached zipped btTriangleMesh.cpp, it avoids the slow duplicate vertex search in addTriangle by default.

Still, it is better to use btTriangleIndexVertexArray .
Hope this helps,
Erwin
You do not have the required permissions to view the files attached to this post.
Prot
Posts: 3
Joined: Thu Oct 30, 2008 7:25 pm

Re: btHeightfieldTerrainShape is always flat as a pancake

Post by Prot »

Thank you, but I already reimplemented with btTriangleIndexVertexArray and it's working perfectly now :)
I'll keep your example for future reference! Thanks a lot.