results of rayTest vs Multiple Objects

nigul
Posts: 12
Joined: Tue Sep 22, 2009 1:40 pm

results of rayTest vs Multiple Objects

Post by nigul »

Hello,

I'd like to simply get the results of a rayTest vs several bullet objects in a scene rendered by Ogre3D :

Indeed, I use two kind of objects : static planes that aren't associated to an Ogre renderable object : the userPointer is set to NULL
Rigid Bodies that are associated to Ogre renderable objects (using the userPointer).

I'd like to detect only the rigid bodies by using the following method

1) perform a rayTest vs the dynamicsWorld
2) get a list or array (or whatever container) of all the objects that collide with the ray
3) sort objects by distance
4) select the first "selectable" object (while(object->getUserPointer()==NULL){try next object}

points 1) 3) and 4) are no problem to me, but I'm stuck with point 2)

Is there a simple way to handle point 2) ?
Dominik
Posts: 32
Joined: Fri Dec 19, 2008 2:51 pm

Re: results of rayTest vs Multiple Objects

Post by Dominik »

Bullet's ray test uses the btCollisionWorld::RayResultCallback, whose function addSingleResult is called for each found ray intersection.
Sinply inherit this class, and add your own code to addSingleResult, where you check if the object's user pointer is non-NULL, and then store it somewhere (preferrably a sorted list).

example:

Code: Select all

struct MyRaytestResult
{
  MyRaytestResult( btObject* pObject, float fFraction )
  : pHitObject( pObject ), fHitFraction( fFraction )
  {}

  btObject* pHitObject;
  float        fHitFraction;
}

structCMyRaytestCallback : public btCollisionWorld::RayResultCallback
{
  virtual btScalar addSingleResult( btCollisionWorld::LocalRayResult& oRayResult,
                                               bool bNormalInWorldSpace)
  {
    if( oRayResult.m_collisionObject->getUserPointer() == NULL )
      return 1.0f;
     Results.push( MyRaytestResult( oRayResult.m_collisionObject, oRayResult.m_hitFraction ) );
    return oRayResult.m_hitFraction;
  }

  sort()
  {
    //sort when everything is done, or sort manually when inserting, or use a priority queue or whatever
  }
  
  std::some_stl_container<MyRaytestResult> Results;
}
nigul
Posts: 12
Joined: Tue Sep 22, 2009 1:40 pm

[SOLVED]Re: results of rayTest vs Multiple Objects

Post by nigul »

Thank you Dominik,

This actually works fine. I can now flag my objects with a dynamic SELECTABLE_BY_RAYTEST attribute.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: results of rayTest vs Multiple Objects

Post by Erwin Coumans »

It is likely more efficient to ignore/filter out certain ("selectable") objects for the ray test using either collision filter flags or by overriding the 'needsCollision' virtual method in RayResultCallback.

Hope this helps,
Erwin