Lots of static bodies

Piter467
Posts: 2
Joined: Sun Mar 09, 2014 2:35 pm

Lots of static bodies

Post by Piter467 »

Hi all,

in the game I'm making, the map is made of cubes (it isn't a minecraft clone). There are lots of them, and although they're static bodies the perfomance is poor...

Is there any configuration to improve it?

Any advice is welcome... Thank you


Edit: First I created all the bodies with their respective shapes. Then I tried creating only one body with a compoundshape, but Im still getting bad FPS
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Lots of static bodies

Post by Flix »

Piter467 wrote:Is there any configuration to improve it?
Piter467 wrote:Edit: First I created all the bodies with their respective shapes. Then I tried creating only one body with a compoundshape, but Im still getting bad FPS
Well, these are only my very generic suggestions (but I bet you already know them):
1) Unwrap your bodies from the btCompoundShape. AFAIK the btCompoundShape is not optimized for static objects.
2) Use: m_dynamicsWorld->setForceUpdateAllAabbs(false);
Create your bodies as static (not kinematic) and if you need to move them, call: m_dynamicsWorld->updateSingleAabb(myStaticBoxShapedCollisionObjectIveJustMoved);
3) [This optimizes memory only (not performance), and can be used only if the static boxes are not used in any constraints and if your code supports both btRigidBodies and 'naked' btCollisionObjects]: Add static objects as btCollisionObjects, in a similiar (the exact code has not been tested) way:

Code: Select all

btCollisionObject* co = new btCollisionObject();
    co->setCollisionShape(myBoxShape);
    co->setWorldTransform(T);
    co->setActivationState(ISLAND_SLEEPING); // very important (otherwise bodies on it might not fall asleep!)
    m_dynamicsWorld->addCollisionObject(co,btBroadphaseProxy::StaticFilter,btBroadphaseProxy::AllFilter&(~(btBroadphaseProxy::StaticFilter | btBroadphaseProxy::KinematicFilter)));
    co->setCollisionFlags(co->getCollisionFlages|btCollisionObject::CF_STATIC_OBJECT); // This is an additional 'static' flag
    co->setFriction(friction);
    co->setRestitution(restitution);
For further info, we can check the flags that btDiscreteDynamicsWorld::addRigidBody(...) sets by default when adding a static object.
4) Use a btDbvtBroadphase when you create your world (this should be the default).
5) [For memory only] If you use big btCollisionShapes, consider wrapping them inside btUniformScalingShapes/btScaledBvhTriangleMeshShapes when possible (although for plain btBoxShapes, I'm not sure this saves some byte or not); and of course reuse the same collision shapes whenever possible.

That's all that I use (but I've never needed to add a huge amount of static objects, so that the performance is poor, so far).
If you have other hints, please post them :D
Piter467
Posts: 2
Joined: Sun Mar 09, 2014 2:35 pm

Re: Lots of static bodies

Post by Piter467 »

First of all, thank you Flix for your answer. I'm quite new to Bullet :?

I will try using
Flix wrote: co->setActivationState(ISLAND_SLEEPING); // very important (otherwise bodies on it might not fall asleep!)
co->setCollisionFlags(co->getCollisionFlages|btCollisionObject::CF_STATIC_OBJECT); // This is an additional 'static' flag
since now what I do is join all the meshes (with a tool), export the obj, import it to Blender and then export the .bullet to load it later with the Bullet worldimporter. So, I think the only solution to my problem is to reduce the resolution of the mesh (.obj) by removing inner geometry, using the modifier Decimate, or by any other methods, so that it has less vertexes (= contact points).
Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

Re: Lots of static bodies

Post by Flix »

Piter467 wrote:now what I do is join all the meshes (with a tool), export the obj, import it to Blender and then export the .bullet to load it later with the Bullet worldimporter. So, I think the only solution to my problem is to reduce the resolution of the mesh (.obj) by removing inner geometry, using the modifier Decimate, or by any other methods, so that it has less vertexes (= contact points).
Yes, you can just have a single btBvhTriangleMeshShape out of it if you want.