Games How do I distinguish between different "sources" of Callback?


Active Member
Licensed User
The Box2d Raycast do have the ability to set the name of the CallBack procedure.

void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2);

in XUI2D only the World do have the .RayCast member. Means the World_RaycastCallback is getting always the answer to the question.

How do I distinguish between different "sources" in a single callback procedure than? Sure, the source is always the world object, but may it is requested be different procedures with different useage of the answer. like 2 different chatacters at unknown times. Who ask the question answered in the callback some more efficient than setting a special boolean IF THEN structure?

If the boolean structure is the single way how we make sure that the set boolean was really the one wihch ask the world for the RayCast result? I mean this an async process. So we should know/check and not assume the the last answer was the right answer to the last question the world, am I right?
Last edited:


B4X founder
Staff member
Licensed User
The Sleep(0) in the walking character example is not needed. I've removed it.
The code is:
Private Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
   If Action = Panel1.TOUCH_ACTION_DOWN Then
       Dim p As B2Vec2 = X2.ScreenPointToWorld(X, Y)
       'cast a ray from the kid to the point
       RaycastBody = Null
       RaycastPoint = p
       world.RayCast(, p)
       If RaycastBody <> Null And RaycastBody.IsDeleted = False Then
           'ray hit a body
           RaycastBody.TimeToLiveMs = 1
       End If
   End If
End Sub

Private Sub World_RaycastCallback (Fixture As B2Fixture, Point As B2Vec2, Normal As B2Vec2, Fraction As Float) As Float
   Dim bw As X2BodyWrapper = Fixture.Body.Tag
   'ignore static bodies
   If bw.Body.BodyType = bw.Body.TYPE_STATIC Then Return -1
   RaycastBody = bw
   RaycastPoint = Point.CreateCopy
   'return fraction to limit the ray up to the current fixture.
   'The result is that the last event will be the closest body.
   Return Fraction
End Sub

The raycast callback happens synchronously. This means that it starts when you call world.Raycast and ends on the next line.


Active Member
Licensed User
I would be grate if the listener of the Callback would be a variavle as it in the original. The code is more structured than.

Just assume that one Raycast Survey is for Static Bodys only the other for Dynamic Bodys only. Then the RayCast-CallBack reduces to just handing over the local variables to gobal variables (RaycastBody, RaycastPoint, etc) and the further processing must be done in the following lines of code of the World.Raycast calling.