Performance problems

Post Reply
robwypior
Posts: 2
Joined: Sun Jun 04, 2017 8:06 pm

Performance problems

Post by robwypior » 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:

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));
This is where terrain is created:

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);
This is where character is created:

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)));
Character uses MotionStatePairCachingGhostObject class which basically extends btPairCachingGhostObject to replace this method:

Code: Select all

void MotionStatePairCachingGhostObject::setWorldTransform(const btTransform& worldTrans)
{
	btPairCachingGhostObject::setWorldTransform(worldTrans);
	this->motionState->setWorldTransform(worldTrans);
}
And the simulation is stepped using following:

Code: Select all

this->dynamicsWorld->stepSimulation(1.0f / 60.0f, 10);
I tried fiddling with those numbers, but with no improvement.

I completely run out of ideas, what am I missing here?

pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

Re: Performance problems

Post by pico » Wed Dec 05, 2018 7:46 am

Hi,

Bullet in debug mode can slow down a lot (like 800% slower in some of my cases).
Try release mode to see if there is really a performance penalty.

User avatar
drleviathan
Posts: 460
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Performance problems

Post by drleviathan » Wed Dec 05, 2018 4:08 pm

Alternatively, you could use the CProfileManager API to get some detailed information about where the time is being spent. Look in the btQuickprof.h file for see its API. In short you can call CProfileManager::reset() before your stepSimulation(), then CProfileManager::dumpAll() after.

Post Reply