Detecting bodies near a body

Hello,

What is the best method to detect bodies near a specific body ? (in a range of 20 pixels around this body, for example)

Do you advise me the use of shapes ? And how ? Can I use an invisible shape bigger than the shape of the body to detect nearest bodies ?

Thanks
b2World::QueryAABB() is probably what you're looking for.

Note that QueryAABB() returns all shapes whose AABBs overlap the query AABB (or at least that's my understanding), so you may have to perform an additional narrow-phase test if you need more detailed information than that.
Thanks, I've implemented the queryAABB with a callback function (in AS3) and it works. But I use the bounds for the moment.

Is it possible to check intersection between two shapes in the callback function ? (with one circle shape that is not associated directly with a fixture)
The C++ testbed has an example of this. It is called PolyShapes. It uses b2TestOverlap to test overlap precisely.
Thanks, it works like I want now.
I've talked too fast. Sometimes the ball seems to get balls beyond the range and other times, when there is a unique body in the range, this body is not detected.

Here's my code :
Code:
public function joinBodiesInRange( $body:b2Body, $radius:Number ) : void
{
   var aabbAnchor:b2AABB = new b2AABB( );
   aabbAnchor.lowerBound.Set( $body.GetPosition().x - $radius / PHYS_SCALE, $body.GetPosition().y - $radius / PHYS_SCALE );
   aabbAnchor.upperBound.Set( $body.GetPosition().x + $radius / PHYS_SCALE, $body.GetPosition().y + $radius / PHYS_SCALE );
   
   var circleShape:b2CircleShape = new b2CircleShape( $radius );
   circleShape.SetLocalPosition( $body.GetPosition() );
   
   function getBodyCallback( fixture:b2Fixture ) : Boolean
   {
      var body:b2Body = fixture.GetBody( );
      
      if ( body != $body )
      {
         var shape:b2Shape = fixture.GetShape( );
         if ( b2Shape.TestOverlap( circleShape, $body.GetTransform( ), shape, body.GetTransform( ) ) )
         {
            var distanceJoint:b2DistanceJointDef = new b2DistanceJointDef( );
            distanceJoint.Initialize( $body, body, $body.GetPosition( ), body.GetPosition( ) );
            _world.CreateJoint( distanceJoint );
         }
      }
      
      return true;
   }
   
   _world.QueryAABB( getBodyCallback, aabbAnchor );
}
Are you using PHYS_SCALE consistently? It seems the circle radius is not scaled.
Indeed, but strangely, when I scale the radius in the b2CircleShape, no body is detected (TestOverlap never returns true). It's not logical 😕

Is the use of the Transforms correct with TestOverlap ? And can I set shape's coordinates when the shape is not attached with a body ?
Ah, try setting the circle's local position to zero. It is getting double shifted in the overlap call.
Ok it's seems to work now. The radius should be not enough large too in the first tests.

However, I've always a problem when one body is in the range the ball. This body doesn't appear in the fixture. I call the function getBodiesInRange when the ball is colliding with a body. I'm looking where the bug is...

edit: fixed too 🙂

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