What is the best way to check if there is overlap between player and static, non-collidable items in bullet physic engine
- by tigrou
I'd like to add non collidable objects (eg: power ups, items, ...) in a game world using Bullet Physics Engine and to know if there is collision between player and them.
Some info : there is a lot of items ( 1000), all are box shapes and they don't overlap.
Here is things i have tried :
btDbvt* bvtItems = new btDbvt(); //btDbvt is a hierachical AABB tree, used by Bullet
foreach(var item ...)
{
btDbvtVolume volume = ... //compute item AABB;
bvtItems->insert(volume, (void*)someExtraData);
}
Then, to find collisions between items and player :
playerRigidBody->getAabb(min, max);
btDbvtVolume playervolume = ... //compute player AABB
bvtItems->collideTV(bvtItems->m_root, playervolume, *someCollisionHandler);
This works fairly well (and its very fast), however, there is a problem : it only check items AABB against player AABB. That loss of precision is acceptable for items but not for player which is not a box. It would actually need another check to make sure player really collide with item but i don't know how to do this in Bullet. It would have been nice to have a function like this :
playerRigidBody->checkCollisionWithAABB();
After doing trying that, I discovered that a btGhostObject exist and seems to have been made for that.
I changed my code like this :
foreach(var item...)
{
btCollisionObject* ghostObject = new btGhostObject();
ghostObject->setCollisionShape(boxShape);
ghostObject->setCollisionFlags(ghostObject->getCollisionFlags()
| btCollisionObject::CF_NO_CONTACT_RESPONSE);
startTransform.setOrigin(...); //item position
ghostObject->setWorldTransform(startTransform);
dynamicsWorld->addCollisionObject(ghostObject, btBroadphaseProxy::SensorTrigger,
btBroadphaseProxy:: CharacterFilter);
}
It also works ok, but there is a huge fps drop (almost ten times slower) which is not acceptable. Maybe there is something missing (forget set a flag) and Bullet is doing extra job for nothing or maybe all that ghostObjects are polluting broad phase and ghostObject is not the right thing for that.
Any help would be appreciated.