Performance problems
Posted: Sun Dec 02, 2018 9:53 pm
Hello,
I've implemented Bullet Physics into my game engine and I've encountered a performance problem.
At first I thought that the problem was related to my character being close to too many triangles, however later I realized the problem is a bit weirder.
I've got a terrain mesh made of btBvhTriangleMeshShape made totally out of 600 triangles and character model that's using btBoxShape. Walking on the ground shape is good until a place where a lot of triangles are bundles together (420 triangles) - they form a hill. The problem, however, only appears when walking DOWN the hill, when walking up the hill - everything is as it should be.
I'm using Bullet library in debug mode - I've heard that Bullet can slow down a lot when in debug mode, but I suppose 600 triangles is relatively small number to actually make such a difference - the FPS drops from 60 to 30.
This is the code where I initialize the physics world:
This is where terrain is created:
This is where character is created:
Character uses MotionStatePairCachingGhostObject class which basically extends btPairCachingGhostObject to replace this method:
And the simulation is stepped using following:
I tried fiddling with those numbers, but with no improvement.
I completely run out of ideas, what am I missing here?
I've implemented Bullet Physics into my game engine and I've encountered a performance problem.
At first I thought that the problem was related to my character being close to too many triangles, however later I realized the problem is a bit weirder.
I've got a terrain mesh made of btBvhTriangleMeshShape made totally out of 600 triangles and character model that's using btBoxShape. Walking on the ground shape is good until a place where a lot of triangles are bundles together (420 triangles) - they form a hill. The problem, however, only appears when walking DOWN the hill, when walking up the hill - everything is as it should be.
I'm using Bullet library in debug mode - I've heard that Bullet can slow down a lot when in debug mode, but I suppose 600 triangles is relatively small number to actually make such a difference - the FPS drops from 60 to 30.
This is the code where I initialize the physics world:
Code: Select all
this->collisionConfiguration = new btDefaultCollisionConfiguration();
this->dispatcher = new btCollisionDispatcher(this->collisionConfiguration);
this->overlappingPairCache = new btDbvtBroadphase(); // btAxis3Sweep
this->solver = new btSequentialImpulseConstraintSolver();
this->dynamicsWorld = new btDiscreteDynamicsWorld(this->dispatcher, this->overlappingPairCache, this->solver, this->collisionConfiguration);
this->ghostPairCallback = new btGhostPairCallback();
this->dynamicsWorld->getPairCache()->setInternalGhostPairCallback(ghostPairCallback);
this->overlappingPairCache->getOverlappingPairCache()->setInternalGhostPairCallback(this->ghostPairCallback);
this->dynamicsWorld->setGravity(btVector3(0, -GRAVITY, 0));
Code: Select all
this->collisionObject = new btCollisionObject();
this->collisionObject->getWorldTransform().setIdentity();
this->collisionObject->setRestitution(0.0f);
this->collisionObject->setUserPointer(this);
this->collisionObject->setFriction(1.2f);
this->collisionObject->setCollisionShape(this->physics.getEmptyShape());
this->collisionObject->setCollisionFlags(btCollisionObject::CollisionFlags::CF_STATIC_OBJECT);
this->collisionObject->setCcdMotionThreshold(1);
this->collisionObject->setCcdSweptSphereRadius(0.2f);
this->physics.getDynamicsWorld()->addCollisionObject(this->collisionObject);
Code: Select all
this->collisionObject = this->ghost = new MotionStatePairCachingGhostObject(this->motionState);
this->collisionObject->getWorldTransform().setIdentity();
this->ghost->setCollisionShape(this->physics.getEmptyShape());
this->ghost->getWorldTransform().setIdentity();
this->ghost->setRestitution(0.0f);
this->ghost->setUserPointer(this);
this->ghost->setFriction(1.2f);
this->ghost->setCollisionFlags(btCollisionObject::CollisionFlags::CF_CHARACTER_OBJECT);
this->ghost->setCcdMotionThreshold(1);
this->ghost->setCcdSweptSphereRadius(0.2f);
btConvexShape *shape = static_cast<btConvexShape*>(this->collisionShape->getShape());
this->controller = new btKinematicCharacterController(this->ghost, shape, 0.2f, fromVec3(glm::vec3(0.0f, 1.0f, 0.0f)));
this->physics.getDynamicsWorld()->addCollisionObject(this->ghost, btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter);
this->physics.getDynamicsWorld()->addCharacter(this->controller);
this->controller->setWalkDirection(fromVec3(glm::vec3(0.0f)));
Code: Select all
void MotionStatePairCachingGhostObject::setWorldTransform(const btTransform& worldTrans)
{
btPairCachingGhostObject::setWorldTransform(worldTrans);
this->motionState->setWorldTransform(worldTrans);
}
Code: Select all
this->dynamicsWorld->stepSimulation(1.0f / 60.0f, 10);
I completely run out of ideas, what am I missing here?