character control problem

xhm20122012
Posts: 1
Joined: Sat Aug 18, 2012 2:44 am

character control problem

Post by xhm20122012 »

Hi everybody
I want to control a character in my 3D scene with physics effect,like walk,back,turn left,turn right,and jump and so on.
I used bullet and openscenegraph ,like http://www.bulletphysics.org/Bullet/php ... Controller I used btPairCachingGhostObject and btKinematicCharacterController. But it seems not works. the code as follow:
first,define a character

Code: Select all

//add character controller
	btTransform startTransform;
	startTransform.setIdentity();
	startTransform.setOrigin(btVector3(0,20,40));

	m_ghostobject = new btPairCachingGhostObject();
	m_ghostobject->setWorldTransform(startTransform);
	sweepBP->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
	
	btScalar character_height = 2.0;
	btScalar character_width = 2.0;
	btConvexShape* character_capsule = new btCapsuleShape(character_width,character_height);
	m_ghostobject->setCollisionShape(character_capsule);
	m_ghostobject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);

	btScalar stepHeight = btScalar(2);

	m_character = new btKinematicCharacterController(m_ghostobject,character_capsule,stepHeight);

	bulletWorld->addCollisionObject(m_ghostobject,btBroadphaseProxy::CharacterFilter,btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
	bulletWorld->addAction(m_character);
then,write the controller

Code: Select all

if (bulletWorld)
		{
			btTransform xform;
			xform = m_ghostobject->getWorldTransform();

			btVector3 forwordDir = xform.getBasis()[2];
			btVector3 upDir = xform.getBasis()[1];
			btVector3 strafeDir = xform.getBasis()[0];
			forwordDir.normalize();
			upDir.normalize();
			strafeDir.normalize();

			btVector3 walkDirection = btVector3(0.0,0.0,0.0);
			btScalar walkVelocity = btScalar(1.1)*4.0;
			btScalar walkSpeed = walkVelocity * 20;

			if (UseEventHandler::btUp)
			{

				walkDirection +=forwordDir;

				return true;
			}

			if (UseEventHandler::btDown)
			{
				walkDirection -= forwordDir;

				return true;
			}

			if (UseEventHandler::btLeft) //A
			{ 

				btMatrix3x3 orn = m_ghostobject->getWorldTransform().getBasis();
				orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),0.01));
				m_ghostobject->getWorldTransform ().setBasis(orn);
				return true; 
			}

			if (UseEventHandler::btRight)//D
			{
				btMatrix3x3 orn = m_ghostobject->getWorldTransform().getBasis();
				orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),-0.01));
				m_ghostobject->getWorldTransform ().setBasis(orn);
				return true; 
			}
			m_character->setWalkDirection(walkDirection*walkSpeed);

		}
		//更新渲染对象
		m_trans=m_ghostobject->getWorldTransform();
		//xiao_matrix->setMatrix(osg::Matrix::translate(m_trans.getOrigin.getX(),m_trans.getOrigin.getY(),m_trans.getOrigin.getZ())*osg::Matrix::rotate(m_trans.getRotation().getW(),m_trans.getRotation().getX(),m_trans.getRotation().getY(),m_trans.getRotation().getZ()));
		xiao_matrix->setMatrix(osg::Matrix::rotate(m_trans.getRotation().getW(),m_trans.getRotation().getX(),m_trans.getRotation().getY(),m_trans.getRotation().getZ())*osg::Matrix::translate(m_trans.getOrigin().getX(),m_trans.getOrigin().getY(),m_trans.getOrigin().getZ()));
and use the osg keybord control to change the value of btUp and so on

that's all.

any suggest?
frank28_nfls
Posts: 7
Joined: Wed Jul 18, 2012 4:54 am

Re: character control problem

Post by frank28_nfls »

after

Code: Select all

m_character->setWalkDirection(walkDirection*walkSpeed);

      }
and before

Code: Select all

m_trans=m_ghostobject->getWorldTransform();
you should let your world update(stepSimulation) to let bullet do calculation.