Raycasting

From Box2D Wiki

Raycasting is a method of determining what shapes intersect a given segment, and where on those shapes the line, known as a ray, hits. It is similar in many ways, to using TestSegment, but allows testing of multiple shapes. Box2D rays are not infinitely extended, contrary to convention, so are basically identical to segments. As the world is bounded, this presents little difficulties.

Contents

Usage

The following two functions provide raycast functionality.

int32 b2World::Raycast(      const b2Segment& segment,
                             b2Shape** shapes,
                             int32 maxCount,
                             bool solidShapes,
                             void* userData);
 
b2Shape* b2World::RaycastOne(const b2Segment& segment,
                             float32* lambda,
                             b2Vec2* normal,
                             bool solidShapes,
                             void* userData);

Raycast provides a general case ray, allowing you to determine the first n shapes that it hits, while RaycastOne is for the specific case of finding only the first collision, but provides more detailed information.

In general, one passes a b2Segment called segment in describing the ray to be tested. This behaves identically to the same argument in TestSegment. The bool solidShapes determines whether the ray will stop immediately if it starts inside a shape, or not. And the userData field is used for filter shapes from the ray. Pass null to userData to ignore this.

Raycast, like b2World.Query, fills up an array of shapes of capacity maxCount. It returns the number of shapes found. You will need to call TestSegment to determine where on each shape the ray hits.

RaycastOne, like TestSegment, provides lambda and normal values as extra information. It returns null if there is no collision, otherwise, the shape collided with.

Example

b2Segment segment;
b2Vec2 start(5.0f,0.0f);
b2Vec2 direction(1.0f,0.0f);
//Set to world width + world height to be sure of infinite ray
float32 maxLength = 10000;
segment.p1 = start
segment.p2 = start + maxLength * direction;
 
float32 lambda=1;
b2Vec2 normal;
b2Shape* shape = m_world->RaycastOne(segment,&lambda,&normal,false,NULL);
if(shape){
	b2Vec2 collisionPoint = (1 - lambda) * segment.p1 + lambda * segment.p2;
	// Do something...
}

There is a further example in the Testbed.

Filtering Shapes

Sometimes you only want to check if the ray hits certain shapes, for example ignoring sensors. But RaycastOne only returns the first shape, which might not be the one you want. The solution, is filtering which shape the ray sees. This works similarly to collision filtering.

By default, passing null as the userData field disables filtering, or you can pass any shape pointer to have the ray filtered in the same way that that shape would be.

If this doesn't give you enough control, you can override b2ContactFilter::RayCollide just as in collision filtering to provide arbitrary control.

Internally

Internal Information

The following text is interesting in case you want to know how Box2D behaves internally, it is not required to use the API.

Box2D uses its broadphase data to step through shapes that have their AABB intersecting the ray. When it finds one which passes TestSegment, and any filtering, it adds it to the output array, sorted by it's lambda value. This allows raycasts to return very quickly when they are only looking for the first few collisions.

TOOLBOX
LANGUAGES