2013-01-05 1 views
0

저는 Box2D 2.2.1과 함께 cocos2d-x 2.0.3을 사용하고 있으며 첫 번째 예가 프레임에 동적 본문 (PhysicsEditor로 정의 된 보트)을 도입했습니다. 중력을 적용하면 보트가 프레임과 충돌하지만 대신 통과합니다.box2d 본문과 프레임 간의 충돌 누락

다음
 // 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(); 
} 

당신이 보트가 프레임에있는 다음 스크린 샷에서 볼 수 있듯이 다음이해야 충돌 수 : screenshot

어떤 도움이 필요합니까? 사전에

감사합니다.

답변

0

문제가 해결의 문제가 PhysicsEditor에서 I는 (플래그)와 범주에 동적 본체를 설정하지 않은 것이었다. 내 코드에서는 아직 사용하지 않지만 아마도 충돌을 일으킬 수있는 카테고리를 설정해야합니다 (저는 bit_0에서 내 몸을 설정했습니다).