C++ Box2D-在运行时创建实体时,实体不会发生碰撞

C++ Box2D-在运行时创建实体时,实体不会发生碰撞,c++,box2d,C++,Box2d,我一直在开发一款具有可破坏环境的游戏,我提出了一个解决方案,在该解决方案中,我检查我的ContactListener对象中是否存在可能的破坏。显然,因为这是在步骤()中发生的,所以我将销毁处理推迟到该步骤之后。为此,我将需要在contact listener中处理的“销毁事件”汇集在一起,然后在Step()之后立即调用类似contactListener->processDestructionEvents()的函数 我这样做的方式是通过捕捉beginContact事件中的碰撞装置,然后确定碰撞角度

我一直在开发一款具有可破坏环境的游戏,我提出了一个解决方案,在该解决方案中,我检查我的
ContactListener
对象中是否存在可能的破坏。显然,因为这是在
步骤()中发生的,所以我将销毁处理推迟到该步骤之后。为此,我将需要在contact listener中处理的“销毁事件”汇集在一起,然后在
Step()
之后立即调用类似
contactListener->processDestructionEvents()的函数

我这样做的方式是通过捕捉beginContact事件中的碰撞装置,然后确定碰撞角度,然后使用该角度对装置本身进行光线投射。然后,我从夹具的
b2PolygonShape
绘制顶点,然后生成两个新形状,根据光线的影响点和退出点进行分割。在主体上销毁原始夹具,然后为第一个新形状生成新夹具并添加到原始主体。对于第二个形状,将生成一个新实体,并将该形状添加到此新实体

不管怎样,一切都很好,在调试视图中,我可以看到新的形状已经生成,并且都已经就位,正如它们应该的那样。然而,在这一点上,我的行为真的搞砸了。这一过程一旦完成,原始物体和新生成的物体都不会与任何物体发生碰撞。如果启用连续物理,有时动态对象会与这些实体/装置的某个边缘碰撞,但并不总是如此。我想知道我在运行时重建实体/装置的方法中是否做错了什么。以下是生成新对象的代码,如有任何帮助,将不胜感激

void PhysicsContactListener::processDestructionEvents() {
   if(!hasDestructionEvents) {return;}

   for(destructionEventsIterator = destructionEvents.begin(); destructionEventsIterator != destructionEvents.end(); ++destructionEventsIterator) {

      b2Filter f1, f2;
      f1.groupIndex = destructionEventsIterator->originalFixture->GetFilterData().groupIndex;
      f1.categoryBits = destructionEventsIterator->originalFixture->GetFilterData().categoryBits;
      f1.maskBits = destructionEventsIterator->originalFixture->GetFilterData().maskBits;

      f2.groupIndex = destructionEventsIterator->originalFixture->GetFilterData().groupIndex;
      f2.categoryBits = destructionEventsIterator->originalFixture->GetFilterData().categoryBits;
      f2.maskBits = destructionEventsIterator->originalFixture->GetFilterData().maskBits;

      b2PolygonShape newShape0 = destructionEventsIterator->newFixtures[0];

      b2FixtureDef fixture0Def;
      fixture0Def.shape = &newShape0;
      fixture0Def.density = 1.0f;
      fixture0Def.restitution = 0.2f;

      b2Fixture* fixture1 = destructionEventsIterator->hostBody->CreateFixture(&fixture0Def);
      fixture1->SetFilterData(f1);
      //destructionEventsIterator->hostBody->SetAwake(true);
      destructionEventsIterator->hostBody->ResetMassData();
      //destructionEventsIterator->hostBody->SetActive(true);
      destructionEventsIterator->hostBody->SetTransform(destructionEventsIterator->hostBody->GetPosition(), destructionEventsIterator->hostBody->GetAngle());

      b2BodyDef bd;
      bd.position = destructionEventsIterator->hostBody->GetPosition();
      bd.angle = destructionEventsIterator->hostBody->GetAngle();
      bd.type = destructionEventsIterator->hostBody->GetType();

      b2Body* newBody = destructionEventsIterator->hostBody->GetWorld()->CreateBody(&bd);
      b2PolygonShape* newShape1 = (b2PolygonShape*)(&destructionEventsIterator->newFixtures[1]);
      b2Fixture* fixture2 = newBody->CreateFixture(newShape1, destructionEventsIterator->hostBodyDensity);
      fixture2->SetFilterData(f2);
      newBody->SetAngularVelocity(destructionEventsIterator->hostBody->GetAngularVelocity());
      newBody->SetLinearVelocity(destructionEventsIterator->hostBody->GetLinearVelocity());
      //newBody->SetAwake(true);
      newBody->ResetMassData();
      //newBody->SetActive(true);
      newBody->SetTransform(destructionEventsIterator->hostBody->GetPosition(), destructionEventsIterator->hostBody->GetAngle());

      destructionEventsIterator->hostBody->DestroyFixture(destructionEventsIterator->originalFixture);

   }

这两块不会互相碰撞吗?看一看每个夹具最终得到的categoryBits和maskBits值-看起来每个工件都有相同的值。我猜您只是忽略了这样一个事实,即这些掩码是通过两种方式相互检查的,例如,从Box2D源代码:

bool collide =
      (filterA.maskBits & filterB.categoryBits) != 0 &&
      (filterA.categoryBits & filterB.maskBits) != 0;
另一方面,如果你的意思是这些碎片完全没有碰撞,只是从地上掉下来,直到永远,除了有时,那么我可能会怀疑一个不正确的多边形缠绕

顺便说一句,b2Filter只保存原语,因此您可以直接分配这些原语:

b2Filter f1 = destructionEventsIterator->originalFixture->GetFilterData();

…而且,第一个SetTransform和第二个ResetMassData是冗余的。

可以肯定的是,两个物体都不会与任何物体碰撞,这意味着它们会从地面坠落并坠入深渊。。。?或者这两个物体不会相互碰撞?没错,它们根本不会碰撞任何物体。如果我将它们设置为静态,则什么都没有。如果我把它们设置为动态,它们就会掉进深渊。谢谢,我会就此回复你的。我意识到我的示例中包含了垃圾代码,这几乎只是我随机尝试不同东西的复制和粘贴,因为我已经没有想法了,哈哈如果是坏的多边形缠绕,那不是错误吗?我以为box2D中有一个assert()语句在运行时的调试中检查了这一点。你是对的,非常感谢。这是一个棘手的问题。我把box2D论坛上一篇关于可破坏物体的旧文章中的代码移植到最新的box2D,在我使用的代码中,这些检查仅限于三角化物体。我用四头肌。解决方案是使用SVN中的box2D扩展byPolygon和分解VexandaddTo(poly,body,def);方法。再次感谢!