Box2D Get Bounding Box of a Body

转载自:http://gamedev.stackexchange.com/questions/1366/box2d-get-bounding-box-of-a-body

问题:

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?

答案:

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();
}

posted on 2012-04-22 15:52  yang3wei  阅读(288)  评论(0编辑  收藏  举报