remove bodies after collision (bullet physics)
I have a setup like this for collision detection:
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:
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?
The difference that causes the incompatibility is the const qualifier of const btCollisionObject*
.
I never tried to remove objects during collision or whatever dispatching and I doubt, that it will work flawlessly.
Since you're doing manual contact tests, you could try to remove the collision object using the const_cast
operator: bullet->bt_dynamicsWorld->removeCollisionObject(const_cast<btCollisionObject*>(colObj0Wrap->getCollisionObject()));
but again, enforcing that might not work properly now, or after changes to the simulation steps.
Instead, I would collect the 'dead' zombies via ContactResultCallback
in another container defeatedZombies
for deferred removal from bt_dynamicsWorld
and zombies
(at the end of a simulation step, or your nested loops, respectively).
上一篇: 子弹物理盒不旋转
下一篇: 碰撞后移除物体(子弹物理)