The thing is, rayTrace is terribly slow, so slow in fact that I feel like I'm doing something wrong?!?
Look at this test scene: 40 AABBs and tracing a ray from left to right. I initially figured that btCollisionWorld::ClosestRayResultCallback will walk the underlying partitioned space in a front-to-back manner so that the ray stops efficiently at the first collision. But it looks like it does the same thing as btCollisionWorld::AllHitsRayResultCallback - it does precise collision detection with ALL the boxes that the ray goes through, then only keeps the closest one?
100 ray tests in this scene take up 14 milliseconds (the same with ClosestCallback as with AllHitsCallback)
Some code:
Code: Select all
struct sMyFilterClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
{
public:
sMyFilterClosestRayResultCallback( const btVector3& _rayFromWorld, const btVector3& _rayToWorld, unsigned int _uiColFlags )
: ClosestRayResultCallback( _rayFromWorld, _rayToWorld )
, uiColFlags( _uiColFlags )
{
}
public:
unsigned int uiColFlags;
public:
virtual bool needsCollision( btBroadphaseProxy* proxy0 ) const
{
bool bBaseResult = ClosestRayResultCallback::needsCollision( proxy0 );
if( bBaseResult ) // if collision test passed, further test with our own criteria
{
btCollisionObject* pColObj = (btCollisionObject*)proxy0->m_clientObject;
sCollisionObject* pMyColObj = (sCollisionObject*)pColObj->getUserPointer();
// game collision info ref; related to visibility / movement / bullet hits etc
if( pMyColObj->GetFlags() & uiColFlags )
return true;
return false;
}
return false;
}
};
sMyFilterClosestRayResultCallback singleResult( from, to, uiColFlags );
m_pWorld->rayTest( from, to, singleResult );