Remove bodies after collision

b0gd4n
Posts: 14
Joined: Thu Nov 07, 2013 2:38 am

Remove bodies after collision

Post by b0gd4n »

Hey guys.

I have a setup like this for collision detection:

Code: Select all

struct ZombieBulletCallback : public btCollisionWorld::ContactResultCallback
		{
			ZombieBulletCallback(BulletStuff* ptr) : bullet(ptr) {}

			btScalar addSingleResult(btManifoldPoint& cp,
			const btCollisionObjectWrapper* colObj0Wrap,
			int partId0,
			int index0,
			const btCollisionObjectWrapper* colObj1Wrap,
			int partId1,
			int index1)
			{
				// your callback code here
				char strr[256];
				sprintf_s(strr, "zombie-bullet collision \n");
				OutputDebugString(strr);

				// increment points
				bullet->currentPoints += 10;

				// increase the kill counter
				bullet->killCounter += 1;

				// TODO remove bodies

				return 1.f;
			}

			BulletStuff* bullet;
		};

		ZombieBulletCallback zombieBulletCollision(this);

		for (int i = 0; i < zombies.size(); i++) {
			for (int j = 0; j < bullets.size(); j++) {
 				bt_dynamicsWorld->contactPairTest(zombies[i], bullets[j], zombieBulletCollision);
			}
		}
I want to remove the bodies after the collision is detected.

The struct has access to colObj0Wrap and colObj1Wrap (type const btCollisionObjectWrapper*) which, I assume, are the 2 bodies that collide.
I have tried this:

Code: Select all

bullet->bt_dynamicsWorld->removeCollisionObject(colObj0Wrap->getCollisionObject());
, but this gives an error: argument of type const btCollisionObject* is incompatible with param of type btCollisionObject*

How do I remove those 2 bodies from the world?
cj3j
Posts: 8
Joined: Wed Nov 20, 2013 6:40 am

Re: Remove bodies after collision

Post by cj3j »

I'm new to Bullet so take this with a grain of salt. The wrapper's getCollisionObject() function returns a "const btCollisionObject*, which means it intends for the collision object to not be modified during this function (which means bad things would probably happen if you destroyed the object right now).

What I do is add the collision object to an array of objects waiting to be destroyed. I then loop through this array after the call to stepSimulation() completes so the objects can be safely removed.

To get it to compile, you'll have to cast the "const btCollisionObject*" to "btCollisionObject*":

btCollisionObject* objectToRemove = (btCollisionObject*)colObj0Wrap->getCollisionObject();

... then after simulate ...

bullet->bt_dynamicsWorld->removeCollisionObject(objectToRemove);