【问题标题】:Collision detection Fail in Box2dBox2d中的碰撞检测失败
【发布时间】:2013-02-11 19:21:04
【问题描述】:

我想检测物体之间的碰撞,一个物体是圆形,30多个物体是凸体。也许问题是因为检测到圆和凸之间的碰撞?求大神帮忙,2天没找到答案。。。 我有 3 个类:PlayerConctactListenerlevel1(我在其中创建多边形)。

Player 中我设置了类型 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!");
    }
}

level1中,我创建了静态多边形,应该是玩家应该接触的地面。

    - (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

【问题讨论】:

  • 你如何检测碰撞?什么是不工作?提供意外行为的代码和描述。
  • 我编辑了答案,在 ContactListener 中似乎一切正常,也许我在 -(void) drawStaticPolygons 中做了一些事情。当玩家从静态多边形上跳下来时什么都不会发生,如果他着陆也什么都不会发生。类 Player 使用类型 kGameObjectPlayer 初始化,我通过 userData 设置类型为 kGameObjectGround 的多边形。

标签: ios listener box2d collision-detection contact


【解决方案1】:

我看到的问题是,因为我没有检查其他东西多边形形状确实必须是 CCW 并且 vertices 看起来不像是遵循这个规则。以逆时针顺序制作顶点并且应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多