2013-02-11 170 views
0

我想檢測身體之間的碰撞,一個身體有圓形,30+有凸體。也許問題是因爲檢測到了圓和凸之間的碰撞?請幫忙,找不到答案2天... 我有3個班級:玩家,ConctactListener級別1(我在哪裏創建多邊形)。碰撞檢測失敗Box2d

球員我定了型kGameObjectPlayer

- (id) init { 
     if ((self = [super init])) { 
      type = kGameObjectPlayer; 
     } 
     return self; 
    } 
-(void) createBox2dObject:(b2World*)world { 


    b2BodyDef playerBodyDef; 
    playerBodyDef.type = b2_dynamicBody; 

    playerBodyDef.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO); 
    playerBodyDef.userData = self; 
    playerBodyDef.fixedRotation = true; 

    body = world->CreateBody(&playerBodyDef); 

    b2CircleShape circleShape; 
    circleShape.m_radius = 0.7; 
    b2FixtureDef fixtureDef; 
    fixtureDef.shape = &circleShape; 
    fixtureDef.density = 1.0f; 
    fixtureDef.friction = 1.0f; 
    fixtureDef.restitution = 0.0f; 
    body->CreateFixture(&fixtureDef); 
} 

ContactListener

void ContactListener::BeginContact(b2Contact *contact) { 
    GameObject *o1 = (GameObject*)contact->GetFixtureA()->GetBody()->GetUserData(); 
    GameObject *o2 = (GameObject*)contact->GetFixtureB()->GetBody()->GetUserData(); 

    if (IS_PLATFORM(o1, o2) && IS_PLAYER(o1, o2)) { 
     CCLOG(@"-----> Player made contact with platform!"); 
    } 
} 

void ContactListener::EndContact(b2Contact *contact) { 
    GameObject *o1 = (GameObject*)contact->GetFixtureA()->GetBody()->GetUserData(); 
    GameObject *o2 = (GameObject*)contact->GetFixtureB()->GetBody()->GetUserData(); 

    if (IS_PLATFORM(o1, o2) && IS_PLAYER(o1, o2)) { 
     CCLOG(@"-----> Player lost contact with platform!"); 
    } 
} 

而且在1級我創建靜態polygones這應該是一個與哪位玩家對陣d聯繫。

- (void) drawStaticPolygons 
    { 
     GameObject *ground = [[GameObject alloc] init]; 
     [ground setType:kGameObjectGround]; 

     //1st polygon 
     b2Vec2 vertices1[4]; 
     vertices1[0].Set(0, 1); 
     vertices1[1].Set(0, 0); 
     vertices1[2].Set(16, 0); 
     vertices1[3].Set(16, 1); 

     b2BodyDef myBodyDef1; 
     myBodyDef1.type = b2_staticBody; 
     myBodyDef1.userData = ground; 

     b2PolygonShape polygonShape1; 
     polygonShape1.Set(vertices1, 4); 

     b2FixtureDef myFixtureDef1; 
     myFixtureDef1.shape = &polygonShape1; //change the shape of the fixture 
     myBodyDef1.position.Set(0,0); 
     b2Body *staticBody1 = world->CreateBody(&myBodyDef1); 
     staticBody1->CreateFixture(&myFixtureDef1); //add a fixture to the body 

     //2nd polygon 
     .... 
     //n polygon 
    } 

的問題是如何使ContactListener知道我的多邊形kGameObjectGround

+0

你如何檢測碰撞?什麼是不工作?提供代碼和意外行爲的描述。 – Pavel 2013-02-11 19:56:20

+0

我編輯了答案,在ContactListener中似乎沒有問題,也許我在 - (void)drawStaticPolygons中做了些什麼。當玩家跳下靜態多邊形時沒有任何反應,如果他降落沒有任何反應。 類播放器使用類型kGameObjectPlayer初始化爲多邊形,我通過userData設置類型kGameObjectGround。 – user1974123 2013-02-11 21:15:08

回答

0

我看到的問題,因爲我沒有檢查其他的東西,就是多邊形也必須CCW頂點鴕鳥政策樣子是以下這條規則。使頂點逆時針順序,應該工作。