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);
}
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.));
}
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);
}
Code: Select all
dynamicsWorld->stepSimulation(dt, 10);