btKinematicCharacterController no collision on btStaticPlane

pjStyle
Posts: 4
Joined: Thu Feb 26, 2009 6:36 pm

btKinematicCharacterController no collision on btStaticPlane

Post by pjStyle »

Hey guys,

I am testing out Bullet's capabilities and liking it so far! I do have a problem with the character controller.

I have used the code from the character demo as a guideline and created the character and capsule. In addition to that i have a btStaticPlaneShape in my scene. Now if i understand correctly, the character will fall down until it hits this plane. After that i should be able to move the character around.

But that doesn't happen! :D The character just falls down, i even tried to set the gravity to (0,0,0) and it would still fall down endlessly. What is it i am forgetting? Here is the code for the creation of the plane and character:

Code: Select all

			{
				std::fstream file_op("c:\\test_file.txt", std::ios::out);

				btVector3 worldAabbMin(-10000,-10000,-10000);
				btVector3 worldAabbMax(10000,10000,10000);
				int maxProxies = 1024;
				btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);

				btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
				btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

				btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

				btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);

				dynamicsWorld->setGravity(btVector3(0,-10,0));


				btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);

				btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
				btRigidBody::btRigidBodyConstructionInfo
					groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
				btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
				dynamicsWorld->addRigidBody(groundRigidBody);


				btKinematicCharacterController* m_character;
				btPairCachingGhostObject* m_ghostObject;
				btVector3 worldMin(-10000,-10000,-10000);
				btVector3 worldMax(10000,10000,10000);
				btAxisSweep3* sweepBP = new btAxisSweep3(worldMin,worldMax);

				{   
					btTransform startTransform;
					startTransform.setIdentity ();
					startTransform.setOrigin (btVector3(0.0, 20.0, 0.0));

					m_ghostObject = new btPairCachingGhostObject();
					m_ghostObject->setWorldTransform(startTransform);
					sweepBP->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);      

					///only collide with static for now (no interaction with dynamic objects)					
					dynamicsWorld->addCollisionObject(m_ghostObject);
					dynamicsWorld->addCharacter(m_character);
				}

				float y = 0;

				for (int i=0 ; i<3000 ; i++) {
					dynamicsWorld->stepSimulation(1/60.f,10);

					btTransform trans = m_ghostObject->getWorldTransform();
					y = trans.getOrigin().getY();

					file_op << "Capsule height: " << y << std::endl;
				}

				file_op.close();
			}
Output:

Code: Select all

Capsule height: 19.3
Capsule height: 18.95
Capsule height: 18.6
Capsule height: 18.25
Capsule height: 17.9
Capsule height: 17.55
Capsule height: 17.2
Capsule height: 16.85
Capsule height: 16.5
Capsule height: 16.15
Capsule height: 15.8
Capsule height: 15.45
Capsule height: 15.1
Capsule height: 14.75
Capsule height: 14.4
Capsule height: 14.05
Capsule height: 13.7
Capsule height: 13.35
Capsule height: 13
Capsule height: 12.65
Capsule height: 12.3
Capsule height: 11.95
Capsule height: 11.6
Capsule height: 11.25
Capsule height: 10.9
Capsule height: 10.55
Capsule height: 10.2
Capsule height: 9.85001
Capsule height: 9.50001
Capsule height: 9.15001
Capsule height: 8.80001
Capsule height: 8.45001
Capsule height: 8.10001
Capsule height: 7.75001
Capsule height: 7.40001
Capsule height: 7.05001
Capsule height: 6.70001
Capsule height: 6.35001
Capsule height: 6.00001
Capsule height: 5.65001
Capsule height: 5.30001
Capsule height: 4.95001
Capsule height: 4.60001
Capsule height: 4.25001
Capsule height: 3.90001
Capsule height: 3.55001
Capsule height: 3.20001
Capsule height: 2.85001
Capsule height: 2.50001
Capsule height: 2.15001
Capsule height: 1.80001
Capsule height: 1.45001
Capsule height: 1.10001
Capsule height: 0.750009
Capsule height: 0.400009
Capsule height: 0.0500092
Capsule height: -0.299991
Capsule height: -0.649991
Capsule height: -0.999991
Capsule height: -1.34999
Capsule height: -1.69999
Capsule height: -2.04999
Capsule height: -2.39999
Capsule height: -2.74999
Capsule height: -3.09999
Capsule height: -3.44999
mickey
Posts: 107
Joined: Fri Sep 19, 2008 6:08 pm

Re: btKinematicCharacterController no collision on btStaticPlane

Post by mickey »

Hi

The btStaticPlaneShape should hold your character and now allow it to fall through. It works perfectly for me.

This is how I initialized mine (its almost identical to yours but i could not spot the difference)

Code: Select all

btTransform startTransform;
startTransform.setIdentity ();		

// Create a static plane for the ground
btCollisionShape *groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);			
mCollisionShapes.push_back(groundShape);	

btRigidBody *groundBody = CreateRigidBody( .0, startTransform, groundShape );
groundBody->setRestitution(0);
groundBody->setFriction(1.0f);	
mDynamicsWorld->addRigidBody(groundBody);	
and my character controller:

Code: Select all

btTransform startTransform;
startTransform.setIdentity ();
startTransform.setOrigin (btVector3(0.0, 20.0, 0.0));

mCharacterGhostObject = new btPairCachingGhostObject();
mCharacterGhostObject->setWorldTransform(startTransform);					

btScalar characterHeight= 3.f;
btScalar characterWidth = 1.f;
btConvexShape* capsule = new btCapsuleShape(characterWidth,characterHeight);	
mCollisionShapes.push_back(capsule);	
mCharacterGhostObject->setCollisionShape (capsule);
mCharacterGhostObject->setCollisionFlags (btCollisionObject::CF_CHARACTER_OBJECT);	

btScalar stepHeight = btScalar(0.35);
mCharacterController = new btKinematicCharacterController (mCharacterGhostObject, capsule, stepHeight);		

mDynamicsWorld->addCollisionObject(mCharacterGhostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
mDynamicsWorld->addCharacter(mCharacterController);				
You can download the complete source at my site if you want there's an exe of it as well.

Hope that helps.
pjStyle
Posts: 4
Joined: Thu Feb 26, 2009 6:36 pm

Re: btKinematicCharacterController no collision on btStaticPlane

Post by pjStyle »

I still havent been able to resolve this issue. My character is still having no collision whatsoever with the world it seems. On the other had i am able to create a box shaped rigid body, and it will collide with the scenery as expected.

I have found another question regarding this issue. And the poster has resolved his issue. Here is the link.

http://www.bulletphysics.com/Bullet/php ... +collision

What i don't understand is what he means by broadphase interface?

Edit:

I fixed the problem. The following line had to be added in the initialization part of the physics world.

Code: Select all

m_dynamicsWorld->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());