我最近在一个长期运行的项目中将 box2d 的版本升级到 v2.2.1,这导致了现有项目代码的一些向后兼容性问题。大多数都已解决,除了这个
b2Fixture *f = body->GetFixtureList();
b2RayCastOutput output;
b2RayCastInput input;
f->RayCast(&output, input) // broken call
现在坏了,期待第三个参数。我在box2d源代码中看到函数签名是
inline bool b2Fixture::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, int32 childIndex) const;
但我找不到任何childIndex
应该是什么的例子。有人可以举例说明如何使用这个更新的 RayCast 函数吗?
编辑:我注意到将 childIndex
设置为 0 似乎可行,但我不知道为什么。
最佳答案
此参数仅与 b2ChainShape 灯具相关。对于其他形状类型,它只是为了符合虚函数签名。
链形的功能实际上是由多个 b2EdgeShapes 完成的,链形本身可以被认为是组织这些边形“ child ”的助手。它分配内存,为您设置幻影顶点,并将诸如碰撞检查之类的事情委托(delegate)给边缘形状。
如果您不对链形状转换光线,您可以将其保留为零。如果是,则可以使用 b2ChainShape 的这些函数将光线转换到每个子边上:
int32 GetChildCount() const;
void GetChildEdge(b2EdgeShape* edge, int32 index) const;
第二个用法如下:
b2EdgeShape edgeShape;
chainShape->GetChildEdge(&edgeShape, 123);
您需要先将形状转换为 b2ChainShape*:
if ( e_chain == fixture->GetType() ) {
b2ChainShape* chainShape = (b2ChainShape*)fixture->GetShape();
....
}
...使用b2World的RayCast功能会更简单高效:)
关于iphone - box2d raycast 向后兼容性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385100/