C++ 从复制集销毁box2d关节
我正在尝试创建一个方法(destroyWeldJoint),用于从复制集销毁焊接接头。接头最初是用另一种方法(createWeldJoint)创建的。我在另一个方法中尝试销毁WeldJoint的原因是因为我在update方法中调用createWeldJoint,并且我希望避免在枚举集合时尝试更改集合。请参阅下面的代码: 创建焊接接头:C++ 从复制集销毁box2d关节,c++,cocos2d-iphone,box2d,box2d-iphone,C++,Cocos2d Iphone,Box2d,Box2d Iphone,我正在尝试创建一个方法(destroyWeldJoint),用于从复制集销毁焊接接头。接头最初是用另一种方法(createWeldJoint)创建的。我在另一个方法中尝试销毁WeldJoint的原因是因为我在update方法中调用createWeldJoint,并且我希望避免在枚举集合时尝试更改集合。请参阅下面的代码: 创建焊接接头: -(void) createWeldJoint { // using the iterator pos over the set std::set<Bod
-(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);
}
}
-(void)创建焊接接头{
//在集合上使用迭代器pos
std::set::iterator pos;
for(pos=bodiesForJoints.begin();pos!=bodiesForJoints.end();++pos)
{
b2WeldJointDef weldJointDef;
BodyPair*BodyPair=*位置;
b2Body*bodyA=bodyPair->bodyA;
b2Body*bodyB=bodyPair->bodyB;
weldJointDef.初始化(车身A,
bodyB,
bodyA->GetWorldCenter());
weldJointDef.collideConnected=false;
焊接接头=(b2WeldJoint*)世界->创建接头(&weldJointDef);
//释放我们先前分配的结构。
自由(体对);
//从集合中删除该项。
//接头的阀体。擦除(位置);
}
//接头阀体副本
std::set bodiesForJointsCopy(bodiesForJoints.begin(),bodiesForJoints.end());
用于关节的主体。清除();
}
焊接接头:
-(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);
}
}
-(无效)焊接接头{
std::set::iterator pos;
for(pos=bodiesForJointsCopy.begin();pos!=bodiesForJointsCopy.end();+pos)
{
BodyPair*BodyPair=*位置;
b2Body*bodyA=bodyPair->bodyA;
b2Body*bodyB=bodyPair->bodyB;
//焊接接头应该在这里销毁
//释放我们先前分配的结构。
自由(体对);
}
}
如何在destroyWeldJoint方法中销毁weldjoint(在createWeldJoint中)?@Nick:如何在destroyWeldJoint方法中销毁weldjoint(在createWeldJoint中)?执行此操作时,weldjoint=(b2WeldJoint*)world->CreateJoint(&weldJointDef);weldJoint是对关节的唯一引用吗?因为每次迭代都会覆盖它。@Nick:是的。但焊接接头由储存在车身中用于接头的车身对组成。这就是为什么我做了一个人体关节的复制品,我认为通过复制品破坏人体或其他东西会破坏关节。用于关节的车身被声明为std::set车身用于关节@尼克:我做了一个接触式监听器,当两个身体接触时,它会把这对身体进行监听,并将它们储存在身体的关节处。