void GamePhysicsContactListener::BeginContact(b2Contact* contact)
{
    // 衝突した双方の物体を取得
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    CCNode* nodeA = (CCNode*)bodyA->GetUserData();
    CCNode* nodeB = (CCNode*)bodyB->GetUserData();

    if( nodeA != NULL && nodeB != NULL ){
        //
        if( nodeA->getTag() == NODE_TAG_BALL ){
            Ball* pBall = (Ball*)nodeA;
            pBall->contactWith(nodeB);
        }
        else if( nodeB->getTag() == NODE_TAG_BALL ){
            Ball* pBall = (Ball*)nodeB;
            pBall->contactWith(nodeA);
        }
    }

#if 0
    // 物体にひもづくSpriteを取得
    PhysicsSprite* spriteA = (PhysicsSprite*)bodyA->GetUserData();
    PhysicsSprite* spriteB = (PhysicsSprite*)bodyB->GetUserData();

    // 地面との衝突は無視する
    if (spriteA->getTag() == Config::kTag_Ground ||
        spriteB->getTag() == Config::kTag_Ground)
    {
        return;
    }

    // 衝突時の加速度を取得
    b2Vec2 velocityA = bodyA->GetLinearVelocity();
    b2Vec2 velocityB = bodyB->GetLinearVelocity();
    CCLOG("[BeginContact] A(%f, %f) B(%f, %f)", velocityA.x, velocityA.y, velocityB.x, velocityB.y);

    // 加速度が一定上の大きさだったら、ぶつかられた方を削除する
    float threshold = 3;
    if (pow(velocityA.x, 2) + pow(velocityA.y, 2) > pow(threshold, 2)) {
        spriteB->setDeleteFlag(true);
    }
    if (pow(velocityB.x, 2) + pow(velocityB.y, 2) > pow(threshold, 2)) {
        spriteA->setDeleteFlag(true);
    }
#endif
}
Exemple #2
0
void Prototype::testSeparator() {
	b2Separator sep;
    vector<b2Vec2>* vec = new vector<b2Vec2>();
    vec->push_back(b2Vec2(-4, -4));
    vec->push_back(b2Vec2(4, -4));
    vec->push_back(b2Vec2(4, 0));
    vec->push_back(b2Vec2(0, 0));
    vec->push_back(b2Vec2(0, 4));
	vec->push_back(b2Vec2(-4, 4));

	m_bodyDef.position.Set((m_centerPoint.x-300)/PTM_RATIO, m_centerPoint.y/PTM_RATIO);
	b2Body* body = getWorld()->CreateBody(&m_bodyDef);
	try {
		sep.Separate(body, &m_fixtureDef, vec, PTM_RATIO);
	} catch(b2SeparatorException& e) {
		CCLog("b2Separator Exception: %s", e.what());
	}


	//	Only need below to attach box2d body to a cocos2d sprite...
	PhysicsSprite* ps = new PhysicsSprite();
	ps->setTag(2);
    ps->setPosition( CCPointMake( m_centerPoint.x+300, m_centerPoint.y ) );
    ps->setPhysicsBody(body);
	body->SetUserData(ps);
	m_sprites[ps->getTag()] = ps;
}
Exemple #3
0
void Prototype::testSimple() {
	b2PolygonShape shape;
	m_bodyDef.position.Set(m_centerPoint.x/PTM_RATIO, m_centerPoint.y/PTM_RATIO);
	b2Body* body = getWorld()->CreateBody(&m_bodyDef);
	shape.SetAsBox(4, 4);
	m_fixtureDef.shape = &shape;
	body->CreateFixture(&m_fixtureDef);

//	Only need below to attach box2d body to a cocos2d sprite...
	PhysicsSprite* ps = new PhysicsSprite();
	ps->setTag(1);
    ps->setPosition( CCPointMake( m_centerPoint.x+300, m_centerPoint.y ) );
    ps->setPhysicsBody(body);
	body->SetUserData(ps);
	m_sprites[ps->getTag()] = ps;

}