Why is there a b2QueryCallback class?

Was this really necessary? It has a single function, why not make b2World::Query accept a function as the first argument?

Now I have something like this in my code:

Code:
      b2AABB aabb;
      aabb.lowerBound = b2Vec2(mousePos.x()-eps, mousePos.y()-eps);
      aabb.upperBound = b2Vec2(mousePos.x()+eps, mousePos.y()+eps);
      physWorld->Query(new FixtureQuery, aabb);


I'm creating two objects for one little query. Don't get me wrong, I love Box2D, but it seems to be getting less and less user friendly.
Your question is a broader one. The question is whether we should use callback functions or callback classes.

With a callback function you will need to store the results somewhere. So either you use some globals or you also pass the query a results structure via a void*.

With a callback class you pass a class that is basically a merging of the callback function and the results structure.

So you have either:
Code:
void CallbackFunction(args);
struct Results
{
... results data ...
};


or

Code:
class CallbackClass : public AbstractCallback
{
  void CallbackFunction(args);
  ... results data ...
};


I chose the latter approach because it is more of a C++ style.

BTW, you should not use new in your argument list. It will cause a leak. Instead you should instantiate your callback class on the stack.

Sometimes the Box2D API becomes more complex in some ways. You have to try to appreciate that there is a constant tug of war between features, ease of use, memory savings, and performance. Sometimes an improvement in one area causes a regression in another area. You have to also try to appreciate that people value some areas more than others.

The introduction of the callback class for the queries is purely for memory savings. You may not want to store a huge buffer of fixtures when you are only interested in a few. With a callback class, I am able to let the user decide on memory consumption.
Fair enough.

I realized there as a leak a minute later.

It's just that the data I want to extract from the callback doesn't really belong in some sort of fixture callback class, it belongs somewhere else so I'd have to transfer it anyway. I suppose as long as there is a method to your madness 😉
The C++ callback is not my invention and this pattern is used in other libraries and is discussed in books on C++ programming.

I tend to avoid putting any experimental C++ programming models in Box2D.
Callback objects are strictly more general too. If you are used to c-style callbacks, you can make yourself a wrapper class, once, and use it all over the place.
world.Query(QueryWrapper(myFun, myUserData), aabb);

Something like:

Code:
struct QueryWrapper : public b2QueryCallback
{
  typedef bool (*CallbackFunction)(b2Fixture*, void*);

  CallbackFunction cb;
  void* userData;

  QueryWrapper(CallbackFunction cb, void* userData = 0):
    cb(cb), userData(userData)
  {}

  virtual ~QueryWrapper(){}
  virtual bool ReportFixture(b2Fixture* fixture)
  {
    return cb(fixture, userData);
  }

}


I'd recommend a similar general approach for most applications. E.g. a callback to populate an STL set with the bodies involved can probably be re-used all over the place.

Edit: Ugh, const correctness will stop this. You'd need:
QueryWrapper w(myFun, myUserData)
world.Query(w, aabb);
I think structs are reserved for C-style code :p

In any case, I do like this C++ query callback method.. a lot better than I liked the old query with the list!

-P

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