More contributed code - True Raycasts

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).
Hey i wrote a reply lastnight... i guess i must hit preview by mistake.

Thats an impressive addition boris, im not sure what i could use raycasting for yet as its new to me but the demo seems to speak for itself!

Downloaded.

I also noticed you added another piece of code previously which was impressive as well. Would it be possible to create a sticky thread for 2.0.1 code like this? I think it would be beneficial to all flash users to know this extra functionality is available if required.
clark wrote:
I also noticed you added another piece of code previously which was impressive as well. Would it be possible to create a sticky thread for 2.0.1 code like this? I think it would be beneficial to all flash users to know this extra functionality is available if required.


Or the wiki. I've been meaning to add a wiki page for one of my add-ons, and there are a bunch of others that I think would be good to add as well (Boris' concave arc code, verso's CAD tool, ewjordan's stuff, etc.).

I wasn't sure where to link to it though. Maybe the front page should have another "box" section for add ons?
I prefer the idea of the wiki to stikies. Sticky topics very quickly get cluttersome, I think they're best for announcements. I've just bunged a contributed code link on the front page for the moment. The whole front page needs reworking, really, it's too cluttered. Why don't you each put up your own stuff in a page, in the format of mine (i.e. Usage, Download, License and putting it in Category:Contributed).

I have to agree, raycasts sound cool, but it is hard to think of uses for them. But I know there are people out there who want to do line of sight and lasers etc.
Yeah or the Wiki, i often forget theres a wiki!

I said once that i would help with the wiki and im know im stupid... but i dont get wikis yet when it comes to adding new things. Im sure i could take a stab at it but i dont want to create a cluttered mess of a wiki which will take more effort in the long run to clean up. I will honestly sit down one afternoon and take a proper look though.
clark wrote:
I said once that i would help with the wiki and im know im stupid... but i dont get wikis yet when it comes to adding new things. Im sure i could take a stab at it but i dont want to create a cluttered mess of a wiki which will take more effort in the long run to clean up. I will honestly sit down one afternoon and take a proper look though.


I think the opposite. It's better to write as much as you can on the wiki, and then worry about organization afterwards. The nice thing about wikis is you can do everything in bite size pieces. I failed when I tried to put together even a skeleton of a wiki at once, at the beginning, it's a suprising amount of work.
Any chance we could get the raycast to report a collision when the starting point is already inside of another shape? When I press the circle and the rectangle together in your demo, the line goes right through the circle. If raycasts were used for enemy line-of-sight (something I'm considering), we wouldn't want running into the face of the enemy to be a valid strategy for hiding. (Though it worked in the original GoldenEye!)
Ah, you're right. It's inherited that behaviour from TestSegment.

Programmatically, it's easy to resolve, but I'm a little hesitant. TestSegment doesn't generate a hit because it returns normal data, which otherwise wouldn't make any sense, and RaycastOne would suffer the same problem.

I can see various ways of changing the API, but nothing which would be non-breaking. How about if TestSegment returned an enum, according to hit, starts-inside or miss?

As a hack fix, (as is my style), you can replace b2World.RaycastSortKey with the following, which works, but is slower than it could be as it relies on TestPoint to give the extra info TestSegment isn't returning.
Code:
   public function RaycastSortKey(shape:b2Shape){
      if(m_contactFilter && !m_contactFilter.RayCollide(m_raycastUserData,shape))
         return -1;
      
      var body:b2Body = shape.GetBody();
      var xf:b2XForm = body.GetXForm();
      var lambda:Array = [0];
      if(!shape.TestSegment(xf, lambda, m_raycastNormal, m_raycastSegment, 1)){
         if(shape.TestPoint(xf,m_raycastSegment.p1))
            return 0;
         return -1;
      }
      return lambda[0];
   }

Caveat: RaycastOne won't set lambda or normal in that case, but it will return the correct shape. There's a one line fix for that, but I don't have space in this margin 🙂

The "other", more correct fix would be to shoot your line of sight rays from a bit inside your main guy, so that they always start outside other bodies. But I can see that for LoS you don't really care about surface normals, or even distance.
Hmm. For now, starting inside the originator is probably a good solution, thanks.
Ok, patched to work a bit more smoothly now.
TestSegment returns an enum, and Raycast(One) has a boolean toggling the described behaviour. When solidShapes is true and a ray starts in a shape, then the normal is left unset and lambda is set to 0.

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