I found the following description for TestSegment in the .chm file included with v2.0.1, it will also show up in the intellisense if you are using MSVC:
There seem to be a few people still having trouble using this so perhaps I should go into even more detail. Suppose you have a line starting at a point 'start' and going to a point 'end', and a shape called pShape. eg.
To use these you would do something like (this assumes the shape is attached to a body):
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:
Quote:
virtual bool b2Shape::TestSegment ( const b2XForm & xf,
float32 * lambda,
b2Vec2 * normal,
const b2Segment & segment,
float32 maxLambda
) const [pure virtual]
Perform a ray cast against this shape.
Parameters:
xf the shape world transform.
lambda returns the hit fraction. You can use this to compute the contact point p = (1 - lambda) * segment.p1 + lambda * segment.p2.
normal returns the normal at the contact point. If there is no intersection, the normal is not set.
segment defines the begin and end point of the ray cast.
maxLambda a number typically in the range [0,1].
Returns:
true if there was an intersection.
float32 * lambda,
b2Vec2 * normal,
const b2Segment & segment,
float32 maxLambda
) const [pure virtual]
Perform a ray cast against this shape.
Parameters:
xf the shape world transform.
lambda returns the hit fraction. You can use this to compute the contact point p = (1 - lambda) * segment.p1 + lambda * segment.p2.
normal returns the normal at the contact point. If there is no intersection, the normal is not set.
segment defines the begin and end point of the ray cast.
maxLambda a number typically in the range [0,1].
Returns:
true if there was an intersection.
There seem to be a few people still having trouble using this so perhaps I should go into even more detail. Suppose you have a line starting at a point 'start' and going to a point 'end', and a shape called pShape. eg.
Code:
b2Vec2 start;
b2Vec2 end;
b2Shape* pShape;
b2Vec2 end;
b2Shape* pShape;
To use these you would do something like (this assumes the shape is attached to a body):
Code:
float lambda;
b2Vec2 normal;
b2Segment segment;
segment.p1 = start;
segment.p2 = end;
bool hit = pShape->TestSegment( pShape->GetBody()->GetXForm(), &lambda, &normal, segment, 1 );
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:
Code:
b2Vec2 intersectionPoint = (1 - lambda) * segment.p1 + lambda * segment.p2;