[Request] Better world query

Putting aside by syntactical squabbles earlier, it would be nice if we could query all the *bodies* in a certain area, rather than just the fixtures. I don't want body dupes. Also, it would be nice if it were *precise* (doesn't just look at the bounding boxes, but checks if the actual shape is inside or touches the AABB)

http://code.google.com/p/box2d/issues/detail?id=14
That's not better because not all people need something more than a simple aabb-aabb test. And don't expect people to rewrite Box2D just because you don't want/are too lazy to write a function that does more precise testing yourself.. 😉 You should also remember that it is a lot more expensive to test aabb vs shape than it is to test intersection of 2 AABBs.

P.S. The thing that would be useful (I didn't follow the code recently so maybe it's already implemented) is another Query function that adds only those bodies to the array that pass the filter test. The same test that is used to test whether two bodies collide.
I'm getting the impression that the main complaint is that it's hard to write these functions for yourself. That's what much of the recent changes help with. The templated Query method already makes it easier to filter the results. And separating shapes from their fixture properties makes/will make it easier to do general geometry tasks.
Yes, what the OP wants is entirely possible now with the current API. This request is ironic because this is the sort of request that drove me to make many changes leading to the current API.

darkzerox: If you find this difficult please request an example to be written. You can make a formal request in the issue tracker. Or maybe someone on this board will write an example for you.
I can write an example demonstrating this, but from what I've seen of the new API, it's still not possible to test shape-shape overlap easily. There are a few other "pure geometry" questions that it would be nice if Box2D could answer for free shapes as well as fixed ones, now that free shapes exist.

I'd really like to see the following code work:
Code:
bool f(b2AABB aabb,b2Shape* shape, b2Transform xf){
b2PolygonShape poly;
aabb.GetPolygonShape(poly);//Simple wrapper for SetAsBox
return shape->TestShape(xf, &poly, b2Identity);
}

I would need both of these functions before I could truly answer the OPs query without any real boiler plate, or any shape type switching.

(TestShape would virtual dispatch based on the two types, and call through to the b2Collide* set of functions, similar to how Box2D works internally, just without requiring a contact or fixture. I could produce a draft).

Still, in the interest of being useful, here is a completely untested STL example that gathers unique bodies, without precise testing:

Code:
struct UniqueBodies : public b2QueryCallback
{
  UniqueBodies(std::set<b2Body*>& bodies):bodies(bodies){}
  std::set<b2Body*>& bodies;
  void ReportFixture(b2Fixture* fixture)
  {
    bodies.insert(fixture->GetBody());
  }
}

void AddUniqueBodies(b2World* world, const b2AABB& aabb, std::set<b2Body*>& bodies)
{
  UniqueBodies callback(bodies);
  world->Query(&callback, aabb);
}


I'd say that's decently short, this is C++ not python.
@Boris: Ah silly me. I was thinking about adding all the bodies and then filtering out the dupes. I don't why it didn't occur to me to use a "set" or some sort of dictionary. That makes it much easier. I figured this was just inefficient and would be better implemented at the core of Box2D, which is why I asked.

@snake5: I didn't mean to replace the current Query function, just to add another one. I know it would be slower. And your arguments are a bit moot. Okay, maybe I'm being lazy, but you could also say, "write an entire physics library" with that line of reasoning. Anything is *possible* but if I have to write 200 lines to accomplish it... you start to wonder why you should bother using the API at all.

Anyway, it appears this is out of the question. The "unique bodies" request is actually easier to do myself than I thought as Boris pointed out... the precision thing... I guess I'll be having fun with that sometime down the road.
You can compare 2 shapes easily since you're working with convex shapes. To determine if 2 shapes share a common area, you have to do 2 checks. First is using a point that is inside a shape and call TestPoint on the other shape. If this returns true, you're done and the shapes share a common area. If it returns false, then the point is outside the other shape, but they might still intersect at another point. You then use the appropriate function (b2CollideCircles, b2CollidePolygonAndCircle or b2CollidePolygons) to check if the 2 shapes intersect. If they have at least 1 contact point, then they share a common area. If this check is false, the shapes are not intersecting and are not fully contained within one another, so they are fully separated.
Tiblanc wrote:
You can compare 2 shapes easily since you're working with convex shapes. To determine if 2 shapes share a common area, you have to do 2 checks. First is using a point that is inside a shape and call TestPoint on the other shape. If this returns true, you're done and the shapes share a common area. If it returns false, then the point is outside the other shape, but they might still intersect at another point. You then use the appropriate function (b2CollideCircles, b2CollidePolygonAndCircle or b2CollidePolygons) to check if the 2 shapes intersect. If they have at least 1 contact point, then they share a common area. If this check is false, the shapes are not intersecting and are not fully contained within one another, so they are fully separated.


CollidePolygons... of course, Box2D has functions for that too. Shouldn't be too bad then eh? Thanks for the tips guys, this isn't half as bad as I originally thought.

Wait... how exactly do I use this function?

Code:
void b2CollidePolygons(b2Manifold* manifold,
                 const b2PolygonShape* polyA, const b2Transform& xfA,
                 const b2PolygonShape* polyB, const b2Transform& xfB)


I'm assuming manifold gets set by the function, the shapes I can get from the fixtures, and the transforms... I get from the body?
I think I've figured it out... this solves both problems.

Code:
const QSet<Object*>& World::query(const QPointF& point, bool multiSelect) {
    return query(QRectF(point.x()-0.005,point.y()-0.005,0.01,0.01), multiSelect);
}

const QSet<Object*>& World::query(const QRectF& rect, bool multiSelect) {
    b2AABB aabb;
    aabb.upperBound = b2Vec2(rect.left(), rect.top());
    aabb.lowerBound = b2Vec2(rect.right(), rect.bottom());
    m_queriedObjects.clear();
    m_multiSelect = multiSelect;
    m_queryPoly.SetAsBox(aabb.GetExtents().x, aabb.GetExtents().y, aabb.GetCenter(), 0);
    m_world.QueryAABB(this, aabb);
    return m_queriedObjects;
}

bool World::ReportFixture(b2Fixture* f) {
    if(!m_multiSelect && !m_queriedObjects.isEmpty()) return false;
    b2Manifold manifold;
    b2Transform trans;
    trans.SetIdentity();
    b2CollidePolygons(&manifold, static_cast<b2PolygonShape*>(f->GetShape()),
            f->GetBody()->GetTransform(), &m_queryPoly, trans);
    if(manifold.m_pointCount > 0) {
        m_queriedObjects.insert(static_cast<Object*>(f->GetBody()->GetUserData()));
    }
    return true;
}
That looks right. I'm glad you guys worked through it.

I'm trying to keep Box2D fairly lean. Sure, I could add an API that does what you like. But then there may be many flavors of same functionality. I think it is more important to work on the aspects of Box2D that prevent users from doing standard things. Before recent changes it would have been difficult to perform such a query.

I will consider adding boolean overlap tests in the future. For now the current colliders work, but will be a little bit slower than necessary.

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