Character will not collide with static objects.

male$
Posts: 2
Joined: Sat Mar 09, 2013 3:27 am

Character will not collide with static objects.

Post by male$ »

So I've been working on using bullet physics in my project, so I'm pretty new to it. My most recent endeavor has been to add a player controlled body that will be walk around my terrain and collide with objects on the terrain. My code is based on the character demo shipped with bullet physics. So basically I have what I thought I need worked out (the 1st person cameras coordinates are based off of the character), however the character will fall, and can be moved while falling, but will not collide with anything while falling, most importantly the terrain. So I'd like if you'd be able to point out what I'm doing wrong, any possible fixes, or at least in a point in the right direction as far as where to look in documentation in samples. Thanks for any replies in advance.

The code (these are all functions of one class) :

Dynamics world initialization

Code: Select all

bullet_CollisionConfig = new btDefaultCollisionConfiguration();
bullet_Dispatcher = new	btCollisionDispatcher(bullet_CollisionConfig);
bullet_Broadphase = new btDbvtBroadphase();
bullet_Solver = new btSequentialImpulseConstraintSolver;
bullet_World = new btDiscreteDynamicsWorld(bullet_Dispatcher, bullet_Broadphase, bullet_Solver, bullet_CollisionConfig);
bullet_World->getDispatchInfo().m_allowedCcdPenetration = 0.0001f;
bullet_World->setGravity(btVector3(0.0f, -30.0f, 0.0f));
Character initialization

Code: Select all

btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(_POS.x, _POS.y, _POS.z));

_ghostobject = new btPairCachingGhostObject();
_ghostobject->setWorldTransform(transform);

btConvexShape* capsule = new btCapsuleShape(_RADIUS, _HEIGHT);
_ghostobject->setCollisionShape(capsule);
_ghostobject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);

_cameraobject = new btKinematicCharacterController(_ghostobject, capsule, 0.75f);

bullet_World->addCollisionObject(_ghostobject, 
	btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);

bullet_World->addAction(_cameraobject);
Camera movement

Code: Select all

if (!_ghostobject || !_cameraobject) return;

//get basic information for movement
btTransform transform = _ghostobject->getWorldTransform();
btVector3 forward = transform.getBasis()[2];
btVector3 up = transform.getBasis()[1];
btVector3 right = transform.getBasis()[0];
forward.normalize();
up.normalize();
right.normalize();

btVector3 walkdirection = btVector3(0.0f, 0.0f, 0.0f);
btScalar walkvelocity = btScalar(1.1f);
btScalar walkspeed = walkvelocity*(timePassed);

//calculate rotation data
if (KeyDown(DIK_A)) //left
{
	btMatrix3x3 orientation = _ghostobject->getWorldTransform().getBasis();
	orientation *= btMatrix3x3( btQuaternion( btVector3(0, 1, 0), 0.01f ) );
	_ghostobject->getWorldTransform().setBasis(orientation);
} else if (KeyDown(DIK_D)) { //right
	btMatrix3x3 orientation = _ghostobject->getWorldTransform().getBasis();
	orientation *= btMatrix3x3( btQuaternion( btVector3(0, 1, 0), -0.01f ) );
	_ghostobject->getWorldTransform().setBasis(orientation);
}

//calculate forward movement
if (KeyDown(DIK_W)) //forward
	walkdirection += forward;
else if (KeyDown(DIK_S)) //back
	walkdirection -= forward;

//set data
_cameraobject->setWalkDirection(walkdirection*walkspeed);
male$
Posts: 2
Joined: Sat Mar 09, 2013 3:27 am

Re: Character will not collide with static objects.

Post by male$ »

So I browsed through the forum and founds a line of code to add to the end of the worlds initialization.

Code: Select all

bullet_Broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
Anyway with this added the collision of the terrain works fine as far as I can tell, but my frame rate drops from around 300fps to 9fps, so not exactly ideal, especially with only a few collision objects in the world, and the one character node, haven't got any reply to the original post, but anyone know the fix to this?
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Character will not collide with static objects.

Post by xexuxjy »

can you enable the profiler to see where all the time is being taken up? when you say 'few objects' , roughly how many do you mean and are they all set to collide with each other?
going from 300fps to 9 seems quite extreme.