Answers:
在Box2D中,实体没有与之关联的边界框,夹具则没有。因此,您需要遍历所有灯具并生成新的AABB。像这样:
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();
}
仅使用夹具aabb时还包括形状半径-如果要获得没有形状半径的真实aabb,请按照以下步骤进行操作:
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();
}
shapeAABB.lowerBound = shapeAABB.lowerBound + r;
并shapeAABB.upperBound = shapeAABB.upperBound - r;
得到我想要的行为。
实际上,for循环通常更适合迭代。以@noel的答案:
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());
}
据fixture
我了解,以布尔值表示的表达式等效于fixture != NULL
。
fixture->GetAABB()
不存在,但是存在fixture->GetAABB(int32 childIndex)
。