1

Here is code:

@interface MainScene : CCNode<CCPhysicsCollisionDelegate>

//.m

-(void)onEnter
{
    [super onEnter];

    mPhysicsWorld = [CCPhysicsNode node];
    mPhysicsWorld.gravity = ccp(0,0);

    mPhysicsWorld.debugDraw = YES;
    mPhysicsWorld.collisionDelegate = self;

    [self addChild:mPhysicsWorld];

    //Body_A
    {
        CCSprite *sprite = [CCSprite spriteWithImageNamed:@"sprite_1.png"];
        sprite.position = ccp(280, 220);
        sprite.rotation = 13;
        sprite.name = @"Body_A";
        sprite.physicsBody.collisionGroup=@"Group1";
        sprite.physicsBody.collisionType=@"typeA";
        CGRect rect = {CGPointZero, sprite.contentSize};
        CCPhysicsBody *body = sprite.physicsBody = [CCPhysicsBody bodyWithRect:rect cornerRadius:0.0];
        body.velocity = ccp(1000, 2000);
        body.angularVelocity = 1.0;
        body.type = CCPhysicsBodyTypeDynamic;

        [mPhysicsWorld addChild:sprite];
    }

    //Body_B
    {
        CCSprite *sprite = [CCSprite spriteWithImageNamed:@"sprite_2.png"];
        sprite.position = ccp(380, 220);
        sprite.rotation = 13;
        sprite.name = @"Body_B";
        sprite.physicsBody.collisionGroup=@"Group2";
        sprite.physicsBody.collisionType=@"typeB";
        CGSize size = sprite.contentSize;
        CGPoint points[] = {
            ccp(0, 0),
            ccp(size.width, 0),
            ccp(size.width/2, size.height),
        };

        CCPhysicsBody *body = sprite.physicsBody = [CCPhysicsBody bodyWithPolygonFromPoints:points count:3 cornerRadius:0.0];
        body.velocity = ccp(1000, -1000);
        body.angularVelocity = -1.0;
        body.type = CCPhysicsBodyTypeDynamic;

        [mPhysicsWorld addChild:sprite];
    }

//Delegate method

- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair typeA:(CCNode *)nodeA typeB:(CCNode *)nodeB
{
    printf("Function Called\n"); //control not coming here
    if([nodeA.name isEqualToString:@"Body_A"] &&  [nodeB.name isEqualToString:@"Body_B"])
    {
        return YES;
    }

    return NO;
}

Similar Question: Already tried this similar post but didn't get solution.

Community
  • 1
  • 1
Guru
  • 21,652
  • 10
  • 63
  • 102
  • Not certain, but i think that this topic may have some bearing on your question: http://forum.cocos2d-spritebuilder.org/t/solved-collision-detection-issue/16839 – YvesLeBorg Mar 01 '15 at 12:36
  • I tried it in init method but nothing displayed. – Guru Mar 02 '15 at 14:16
  • @YvesLeBorg, here is code: https://app.box.com/s/c5csoh5bbmnfc8zx34jsngzx42gsh13n – Guru Mar 02 '15 at 14:16
  • if you don't mind check code in free time and give me answer..I can offer bounty. Thanks for your time – Guru Mar 02 '15 at 14:17

1 Answers1

1

you are probably going to 'palm-forehead' , took me a while too :(

For both body A and body B the two lines below

sprite.physicsBody.collisionGroup=@"Group1";
sprite.physicsBody.collisionType=@"typeA";

are executed while physicsBody is still nil. Move the lines under the line :

CCPhysicsBody *body = sprite.physicsBody = [CCPhysicsBody bodyWithRect:rect cornerRadius:0.0];

ob cit. tested and verified, works on sim and device. Good luck with your project.

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
  • :- Sorry for simple mistake. Thanks a lot for your time. – Guru Mar 03 '15 at 11:45
  • @DustBin removed comment. somehow i was able to infer the lineage :) ... but i cant remember exactly how (probably an url). You should remove your comment too then. – YvesLeBorg Mar 03 '15 at 16:45