btKinematicCharacterController to scene object

Post Reply
orstdani
Posts: 2
Joined: Fri May 22, 2009 8:14 pm

btKinematicCharacterController to scene object

Post by orstdani »

I was wondering how to connect the position/rotation from the movement of a btKinematicCharacterController to a scene node (e.g. in Irrlicht or OGRE)? For rigid bodies I use motion states as described in the documentation, but this does not seem to be available for the character controller.

Thanks in advance,
daniel
jomunoz
Posts: 3
Joined: Tue Jul 21, 2009 1:37 am

Re: btKinematicCharacterController to scene object

Post by jomunoz »

I was also wondering how to link a Motion State to a btKinematicCharacterController, or what would be the best way to move an scenegraph object that describes a Character.

Thanks in advance.
jomunoz
Posts: 3
Joined: Tue Jul 21, 2009 1:37 am

Re: btKinematicCharacterController to scene object

Post by jomunoz »

Hi again, Ive found a way to update the Node/Object position. Please advice if a better way exists.

Create Character

Code: Select all

btTransform startTransform;
startTransform.setIdentity ();
startTransform.setOrigin (btVector3(5, 5, -10));		
btPairCachingGhostObject* m_ghostObject = new btPairCachingGhostObject();  //ghost Object
m_ghostObject->setWorldTransform( startTransform );
mBroadphase->getOverlappingPairCache()->setInternalGhostPairCallback( new btGhostPairCallback() );
m_ghostObject->setUserPointer( node1 );  // node is an Ogre::SceneNode object
m_ghostObject->setCollisionShape( ent2Shape1 );
m_ghostObject->setCollisionFlags( btCollisionObject::CF_CHARACTER_OBJECT );
m_ghostObject->setActivationState(DISABLE_DEACTIVATION);
btScalar stepHeight = btScalar( 0.35 );
btKinematicCharacterController* m_character = new btKinematicCharacterController( m_ghostObject,
														  ent2Shape1,
														  stepHeight );
mBtWorld->addCollisionObject(  m_ghostObject,
                                           btBroadphaseProxy::CharacterFilter,
                                           btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
mBtWorld->addAction(m_character);	
And in every frame the following code is executed. It updates the Node/Object position.

Code: Select all

int size = mBtWorld->getNumCollisionObjects();
int updateCount = 0;
for ( int i = 0; i < size; ++i ) {
	btCollisionObject* colObj = mBtWorld->getCollisionObjectArray()[i];
	if ( colObj->getCollisionFlags() == btCollisionObject::CF_CHARACTER_OBJECT ) {
		btGhostObject* ghost = btGhostObject::upcast(colObj);
		void *userPtr = ghost->getUserPointer();
		SceneNode *node = (SceneNode*)userPtr;
		if (node) {
			btTransform tr = ghost->getWorldTransform();					
			btQuaternion rot = tr.getRotation();
			btVector3 pos = tr.getOrigin();
			node->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
			node->setPosition(pos.x(), pos.y(), pos.z());
		}
	}
Post Reply