True Raycasts

From Box2D Wiki

This code is a patch to Box2DFlashAS3 to allow for efficient raycasting. Demo. This patch has now been merged into Box2D, so this page is no longer relevant. See Raycasting for a more up to date page.

Contents

Usage

Pretty simple, really. Either call b2World.Raycast in a similar manner to b2World.Query, or call b2World.RaycastOne in a similar way to b2Shape.TestSegment. The former is for finding multiple shapes, and the latter for the common case of just wanting one shape.

In both cases, there is a argument called userData. This is so that you can filter which shapes the ray can hit. By default, passing null means the ray can hit everything, while passing a shape means the ray can only hit things that shape can hit. You can customize it further by overriding the contact filter, which now has an extra method, b2ContactFilter.RayCollide.

(The reason it has this callback rather than making the user filter the results afterwards, is that the code needs to know when it has found valid shapes, so that it can terminate early if possible).

Download

Patch Testbed

License

Created by Adam Newgas (BorisTheBrave) under Box2D's standard license.

TOOLBOX
LANGUAGES