我正在尝试制作一种方法(destroyWeldJoint),从复制的集合中破坏焊缝。关节最初是在另一种方法 (createWeldJoint) 中创建的。我试图在另一种方法中破坏焊缝的原因是因为我在更新方法中调用 createWeldJoint 并且我想避免在枚举它时尝试更改集合。请看下面的代码:
创建焊接接头:
-(void) createWeldJoint {
// using the iterator pos over the set
std::set<BodyPair *>::iterator pos;
for(pos = bodiesForJoints.begin(); pos != bodiesForJoints.end(); ++pos)
{
    b2WeldJointDef      weldJointDef;
    BodyPair            *bodyPair = *pos;
    b2Body              *bodyA = bodyPair->bodyA;
    b2Body              *bodyB = bodyPair->bodyB;
    weldJointDef.Initialize(bodyA,
                            bodyB,
                            bodyA->GetWorldCenter());
    weldJointDef.collideConnected = false;
    weldJoint = (b2WeldJoint*) world->CreateJoint(&weldJointDef);
    // Free the structure we allocated earlier.
    free(bodyPair);
    // Remove the entry from the set.
    //bodiesForJoints.erase(pos);
}
// copy of bodiesForJoints
std::set<BodyPair *> bodiesForJointsCopy(bodiesForJoints.begin(), bodiesForJoints.end());
bodiesForJoints.clear();
}
破坏焊接接头:
- (void) destroyWeldJoint    {
std::set<BodyPair *>::iterator pos;
for(pos = bodiesForJointsCopy.begin(); pos != bodiesForJointsCopy.end(); ++pos)
{
    BodyPair            *bodyPair = *pos;
    b2Body              *bodyA = bodyPair->bodyA;
    b2Body              *bodyB = bodyPair->bodyB;
    // weldjoint should be destroyed here
    // Free the structure we allocated earlier.
    free(bodyPair);
}    
}
如何在 destroyWeldJoint 方法中销毁焊缝(在 createWeldJoint 中)?