我们有两个灯具矢量(全部为一体)
//Create and add fixtures to vector1 and vector2
b2PolygonShape shape1;
shape1.SetAsEdge(from1,to1);
vector1.push_back( body1->CreateFixture(&shape1,0) );
b2PolygonShape shape2;
shape2.SetAsEdge(from2,to2);
vector2.push_back( body2->CreateFixture(&shape2,0) );
当case case1时,我们会在vector1中销毁第一个fixture。
body1->DestroyFixture( vector1[0] ); //destroy first fixture1
vector1.erase( vector1.begin() ); //remove from vector1
当case case2时,我们会在vector2中销毁第一个fixture。
body2->DestroyFixture( vector2[0] ); //destroy first fixture2
vector2.erase( vector2.begin() ); //remove from vector2
问题: 有时,当我们DestroyFixture(...)时,我们得到“断言失败:( fixture-> m_body == this)”
我查了一下,我知道vector1可以有一些fixture2,或者vector2有fixture1。为什么会这样? 我只将fixture1添加到vector1,将fixture2仅添加到vector2。
答案 0 :(得分:2)
如果您在碰撞回调函数中执行此操作,请不要这样做。 Box2D禁止在碰撞事件中移除物体/固定装置。相反,稍后标记主体以进行冲突后处理,例如在下一个更新方法调用中。