Does a shape intersect with an Axis Aligned box?

Hey guys.

Real simple question - what do I call to find out if my b2Body is intersecting at all a user-defined axis-aligned box?

Thanks,
Ian
This will roughly do the job:
Code:
bool bodyOverlapsAABB(b2Body* body, b2AABB &aabb)
{
    //need to make a b2Shape for the aabb...
    b2PolygonShape ps;
    b2Vec2 verts[4];
    verts[0].Set( aabb.lowerBound.x, aabb.lowerBound.y );
    verts[1].Set( aabb.upperBound.x, aabb.lowerBound.y );
    verts[2].Set( aabb.upperBound.x, aabb.upperBound.y );
    verts[3].Set( aabb.lowerBound.x, aabb.upperBound.y );
    ps.Set( verts, 4 );

    b2Transform identityTransform( b2Vec2(0,0), b2Rot(0) );

    for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) {
        for (int childNumber = 0; childNumber < f->GetShape()->GetChildCount(); childNumber++) {
            if ( b2TestOverlap(&ps, 0, f->GetShape(), childNumber, identityTransform, body->GetTransform()) )
                return true;
        }
    }

    return false;
}

I say 'roughly' because this overlap test is designed for use in collision detection, and it adds a small radius 'skin' around each shape to make them slightly larger. From b2Settings.h:
Code:
/// The radius of the polygon/edge shape skin. This should not be modified. Making
/// this smaller means polygons will have an insufficient buffer for continuous collision.
/// Making it larger may create artifacts for vertex collision.
#define b2_polygonRadius      (2.0f * b2_linearSlop)

With the default settings this value is 0.01, so if the size of the objects you are testing are in the order of 1 or more you could probably ignore it. If you need to test overlaps an a much smaller scale, shrinking the aabb by this radius would improve the accuracy somewhat but the 'skin' would remain on the other shape being checked. To remove this skin perfectly I think you would need to poke around the Box2D source and create a version of this function which ignores it.
Works perfectly, thank you 🙂
Hello there, I'm a newbie to processing, java and Box2d library.
I'm searching for some method to detect intersection between a box2d body and graphic shapes without a body. I tried with collision listening methods, but when you attach any shape to a body and fixture they simply can't overlap with each other, just collide. I tried to adapt some mouseover methods using Vec2 pos = box2d.getBodyPixelCoord(body); then calculating position overlap boolean but those attempts didn't work.
I have arrays of polygons falling from top, there's a user's silouette blob body interacting with polygons, All i need is to detect body objects that overlap a certain area of the screen (like a button) in pixel location, not the box2dworld.
I can't get to work the code posted here, since i'm not shure how to assign the values of the corner's pixel coordenates to it or where this code should be placed . Is there any complete pde example with setup, draw & a simple body polygon or box class to learn how this intersect function should be implemented to work?
thanks in advance

Nuria
You can set fixtures to be sensors if you don't want them to have any collision response.

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