QueryAABB and call backs

I am hopeing someone could explain the code below. First of all, I never seen a function nested within another function so I was a little surprise there. But I just want to interpret the code below and if someone could correct me or add on then that will be great.


Code:
public function GetBodyAtMouse(includeStatic:Boolean = false):b2Body {
         // Make a small box.
         mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
         var aabb:b2AABB = new b2AABB();
         aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
         aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
         var body:b2Body = null;
         var fixture:b2Fixture;
         
         // Query the world for overlapping shapes.
         function GetBodyCallback(fixture:b2Fixture):Boolean
         {
            var shape:b2Shape = fixture.GetShape();
            if (fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic)
            {
               var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(), mousePVec);
               if (inside)
               {
                  body = fixture.GetBody();
                  return false;
               }
            }
            return true;
         }
         world.QueryAABB(GetBodyCallback, aabb);
         return body;
      }


First of all this code is meant to get a body located at the mouse position. So you can drag and drop or do what you like with the body. Any how, it first creates a new b2AABB method which I assume is a miniature version of the b2world except it has boundaries.

Quote:
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);


They then set a bounding box around the mouse position

Code:
world.QueryAABB(GetBodyCallback, aabb);


I am assuming by the comments that QueryAABB() finds any or possibly just 1 body that intersects with the bounding box area. Once it finds it, it calls on the callback function GetBodyCallback.

GetBodyCallback then evaluates this body and makes sure its not a static body such as a ground or wall. I am not sure what TestPoint does but if all goes well then it sets the reference of the body into a variable called body. and that is how they selected the body.

I already got mouse interaction with the body working with my code. As of right now I am just skimming the code and trying to interpret how they actually did it.
Nested functions happen once in a while... they follow the function-specific-variable rule...

I.e. don't bother trying to call GetBodyCallback from another function.

As you can see though includeStatic is passed to GetBodyAtMouse, *and* read from within GetBodyCallback, which can be super handy sometimes, but don't go overdoing it!

It's also a good way of handling recursion, i.e. where the nested function can call itsself over and over, with shared variables, and a little code before or after (in the parent function) that isn't called on every iteration.

In this case though, the world.QueryAAB function is being told... 'hey, find everything within the AABB, and instead of returning a list... execute a function elsewhere, with the body(s) as a parameter). Basically, you can pass a function as if it were a variable.

In previous versions it was done along the lines of...
var count:int = m_world.Query(aabb, shapes, k_maxCount);
where count was returned, aaab was passed in, 'shapes' was actually a reference which was altered on return, and k_max count was passed in...
It's not a great/straightforward way of doing things

As for the nested function's return statements... you could probably do a little more with that.
Testpoint just takes what was previously refered to as XForm (X,y vector and the rotation of the obj), and double checks the mouse coords fall within this position.

Hope this helps, didnt really proof read it 🙂

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