0

2つのボディ間の衝突を検出したいのですが、1つのボディは円形で、もう1つのボディは凸体です(頂点と頂点の数を使用)。また、コンタクトリスナークラスを使用してボディ間の衝突を検出します。2つの円の形の衝突を検出しようとして成功しましたが、2つの物体(1つは凸状、もう1つは円の形)の衝突を検出しようとすると失敗しました!私でさえ、これらのボディを識別するためにタグを使用しています。

2つの異なる形状(円と凸)を持つ2つの物体を検出するにはどうすればよいですか?

ContactListener.hコード:

#import "Box2D.h"

class ContactListener : public b2ContactListener 
{
private: 
    void BeginContact(b2Contact* contact);
    void EndContact(b2Contact* contact);
};

ContactListener.mmコード:

#import "ContactListener.h"
#import "SimpleAudioEngine.h"
#import "cocos2d.h"

void ContactListener::BeginContact(b2Contact* contact) {

    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
    CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

    if (spriteA != NULL && spriteB != NULL) {
        if (spriteA.tag == 1 && spriteB.tag == 50) { // this is work (two shapes are circles

            [[SimpleAudioEngine sharedEngine] playEffect:@"Pin.wav"];
            NSLog(@"Contact With Pin is Beginning");
        }

        if (spriteA.tag == 1 && spriteB.tag == 51) { // this is not work (one is circle shape and another one is convex shape)

            [[SimpleAudioEngine sharedEngine] playEffect:@"Jump.wav"];
            NSLog(@"Contact With Barrier is Beginning");
        }
    }
}

void ContactListener::EndContact(b2Contact* contact) {

    b2Body* bodyA = contact->GetFixtureA()->GetBody(); 
    b2Body* bodyB = contact->GetFixtureB()->GetBody(); 

    CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
    CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

    if (spriteA != NULL && spriteB != NULL) {

    }
}

上記のコードのタグ:

  • ボールのタグ(ゲームのメインスプライト)の形は1です
  • 円の形のタグは50です
  • 凸形状のタグは51です

編集:

これは、オブジェクトのボディ作成のコードです。

#define pinSpriteTag 50
#define barrierSpriteTag 51
#define ballSpriteTag 1

// Creating ball of game (has b2CircleShape)
-(void) createBallOfGameWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

        // Put ball on screen
        ballOfGameSprite = [CCSprite spriteWithFile:@"GameBall-1-ipad.png"];
        ballOfGameSprite.position = ccp(x, y);
        [self addChild:ballOfGameSprite z:1 tag: ballSpriteTag];

        // Create body of ball
        b2BodyDef ballOfGameBodyDef;
        ballOfGameBodyDef.type = b2_staticBody;
        ballOfGameBodyDef.position.Set(ballOfGameSprite.position.x/PTM_RATIO, ballOfGameSprite.position.y/PTM_RATIO);
        ballOfGameBodyDef.userData = ballOfGameSprite;
        ballOfGameBody = world->CreateBody(&ballOfGameBodyDef);

        // Create Physics properties of ball
        b2CircleShape ballOfGameShape;
        ballOfGameShape.m_radius = radius/PTM_RATIO;

        // Create fixture of ball
        b2FixtureDef ballOfGameFixtureDef;
        ballOfGameFixtureDef.shape = &ballOfGameShape;
        ballOfGameFixtureDef.density = 0.9f;
        ballOfGameFixtureDef.friction = 1.0f;
        ballOfGameFixtureDef.restitution = 0.9f;
        ballOfGameBody->CreateFixture(&ballOfGameFixtureDef);

}

// Creating pin of game (has b2CircleShape)
-(void) createNormalPinOnScreenWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

        // Put ball on screen
        CCSprite *pin = [CCSprite spriteWithFile:@"GamePinNormal-ipad.png"];
        pin.position = ccp(x, y);
        [self addChild:pin z:2 tag:pinSpriteTag];

        // Create body of ball
        b2BodyDef pinBodyDef;
        pinBodyDef.type = b2_staticBody;
        pinBodyDef.position.Set(pin.position.x/PTM_RATIO, pin.position.y/PTM_RATIO);
        pinBodyDef.userData = pin;
        b2Body *pinBody = world->CreateBody(&pinBodyDef);

        // Create Physics properties of ball
        b2CircleShape pinShape;
        pinShape.m_radius = radius/PTM_RATIO;

        // Create fixture of ball
        b2FixtureDef pinFixtureDef;
        pinFixtureDef.shape = &pinShape;
        pinFixtureDef.density = 0.9f;
        pinFixtureDef.friction = 1.0f;
        pinFixtureDef.restitution = 0.85f;
        pinBody->CreateFixture(&pinFixtureDef);
}

// Creating Barrier of game (has b2PolygonShape)
-(void) createBarrierOnScreenPositionX:(int)x yPosition:(int)y imageName:(NSString *)imageName verts:(b2Vec2*)verts verNum:(int)verNum {

    CCSprite *BarrierOfGameSprite = [CCSprite spriteWithFile:imageName];
    BarrierOfGameSprite.position = ccp(x, y);
    [self addChild:BarrierOfGameSprite z:1 tag:barrierSpriteTag];

    b2BodyDef BarrierOfGameBodyDef;
    BarrierOfGameBodyDef.type = b2_staticBody;
    BarrierOfGameBodyDef.position.Set(BarrierOfGameSprite.position.x/PTM_RATIO, BarrierOfGameSprite.position.y/PTM_RATIO);
    BarrierOfGameBodyDef.userData = BarrierOfGameSprite;

    b2Body *BarrierOfGameBody = world->CreateBody(&BarrierOfGameBodyDef);

    b2PolygonShape BarrierOfGameShape;
    BarrierOfGameShape.Set(verts, verNum);

    b2FixtureDef BarrierOfGameFixtureDef;
    BarrierOfGameFixtureDef.shape = &BarrierOfGameShape;    
    BarrierOfGameFixtureDef.density = 0.9f;
    BarrierOfGameFixtureDef.friction = 1.0f;
    BarrierOfGameFixtureDef.restitution = 0.0f;
    BarrierOfGameBody->CreateFixture(&BarrierOfGameFixtureDef); 

}
4

1 に答える 1

1

2 つの形状間の衝突を検出しません。最良の方法は、オブジェクトを本体の userData として設定することです。

@implemention RigidBody
-(id)init{
    ...
    self.body->userData = self;
}

次に、クラスとアドレスを比較します

void beginContact(b2contact contact){
    RigidBody *A = (RigidBody*)contact->GetFixtureA()->GetBody()->GetUserData();
    if([A isKindOfClass:[RigidBody class]]){

    }
    //  Or
    if(self == A){

    }

お役に立てば幸いです。

于 2012-05-09T06:14:42.877 に答える