TestSegment
From Box2D Wiki
The TestSegment method of the b2Shape class can be used to find out whether a line segment intersects with that shape. It will also tell you the normal of the first edge of the shape that was found, and how far along the segment that the edge was 'hit'.
The function definition is:
bool TestSegment(const b2XForm& xf, float32* lambda, b2Vec2* normal, const b2Segment& segment, float32 maxLambda) const;
Usage
Suppose you have a line starting at a point 'start' and going to a point 'end', and a shape called pShape. eg.
b2Vec2 start; b2Vec2 end; b2Shape* pShape;
To use these you would do something like (this assumes the shape is attached to a body):
float lambda; b2Vec2 normal; b2Segment segment; segment.p1 = start; segment.p2 = end; bool hit = pShape->TestSegment( pShape->GetBody()->GetXForm(), &lambda, &normal, segment, 1 );
The last parameter (maxLambda) is a fraction specifying how far along the segment to check for an intersection. Eg. if you use 0.5, the segment will be checked only half as far as 'end'. In this case a value of 1 means to use the whole segment. In the example below, a maxLambda of 0.5 would not intersect any of the shapes, a maxLambda of 1 would intersect with just pShape, and a maxLambda of more than 1 would be the dotted line extending out to the right, eg. maxLambda = 2 would intersect with pShape2. You could use TestSegment with very large maxLambda values to check shapes in the distance, without actually having the 'end' point far away. The 'start' and 'end' points are just necessary to define the direction of the segment.
If there is an intersection, the parameter 'normal' will be filled in with the normal of the edge that was intersected (green line in the above picture), and the parameter 'lambda' will be filled in with the lambda value of the intersection point along the segment. Like the maxLambda value, this is just a measure of how far along the segment the intersection point was - eg. in the picture above, it looks like about 0.76
To use the returned lambda value to find the point of intersection, (green point in the picture above) just do as the documentation suggests:
b2Vec2 intersectionPoint = (1 - lambda) * segment.p1 + lambda * segment.p2;

