Cocos2dx Box2D - b2body GetUserData始终返回null

时间:2014-07-09 08:58:14

标签: box2d cocos2d-x collision-detection

我有一辆车和一辆自行车用品。在碰撞时。我需要得到哪个(汽车或自行车)与墙碰撞。因为我正在使用body-> getuserdata。但它总是返回null。我很困惑,为什么我会变空。我错了吗?

我引用这个link,但和我一样。没有用。

汽车代码: -

sprintf(temp,CAR_BODY_CAR_PLIST);
m_Texture[2] = new TextureObject(temp,spritesheet1,1,true,kTexture2DPixelFormat_Default,2);  

 // add cart //
        b2BodyDef bodyDef;
        b2FixtureDef fixtureDef;
        b2PolygonShape polygonShape;
        b2CircleShape circleShape;
        bodyDef.position.Set(startPos.x,startPos.y);
        bodyDef.type = b2_dynamicBody;
        **bodyDef.userData = m_Texture[2];**

        cart = m_world->CreateBody(&bodyDef);

        fixtureDef.density = 25.0f;
        fixtureDef.friction = 0.3f;
        fixtureDef.restitution = 0.1f;
        fixtureDef.filter.categoryBits = 0x0008;
        fixtureDef.filter.maskBits = 0x0003;
        fixtureDef.density = 0.0f;
        fixtureDef.isSensor = true;
        b2Vec2 points[6] = 
        {

            b2Vec2(0.7f,0.0f),
            b2Vec2(0.7f,3.25f),
            b2Vec2(-0.7f,3.25f),
            b2Vec2(-0.7f,0.0f),
        };
        polygonShape.Set(points,4);
        m_RiderTest = cart->CreateFixture(&fixtureDef);
        ((GroundTestCallback*)m_GroundRayCastTest)->addIgnoreFixture(m_RiderTest);
        fixtureDef.isSensor = false;
        fixtureDef.density = 25.0f;



        fixtureDef.filter.categoryBits = 0x0010;
        fixtureDef.filter.maskBits = 0x0007;
        polygonShape.SetAsBox(TEST_SCALE*0.4f, TEST_SCALE*0.15f, axel1Pos, axel1Angle);
        ((TestCallback*)Test)->addIgnoreFixture(cart->CreateFixture(&fixtureDef));

        polygonShape.SetAsBox(0.4f*axel2Length, TEST_SCALE*0.15f, axel2Pos, axel2Angle);
        ((TestCallback*)Test)->addIgnoreFixture(cart->CreateFixture(&fixtureDef));

纹理类: -

TextureObject::TextureObject(char *texturename,CCNode *layer, int zOrder, bool isFrameCache,CCTexture2DPixelFormat pixFormat, int tag)
{
            CCTexture2D::setDefaultAlphaPixelFormat(pixFormat);
            CCSprite *newSprite = CCSprite::createWithSpriteFrameName(texturename);
            m_Layer = layer;
            m_Layer->addChild(newSprite,zOrder);
            newSprite->setTag(tag);
            m_Texture = newSprite;
            CCTexture2D::setDefaultAlphaPixelFormat(kTexture2DPixelFormat_Default);
}

在墙上课:-(检测碰撞检测)

bool wall::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
{
        b2Fixture *myFixture = m_MainCollision->GetFixtureList();
        if(myFixture == fixtureA || myFixture == fixtureB )
        {
            b2Body *bodyA =  myFixture->GetBody();
            **CCSprite *sprite = (CCSprite *) bodyA->GetUserData();**  // spriteA am getting null
            CCLog("spritea = %d",sprite->getTag());
            if (sprite->getTag() == 2)
            {
                // rider out and it will satrt from start point
            }
        }
    return false;
}

1 个答案:

答案 0 :(得分:1)

尝试这样: -

   bool wall::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
    {
        b2Body *bodyA =  fixtureA->GetBody();
        b2Body *bodyB =  fixtureB->GetBody();
        if(CartBody == bodyA || CartBody == bodyB )
        {
            CCLog("am collided correctly");
        }
        return false;
    }