Removing rigid body from collision callback

SynVis
Posts: 6
Joined: Fri Sep 12, 2008 10:55 pm

Removing rigid body from collision callback

Post by SynVis »

I am guessing this is a no-no. I've setup a callback to notify the application when a collision is detected when a ball (sphere shape) object has collided with something in the world. If the ball collides with a specific object my intent is to remove the object from the world, play a pre-canned animation, and then reinsert the object into the world in a default state (I want to remove the object because the animation takes multiple frames where we are still stepping the world for other things going on). If I actually remove the btRigidBody and re-insert it there are conditions that cause the application to crash. If I just disable the rendering of the object but leave it attached to the physics world nothing crashes.

Now since I'm getting the callback from bullet while it is stepping the sim I am guessing I am removing the btRigidBody while it is still has other collisions queued for processing with other objects in the world (or bullet is going to step extra internal steps due to timing/frame rate slowdown).

I don't mind working around this but I'm just looking for some understanding in this case. The actual crash is inside btPoolAllocator::allocate() dereferencing m_firstFree on line 69 which in this case is a NULL pointer. Other elements of btPoolAllocator look correct (i.e. no crazy numbers, free size looks like the allocate should succeed, just the null m_firstFree). Call stack indicates it is attempting to allocate a new btPersistentManifold inside btConvexTriangleCallback as a result of collision detection (in btCollisionDispatcher::defaultNearCallback).

*edit* Sorry should have mentioned I'm using bullet 2.72
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Removing rigid body from collision callback

Post by Erwin Coumans »

It is not allowed to add or remove objects during the collision callback. It is best to delay those deletions and perform them after the stepSimulation finished (or during the internal tick callback (see dynamicsWorld->setInternalTickCallback)

Hope this helps,
Erwin
SynVis
Posts: 6
Joined: Fri Sep 12, 2008 10:55 pm

Re: Removing rigid body from collision callback

Post by SynVis »

Thanks for the clarification. Especially helpful is the note about the safety of the operation in the internal tick callback (makes sense).