btDynamicsWorld::rayTest() from multiple threads (AI/Entity updates that need to check for the ground and line-of-sight ).
At first glance this seems to be fine, but after finally tracking down a really strange crash in my program I found that
btCollisionWorld.cpp : 405
Code: Select all
if (collisionShape->isCompound())
{
const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(collisionShape);
int i=0;
for (i=0;i<compoundShape->getNumChildShapes();i++)
{
btTransform childTrans = compoundShape->getChildTransform(i);
const btCollisionShape* childCollisionShape = compoundShape->getChildShape(i);
btTransform childWorldTrans = colObjWorldTransform * childTrans;
// replace collision shape so that callback can determine the triangle
btCollisionShape* saveCollisionShape = collisionObject->getCollisionShape();
collisionObject->internalSetTemporaryCollisionShape((btCollisionShape*)childCollisionShape);
rayTestSingle(rayFromTrans,rayToTrans,
collisionObject,
childCollisionShape,
childWorldTrans,
resultCallback);
// restore
collisionObject->internalSetTemporaryCollisionShape(saveCollisionShape);
}
will end up with collisionObject having its internal shape inconsistant with the root shape. Resulting in crashes in other parts of bullet code.
After digging around in the code, it seems that the btCollisionWorld::LocalRayResult::addSingleResult() is the only function down the call chain that even takes
in the collisionObject itself. So it seems that if I don't care about btCollisionObject::GetCollisionShape() inside my result callbacks, it would be safe for me to
comment out the two calls to internalSetTemporaryCollisionShape();
This seems to work with no flaws in my code, but I'm lacking the understanding for why the calls were there to start with, and what feature I might be breaking.
Can anyone see a flaw in my logic here? as it would be very useful to be able to call the ray tests in parallel during my game object updates ( not parallel with stepSimulation() at the moment ).
Or is there another simple solution to this?