btCollisionShapeGarbageCollector

Flix
Posts: 456
Joined: Tue Dec 25, 2007 1:06 pm

btCollisionShapeGarbageCollector

Post by Flix »

I'm sure there was an another post about this topic, but I can't find it now, so I start a new post...

Well, this is simply a class that inherits from btAlignedObjectArray<btCollisionShape*>, and:
-> Takes care of possible (nested) child shapes and multiple double entries in a btAlignedObjectArray<btCollisionShape*>.
-> Deletes all shapes on exiting (or when clear() is called)

I extended the same class I posted here http://bulletphysics.org/Bullet/phpBB3/ ... 13&start=0 (that implementation did not handle nested container shapes in the correct way).

I assumed that the container shapes are: btCompoundShape, btUniformScalingShape, btBvhScaledTriangleShape (hope I've not missed others), and that their deletion does not imply the deletion of their child shapes by default.

Hope somebody can test it, improve it and fix it if needed.

DOWNLOAD
btCollisionShapeGarbageCollector.zip
BASIC USAGE

Code: Select all

	#include "btCollisionShapeGarbageCollector.h"	// And add btCollisionShapeGarbageCollector.cpp to your project
	[...]
	btCollisionShapeGarbageCollector m_collisionShapeGarbageCollector;
	
	Optional: inside initPhysics(): m_collisionShapeGarbageCollector.push_back(my_shape); //Mandatory only when at the end of the program no instances of my_shape are present in the objects inside the physic world.
	
	When deleting the btCollisionObjects inside the world:
	//remove the rigidbodies from the dynamics world and delete them
	int i;
	for (i=m_collisionWorld->getNumCollisionObjects()-1; i>=0 ;i--)
	{
		btCollisionObject* obj = m_collisionWorld->getCollisionObjectArray()[i];
		if (!obj) continue;
		m_collisionShapeGarbageCollector.push_back(obj->getCollisionShape(),true);	// Tip for deleting shapes the user forgets to add... (The second argument can be omitted or set to false as well: speed might change but it's not important at this point of the program)
		m_collisionWorld->removeCollisionObject( obj );
		delete obj;
	}
	//m_collisionShapeGarbageCollector.clear();	// Useless...	
DEBUGGING

Code: Select all

	-> Make sure the line: 
	#define DEBUG_BTCOLLISIONSHAPEGARBAGECOLLECTOR
	is active (at the top of btCollisionShapeGarbageCollector.h).		
	-> Inside initPhysics() EXPLICITELY add some shapes with: m_collisionShapeGarbageCollector.push_back(my_shape);
	-> At the end of initPhysics() add:
	#ifdef DEBUG_BTCOLLISIONSHAPEGARBAGECOLLECTOR
	m_collisionShapeGarbageCollector.displayDebugInfo();		
	#endif //DEBUG_BTCOLLISIONSHAPEGARBAGECOLLECTOR
	In the console window all the collision shapes should appear.	
	You may try readding the same shape more than once, adding only parent shapes, etc. and see what's inside the vector. 	
This is an example of debugging console text (in code I've only added to the vector the parent compound shape):
debug_console.png
Hope it's useful.

[Update] updated zip file. Now it works with GImpact compound shapes too.
However, if you plan to use the btGImpactConvexDecompositionShape (in GIMPACTUtils), please uncomment the line:
//#define USE_GIMPACT_UTILS
in "btCollisionShapeGarbageCollector.cpp".
The reason is that the btGImpactConvexDecompositionShape seems to delete its child shapes in its destructor.
You do not have the required permissions to view the files attached to this post.