But my character will pass through the terrain without collision.
And I also tested rigid body with capsule,sphere,box,but they were working well with terrain.
Where is my problem?
init terrain:
Code: Select all
int nUpAxis = 2;
m_pShape = new btHeightfieldTerrainShape(m_szGrid
,m_szGrid
,&m_vHeightField[0]
,1.0f
,fMinHeight
,fMaxHeight
,nUpAxis
,PHY_FLOAT
,bFlipQuadEdges);
btAssert( (!m_pShape || m_pShape->getShapeType() != INVALID_SHAPE_PROXYTYPE) );
btVector3 localInertia;
m_pShape->calculateLocalInertia(0.0f,localInertia);
btTransform myTransform;
myTransform.setIdentity();
m_pRigidBody = new btRigidBody(0.0f,new btDefaultMotionState(myTransform),m_pShape,localInertia);
Code: Select all
m_pShape = new btCapsuleShapeZ(0.5f,0.5f);
m_vOrient = btVector3(0.0f,0.0f,0.5f);
btScalar mass(1.0f);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
m_pShape->calculateLocalInertia(mass,localInertia);
btTransform startTransform;
startTransform.setIdentity();
CPXRigidBody *pBody = dynamic_cast<CPXRigidBody*>(p);
m_pGhostObject = new btPairCachingGhostObject();
startTransform = pBody->GetBody()->getWorldTransform();
m_pGhostObject->setWorldTransform(startTransform);
//btScalar characterHeight=0.25;
//btScalar characterWidth =0.25;
m_pGhostObject->setCollisionShape (m_pShape);
m_pCharacter = new btKinematicCharacterController (m_pGhostObject
,m_pShape)
,0.2
,2);
m_pCharacter->setWalkDirection(btVector3(0.0f,0.0f,0.0f));
m_pGhostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);
pScene->m_pOverlappingPairCache->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
m_pDynamicsWorld->addCollisionObject(m_pGhostObject
,btBroadphaseProxy::CharacterFilter
,btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
m_pDynamicsWorld->addCharacter(m_pCharacter);