Sensors all over the world

Is it efficient to completely cover the world with sensors ?

Basically I want to partition the world in zones, and be notified when objects change zone (and know which zone they are entering).

One notable thing is that there would be one big zone (more than 50% of the world's surface), so if I should not make a sensor that big, I would love suggestions about how to determine which zone an object is in (with the less geometry checks on my side, and in the most efficient way as possible, of course).
A sensor is basically a body and Box2D's documentation says:
Quote:
Keep the size of moving objects roughly between 0.1 and 10 meters.

Your areas might not move, but there is a (probably) better and native way as the documentation says:
Quote:
AABB Queries
Sometimes you want to determine all the shapes in a region. [...]

Try this (the following code is C++ but should easily be portable to every other language Box2D is ported for):
Code:
class RegionQueryCallback : public b2QueryCallback
{
public:
    bool ReportFixture(b2Fixture* fixture)
    {
        // Add the body within this area into the list.
        b2Body* body = fixture->GetBody();
        bodies.push_back(body);

        // Return true to continue the query.
        return true;
    }
    list<b2Body*> getBodies() { return this->bodies; }
private:
    list<b2Body*> bodies;
};

[...]

RegionQueryCallback callback;
b2AABB aabb;
// Set the bounding box for the region.
aabb.lowerBound.Set(-1.0f, -1.0f);
aabb.upperBound.Set(1.0f, 1.0f);
myWorld->Query(&callback, aabb);
list<b2Body*> bodies = callback.getBodies();
for (list<b2Body*>::iterator it = bodies.begin(); it != bodies.end(); ++it) {
    b2Body *body = *it;
    // Do something with the body which is within the area.
}


For more information about AABB queries take a look into the documentation: http://www.box2d.org/manual.html#_Toc258082976
Thanks for the help but my "zones" do not move, so they can be static bodies. I kind of need a confirmation that the size limitation does not impact those.

I also would like to avoid iterating over all existing bodies, if a better method exists.
Why not just have thin sensors at the boundaries of the zones like 'doors'. You would just have to check which side of the door an object went out each time they EndContact with the door sensor.
irresistible force wrote:
Why not just have thin sensors at the boundaries of the zones like 'doors'. You would just have to check which side of the door an object went out each time they EndContact with the door sensor.


Thanks for the tip, that looks so simple ! I could even use some of the other existing sensors as doors.
I'm not implementing EndContact yet, but I'll think about this solution as soon as my big sensor starts showing performance problems.

Just one thing: is there an easy built-in way of checking which side of a rectangle (or bounding box) a point (or object) is ?
I'm trying the solution with EndContact and "doors".
I have a few questions about that:
- how wide do I have to make the doors, in order not to miss any event ? my moving objects detection shapes are circles of radius 0.002, and not bullets
- could BeginContact and EndContact be called in the same step for the same pair or fixtures ?
- it seems that when I destroy a body, EndContact is called (if this body had contacts). Is there an easy and clean way to ignore those and only care about the ones from the step simulation ?

Thanks
- I guess the doors will need to be thick enough so that your objects can't 'tunnel' right through them in one timestep.
- no
- if you set something in the body's user data before you destroy it, I expect you could use that in EndContact to know whether it was being destroyed (I have not checked this though). Otherwise a global variable to track whether you are currently inside the step or not would work.

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