Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In Box2D, I was curious if it's possible to get a bounding box of a body already created in the world.

So basically, the Body is created, it's interacting with the world and such. And I needed that Body's bounding box. Is it possible?

share|improve this question

3 Answers 3

In Box2D, bodies don't have bounding boxes associated with them, fixture do. So you need to iterate over all the fixtures and generate a new AABB. Something like this:

b2AABB aabb;
aabb.lowerBound = b2Vec2(FLT_MAX,FLT_MAX);
aabb.upperBound = b2Vec2(-FLT_MAX,-FLT_MAX); 
b2Fixture* fixture = body->GetFixtureList();
while (fixture != NULL)
{
    aabb.Combine(aabb, fixture->GetAABB());
    fixture = fixture->GetNext();
}
share|improve this answer
    
thanks a lot man! This helped very much! –  user8070 Jun 16 '11 at 9:29
1  
Is this really correct. In the box2d I am using, fixture->GetAABB() does not exist, but a fixture->GetAABB(int32 childIndex) does. –  Jonny Apr 4 '13 at 1:27
    
are upperbound and lowerbound the topleft and downright corners ? –  jokoon May 10 '13 at 13:22

just using the fixture aabb also includes the shape radius - if you want to get the real aabb without the radius of the shape, do it like this:

b2AABB aabb;
b2Transform t;
t.SetIdentity();
aabb.lowerBound = b2Vec2(FLT_MAX,FLT_MAX);
aabb.upperBound = b2Vec2(-FLT_MAX,-FLT_MAX); 
b2Fixture* fixture = body->GetFixtureList();
while (fixture != nullptr) {
     const b2Shape *shape = fixture->GetShape();
     const int childCount = shape->GetChildCount();
     for (int child = 0; child < childCount; ++child) {
            const b2Vec2 r(shape->m_radius, shape->m_radius);
            b2AABB shapeAABB;
            shape->ComputeAABB(&shapeAABB, t, child);
            shapeAABB.lowerBound = shapeAABB.lowerBound + r;
            shapeAABB.upperBound = shapeAABB.upperBound - r;
            aabb.Combine(shapeAABB);
    }
    fixture = fixture->GetNext();
}
share|improve this answer
    
Why would you not want to get the shape radius? I originally copied the source from this answer, thinking it was more thorough than the other answers, but now I've found that it's not correct for my case, and am curious about which scenario this code should be used in. –  Mitch Jul 11 at 12:08
    
I see that this uses the newer Box2D API, though. That's one reason to prefer this answer. However, related to what I mentioned above, I had to comment out shapeAABB.lowerBound = shapeAABB.lowerBound + r; and shapeAABB.upperBound = shapeAABB.upperBound - r; to get the behaviour that I wanted. –  Mitch Jul 11 at 12:10

Really, a for loop is usually better for iteration. Taking @noel 's answer:

b2AABB aabb;
aabb.lowerBound = b2Vec2(FLT_MAX,FLT_MAX);
aabb.upperBound = b2Vec2(-FLT_MAX,-FLT_MAX); 
for (b2Fixture* fixture = body->GetFixtureList(); fixture; fixture = fixture->GetNext())
{
    aabb.Combine(aabb, fixture->GetAABB());
}

The expression fixture, taken as boolean, is, I understand, equivalent to fixture != NULL.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.