Search code examples
cocos2d-iphonecollision-detectiondetectionchipmunk

cocos2d v3 chipmunk collision method not firing


ok so i've been messing around with chipmunk a bit, and i can get two sprites to bounce off of each other, but when i try to use the following method, it never fires,

-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair tower:(CCNode *)nodeA BG:    
   (CCNode *)nodeB
   {
   NSLog(@"HELLO");
   return YES;
   }

Heres where I create the physics node:

    _physics = [CCPhysicsNode node];
    _physics.debugDraw = YES;
    [self addChild:_physics z:1];
    _physics.collisionDelegate = self;

I use this code to create the first sprite:

    background = [CCSprite spriteWithImageNamed:gameLevelImage];
    [background setPosition:ccp(winSize.width/2,winSize.height/2)];
    background.physicsBody.collisionType = @"BG";
    background.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:50 andCenter:self.position];

and this for the other :

    tower = [[TowerType alloc] initWithTheGame:self location:ccp(winSize.width/2, winSize.height/2)];
    [towers addObject:tower];
    [self MenuItemsVisible];
    tower.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:50 andCenter:tower.position];
    tower.physicsBody.collisionType = @"tower";

I also have the protocol in the h file.

if anyone knows whats happening help would be greatly appreciated. (:


Solution

  • First of all, are both bodies under the same CCPhysicsNode?

    Second, ccPhysicsCollisionBegin is just called when the collision BEGIN, so as both of your bodies are one over the other and they aparenttly will move together due to gravity the collision will never begin, because they started colliding. The cycle for collision evaluation is:

    ccPhysicsCollisionBegin: called once both bodies start colliding
    ccPhysicsCollisionPreSolve: called every frame update, before physics calculations
    ccPhysicsCollisionPostSolve : called every frame, after physics calculations
    ccPhysicsCollisionSeparates: called once they separate