Cocos2d iphone box2d车身和车架之间缺少碰撞

Cocos2d iphone box2d车身和车架之间缺少碰撞,cocos2d-iphone,box2d,cocos2d-x,Cocos2d Iphone,Box2d,Cocos2d X,我将cocos2d-x2.0.3与Box2D2.2.1结合使用,并在第一个示例中介绍了一个动态实体(使用PhysicsEditor定义的船)。施加重力时,预期的行为是船与车架碰撞,但它会通过 以下是世界和机构的定义: // create world b2Vec2 gravity; gravity.Set(0.1f, -0.0f); bool doSleep = true; world = new b2World(gravity); world->SetAll

我将cocos2d-x2.0.3与Box2D2.2.1结合使用,并在第一个示例中介绍了一个动态实体(使用PhysicsEditor定义的船)。施加重力时,预期的行为是船与车架碰撞,但它会通过

以下是世界和机构的定义:

      // create world
  b2Vec2 gravity;
  gravity.Set(0.1f, -0.0f);
  bool doSleep = true;
  world = new b2World(gravity);
  world->SetAllowSleeping(doSleep);
  world->SetContinuousPhysics(true);
  // Debug Draw functions
  m_debugDraw = new GLESDebugDraw( PTM_RATIO * CCDirector::sharedDirector()->getContentScaleFactor());
  world->SetDebugDraw(m_debugDraw);
  uint32 flags = 0;
  flags += b2Draw::e_shapeBit;
  flags += b2Draw::e_jointBit;
  flags += b2Draw::e_aabbBit;
  flags += b2Draw::e_pairBit;
  flags += b2Draw::e_centerOfMassBit;
  m_debugDraw->SetFlags(flags);

  // for the screenBorder body we'll need these values
  CCSize screenSize = size;
  float widthInMeters = screenSize.width / PTM_RATIO;
  float heightInMeters = screenSize.height / PTM_RATIO;
  b2Vec2 lowerLeftCorner = b2Vec2(- widthInMeters / 2, 0.0);
  b2Vec2 lowerRightCorner = b2Vec2(widthInMeters / 2, 0.0);
  b2Vec2 upperLeftCorner = b2Vec2(- widthInMeters / 2, heightInMeters);
  b2Vec2 upperRightCorner = b2Vec2(widthInMeters/2, heightInMeters);

  // Define the static container body, which will provide the collisions at screen borders.
  b2BodyDef screenBorderDef;
  screenBorderDef.position.Set(0, 0);
  screenBorderDef.type = b2_staticBody;
  b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
  b2EdgeShape screenBorderShape;

  // Create fixtures for the four borders (the border shape is re-used)
  screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
  screenBorderBody->CreateFixture(&screenBorderShape, 0);
  screenBorderShape.Set(lowerRightCorner, upperRightCorner);
  screenBorderBody->CreateFixture(&screenBorderShape, 0);
  screenBorderShape.Set(upperRightCorner, upperLeftCorner);
  screenBorderBody->CreateFixture(&screenBorderShape, 0);
  screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
  screenBorderBody->CreateFixture(&screenBorderShape, 0);

  // add sail boat sprite
  CCSprite *sailBoatSprite = CCSprite::create("SailBoat.png");
  addChild(sailBoatSprite, -1, kSailingBoat);
  sailBoatSprite->setAnchorPoint(ccp(0.5, 0.5));
  sailBoatSprite->setPosition(ccp(0.0, 128.0));

  // add sail boat body
  GB2ShapeCache *shapeCache = GB2ShapeCache::sharedGB2ShapeCache();
  shapeCache->addShapesWithFile("SailBoat.plist");
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0.0/PTM_RATIO, 128.0/PTM_RATIO);
    bodyDef.userData = sailBoatSprite;
    b2Body *body = world->CreateBody(&bodyDef);
  shapeCache->addFixturesToBody(body, "SailBoat");
  sailBoatSprite->setAnchorPoint(GB2ShapeCache::sharedGB2ShapeCache()->anchorPointForShape("SailBoat"));
  this->schedule(schedule_selector(SailingFieldNode::tick));
以下是更新方法:

void SailingFieldNode::tick(float dt)
{
    int32 velocityIterations = 8;
    int32 positionIterations = 1;
    world->Step(0.01, velocityIterations, positionIterations);
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL) {
            CCSprite *myActor = (CCSprite*)b->GetUserData();
            myActor->setPosition(ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO));
            myActor->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
        }
    }
}

void SailingFieldNode::draw()
{
  CCLayer::draw();
  ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);
  kmGLPushMatrix();
  world->DrawDebugData();
  kmGLPopMatrix();
  CHECK_GL_ERROR_DEBUG();
}
正如您在下面的屏幕截图中看到的,船在画面中,那么应该发生碰撞:

有什么帮助吗

提前谢谢


Jean

问题解决了,问题是在PhysicsEditor中,我没有在类别中设置动态实体(带有标志)。在我的代码中,我还没有使用它们,但您可能必须将其设置在一个类别中,以进行碰撞(我已在bit_0中设置了我的身体)