No one actually requested this, but it looked easy enough, so I implemented true raycasts in Box2DAS3. You could do raycasts before with TestSegment, but it wasn't easy, and wasn't efficient for long rays. Well, no more, this new one uses Box2D's sweep and prune structure. Lemme know if you hate the calling conventions and names I've chosen, but at least I expect this code to be reasonably bug free.
I also fixed a bug in b2Circle.TestSegment.
Demo
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, b2ConcactFilter.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).
Updated: Argument solidShapes determines if rays can hit shapes that they start inside (previously, they couldn't). Fixing this nicely has changed b2Shape.TestSegment's return type to an enum, so I've left the old code up in patch form. I've also included Doxygen style comments and a helper function for infinite rays, b2Segment.Extend(Forward/Backward).
I also fixed a bug in b2Circle.TestSegment.
Demo
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, b2ConcactFilter.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).
Updated: Argument solidShapes determines if rays can hit shapes that they start inside (previously, they couldn't). Fixing this nicely has changed b2Shape.TestSegment's return type to an enum, so I've left the old code up in patch form. I've also included Doxygen style comments and a helper function for infinite rays, b2Segment.Extend(Forward/Backward).