btHeightfieldTerrainShape help please!!

Mars_999
Posts: 25
Joined: Tue Dec 04, 2007 7:48 pm

btHeightfieldTerrainShape help please!!

Post by Mars_999 »

Hi,

I have been looking around the web and others as of lately are having issues with objects falling through the btHeightfieldTerrainShape collision shape...

Please look here
http://bulletphysics.org/Bullet/phpBB3/ ... 33&start=0

There are others, but I am having the same issue. I am unsure but from the looks of it, the values are incorrect for the heights of the terrain shape. I sent in an array of just 5 for height values and the debug renderer shows peaks and valleys for my data set?


Thanks for any help....
Mars_999
Posts: 25
Joined: Tue Dec 04, 2007 7:48 pm

Re: btHeightfieldTerrainShape help please!! Erwin?

Post by Mars_999 »

I really think the problem lies in the scaling? I guess my dumb a** can't figure out the scaling... Anyone else have a decent explanation on using scaling?

Code: Select all

HeightFieldActor(33, 33, imageData, 1.0f, 0.0f, 255.0f, 1, PHY_UCHAR, false, cml::vector3f(5,5,5));

HeightFieldActor(int x, int y, void* data, 
					 btScalar heightScale = 1.0f,
					 btScalar minHeight   = 0.0f,
					 btScalar maxHeight   = 255.0f, 
					 int upAxis           = 1,
					 PHY_ScalarType heightDataType = PHY_FLOAT,
					 bool flipQuadEdges = false,
					 cml::vector3f& scaling = cml::vector3f(1,1,1))
	{ 
		/*
		collisionShape = new btHeightfieldTerrainShape(x, y, data,  
													   heightScale, 
													   minHeight,
													   maxHeight,
													   upAxis, 
													   heightDataType, 
													   flipQuadEdges);
		*/
		collisionShape = new btHeightfieldTerrainShape(x, y, data, maxHeight, upAxis, false, flipQuadEdges);
		btHeightfieldTerrainShape* shape = dynamic_cast<btHeightfieldTerrainShape*>(collisionShape);
		shape->setUseDiamondSubdivision(true);
		collisionShape->setLocalScaling(Vector3f_to_btVector3(scaling));
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(0,maxHeight*.5f*scaling[1],0));
		motionState = new btDefaultMotionState(tr);
		rigidBody   = new btRigidBody(0, motionState, collisionShape, btVector3(0,0,0));
		rigidBody->setCollisionFlags(rigidBody->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
	}
robagar
Posts: 48
Joined: Fri May 21, 2010 1:49 am

Re: btHeightfieldTerrainShape help please!!

Post by robagar »

Perhaps this might help - it's my code for initializing a terrain page's physical properties. AFAICS, btHeightfieldTerrainShape defaults to a heightfield with grid points 1 unit apart, so you have to scale it if your point spacing is different.

Note that in my world, z is up not y.

Code: Select all

void TerrainPage::initPhysics()
{
  btScalar minHeight = m_boundingBox.getMinimum().z;
  btScalar maxHeight = m_boundingBox.getMaximum().z;

  m_pCollisionShape = new btHeightfieldTerrainShape(m_size, m_size, 
                                                    m_heightfield,
                                                    1.0, // scale, not used for float data
                                                    minHeight, 
                                                    maxHeight,
                                                    2, // z is up,
                                                    PHY_FLOAT, 
                                                    false);

  // scale horizontally by the distance between grid points
  btScalar gridSpacing  = m_width / (m_size - 1);
  m_pCollisionShape->setLocalScaling(btVector3(gridSpacing, gridSpacing, 1.0));

  // set origin to middle of heightfield
  Vector3 c = m_boundingBox.getCenter();
  btTransform tr;
  tr.setIdentity();
  tr.setOrigin(btVector3(c.x, c.y, c.z));

  m_pMotionState = new btDefaultMotionState(tr);

  m_pPhysicsBody = new btRigidBody(0, m_pMotionState, m_pCollisionShape);
}