Since my objects did not appear to be colliding with the terrain properly, I initialize all the values in the heightfield to a constant, to just produce a flat terrain. I made sure the constant value is well within the min and max height values, and the width and height of the array of values passed in is 2^n + 1 (65 in my case).
I have a set of spheres that fall onto the flat terrain. They stop at the appropriate height, but they roll around in random directions, as if something is generating a lateral force on them. They don't settle to a stop, and usually end up rolling off the edge of the terrain.
I wondered if I had somehow turned off the angular damping, or friction on the spheres, but replacing the terrain with a large flat cube, the spheres slow to a stop correctly.
Here is my heightfield initialization code:
Code: Select all
void vTypeStaticCollisionShape::initializeAsHeightfield(vNode * node, btTransform&offset)
{
this->m_transform = node->currentTransform;
btHeightfieldTerrainShape * heightfield= 0;
int width=0,height=0;
float * pixels = getHeightmapData(node,width, height);
float minX=0,maxX=0,minY=0,maxY=0,minZ=0,maxZ=0;
::getHeightfieldMinMaxValues(node,minX,maxX,minY,maxY,minZ,maxZ);
offset.setIdentity();
offset.setOrigin(btVector3(0.5*(minX+maxX), 0.5*(minY+maxY),0.5*(minZ+maxZ)));
int pixelCount = width*height;
//for debugging, we ignore the image data and just use a constant value to produce a flat terrain
for (int i = 0; i < pixelCount; i++)
pixels[i] = (btScalar)0.1;// (btScalar)interp(0, minY + 0.001, 1, maxY - 0.001, pixels[i]);
heightfield = new btHeightfieldTerrainShape(width,height,
pixels,
1,
minY, maxY,
1, PHY_FLOAT,false);
heightfield->setLocalScaling( btVector3((maxX-minX)/width,1, (maxZ-minZ)/height));
this->m_collisionShape = heightfield;
}