Kinematic character controller problem

sizeak
Posts: 8
Joined: Thu Jan 07, 2010 6:39 pm

Kinematic character controller problem

Post by sizeak »

Hello,

I recently implemented bullet rigid body collisions and all was working ok. Today I tried to implement a Kinematic character controller based on the demo however now my program exits after a short time. If I make the value passed to stepSimulation smaller then it runs for longer before exiting but this is messes with the movement speed of the character. Any ideas as to why its doing this?

heres some code, its part of one of my classes so there will be some private class variables used etc.

main physics init:

Code: Select all

void Scene::physicsInit()
{
	maxRigidBodies = 1024; //maybe this should be in the scene file instead
	worldAabbMin = new btVector3(-10000, -10000, -10000); //world size for broadphase.
	worldAabbMax = new btVector3(10000, 10000, 10000);	//should probs be loaded from scene file to
	broadphase = new btAxisSweep3(*worldAabbMin, *worldAabbMax, (unsigned short)maxRigidBodies);
	m_overlappingPairCache = broadphase;
	collisionConfig = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfig);
	solver = new btSequentialImpulseConstraintSolver;
	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, m_overlappingPairCache, solver, collisionConfig);
}

init the player

Code: Select all

void Scene::initPlayer()
{
	btTransform startTransform;
	startTransform.setIdentity ();
	startTransform.setOrigin (btVector3(0.0, 0.0, 0.0));

	m_ghostObject = new btPairCachingGhostObject();
	m_ghostObject->setWorldTransform(startTransform);
	broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
	btScalar characterHeight=1.75;
	btScalar characterWidth =1.75;
	btConvexShape* capsule = new btCapsuleShape(characterWidth,characterHeight);
	m_ghostObject->setCollisionShape (capsule);
	m_ghostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);

	btScalar stepHeight = btScalar(0.35);
	m_character = new btKinematicCharacterController (m_ghostObject,capsule,stepHeight);
	dynamicsWorld->addCollisionObject(m_ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
	dynamicsWorld->addAction(m_character);
	
	//not sure these lines needed or not
	dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(m_ghostObject->getBroadphaseHandle(),dispatcher);
	m_character->reset ();	
	m_character->warp (btVector3(0, 0.0, 0.));
}
Set the controllers velocity:

Code: Select all

void Scene::setVel(bool gForward, bool gBackward, bool gLeft, bool gRight, float dt)
{
	///set walkDirection for our character
		btTransform xform;
		xform = m_ghostObject->getWorldTransform ();

		btVector3 forwardDir = xform.getBasis()[2];
	//	printf("forwardDir=%f,%f,%f\n",forwardDir[0],forwardDir[1],forwardDir[2]);
		btVector3 upDir = xform.getBasis()[1];
		btVector3 strafeDir = xform.getBasis()[0];
		forwardDir.normalize ();
		upDir.normalize ();
		strafeDir.normalize ();

		btVector3 walkDirection = btVector3(0.0, 0.0, 0.0);
		btScalar walkVelocity = btScalar(1.1) * 4.0; // 4 km/h -> 1.1 m/s
		btScalar walkSpeed = walkVelocity * dt;

		//rotate view
		if (gLeft)
		{
			btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
			orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),0.01));
			m_ghostObject->getWorldTransform ().setBasis(orn);
		}

		if (gRight)
		{
			btMatrix3x3 orn = m_ghostObject->getWorldTransform().getBasis();
			orn *= btMatrix3x3(btQuaternion(btVector3(0,1,0),-0.01));
			m_ghostObject->getWorldTransform ().setBasis(orn);
		}

		if (gForward)
			walkDirection += forwardDir;

		if (gBackward)
			walkDirection -= forwardDir;

		m_character->setWalkDirection(walkDirection*walkSpeed);
}
Heres the step command I'm using in my draw routine, dt is the time passed since last call in seconds

Code: Select all

dynamicsWorld->stepSimulation(dt, 10);
sizeak
Posts: 8
Joined: Thu Jan 07, 2010 6:39 pm

Re: Kinematic character controller problem

Post by sizeak »

Anyone? Please help, it just dies. It seems to last longer if i move the character backwards but it still dies after 20-30 seconds.
sizeak
Posts: 8
Joined: Thu Jan 07, 2010 6:39 pm

Re: Kinematic character controller problem

Post by sizeak »

No one? I tried it with a sphere instead of a kinematic character controller and it still does it
sizeak
Posts: 8
Joined: Thu Jan 07, 2010 6:39 pm

Re: Kinematic character controller problem

Post by sizeak »

I seem to have solved the crash, the problem looks like I had to many vertices in a btBvhTriangleMeshShape or a btTriangleMesh because after to changing to multiple lower vertex count models rather than a huge one the crash stopped.

Now the only things are for some reason, either the character is moving upwards or the world is falling downwards which is weird because neither has any velocity set :S
The other thing is that movement is very jerky, I thought it might be to do with interpolating rather than simulating some of the steps so I turned the max number of sims per call up but it didn't help