Also, this may be useful to help someone get started very quickly. I have a list of Entity objects, each of which implement getBody(), a function that returns a pointer to a b2Body.
This function just minimizes lambda by calling TestSegment on every entity in the scene. It's not even smart enough to use bounding volumes. I have some significantly better-performing implementations of raycast(), but they're more wired-in to my engine than this simpler one is.
Code:
Entity*
raycast( float32* lambda, b2Vec2* normal, const b2Segment& segment, float32 maxLambda )
{
// TODO: this is exceptionally naive, and should be replaced by proper raycasting at some point
float32 min_lambda = maxLambda, this_lambda;
b2Vec2 min_normal, this_normal;
Entity* min_entity = NULL;
for( std::list<Entity*>::iterator it = entities.begin( ); it != entities.end( ); ++it )
{
b2Body* body = (*it)->getBody( );
b2Shape* shape = body->m_shapeList;
while( NULL != shape )
{
if( true == shape->TestSegment( body->GetXForm( ), &this_lambda, &this_normal, segment, maxLambda ) )
{
if( this_lambda < min_lambda )
{
min_lambda = this_lambda;
min_normal = this_normal;
min_entity = (*it);
}
}
shape = shape->m_next;
}
}
if( NULL != min_entity )
{
*lambda = min_lambda;
*normal = min_normal;
}
return min_entity;
}