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.