Floating/Flying and Movement

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:
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.

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;

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 );

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.
Image

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;
Great info! Do you have time to put this on the wiki?
Yeah, that's really helpful. Thanks! 🙂
Erin Catto wrote:
Great info! Do you have time to put this on the wiki?
Yep, I put it in the 'How do I use' section of the general faq.
This is pretty neat. I understand the line intersection thing, but once you have the intersection point, how do you determine how much force to apply to the object?
Excellent post.

Does TestSegment give the closest intersection if there are multiple ones? In the above example, if the maxLambda was 2, can we be assured that it will set lambda to 0.76 rather than the value at the other end?
Yes, it will give you the closest found intersection.
HI
Sorry for pushing this topic but I have a question about calculating point of intersection.

Wiki says:
Code:
b2Vec2 intersectionPoint = (1 - lambda) * segment.p1 + lambda * segment.p2;


I think that better is to write:
Code:
b2Vec2 intersectionPoint = segment.p1 + lambda * (segment.p2 - segment.p1);

because it clearly shows that intersectionPoint is starting_point + lambda * distance_between_start_and_end

Can someone explain me the idea of approach posted on the wiki? I can't imagine any geometric explanation.
max wrote:
Can someone explain me the idea of approach posted on the wiki? I can't imagine any geometric explanation.


I believe if you do a little bit of vector algebra you'll conclude that they are the same thing. I'm not sure which would ensure faster execution (not that it would matter much since this is such a fast operation) but other than that they do the same exact thing.

If you really want to visualize it, the first expression is linear interpolation between the two points. It should be apparent that if lambda = 1.0, the point will be segment.p2, if lambda = 0.0, it would be segment.p1. If lambda > 1.0 it's sort of analogous to linearly extrapolating the value based on p2 relative to p1...
dc443 wrote:
I believe if you do a little bit of vector algebra you'll conclude that they are the same thing.

Yes, I know. I was only curious about the difference in interpretation of both methods.

dc443 wrote:
If you really want to visualize it, the first expression is linear interpolation between the two points.

And that was the point of my question, thanks.

Original thread: https://box2d.org/forum/viewtopic.php?t=1053