0
votes

Any idea why my didBeginContact method is never firing? The sphere and rectangles are actually touching each other...

Here i declare the two categories

static const uint32_t rectangleCategory = 0x1 << 0;
static const uint32_t ballCategory = 0x1 << 1;

This method creates a sphere

- (SKSpriteNode *)createSphereNode
{
SKSpriteNode *sphereNode =
[[SKSpriteNode alloc] initWithImageNamed:@"sphere.png"];

sphereNode.name = @"sphereNode";
sphereNode.physicsBody.categoryBitMask = ballCategory;
sphereNode.physicsBody.contactTestBitMask = rectangleCategory;

return sphereNode;
}

This method creates a rectangle

- (void) createRectangle
{
SKSpriteNode *rectangle = [[SKSpriteNode alloc] initWithImageNamed:@"balk.png"];

rectangle.position = CGPointMake(randomBetween(0, self.size.width),
                            self.size.height);
rectangle.name = @"rectangleNode";
rectangle.physicsBody =
[SKPhysicsBody bodyWithCircleOfRadius:(rectangle.size.width/2)-7];

rectangle.physicsBody.usesPreciseCollisionDetection = YES;
rectangle.physicsBody.categoryBitMask = rectangleCategory;
rectangle.physicsBody.contactTestBitMask = ballCategory;

[self addChild:rectangle];
}

This is the didBeginContact method that doesn't fire

- (void) didBeginContact:(SKPhysicsContact *)contact
{

NSLog(@"Contact begin");

SKSpriteNode *firstNode, *secondNode;

firstNode = (SKSpriteNode *)contact.bodyA.node;
secondNode = (SKSpriteNode *) contact.bodyB.node;

if ((contact.bodyA.categoryBitMask == rectangleCategory)
    && (contact.bodyB.categoryBitMask == ballCategory))
{
    CGPoint contactPoint = contact.contactPoint;

    float contact_x = contactPoint.x;
    float target_x = secondNode.position.x;
    float margin = secondNode.frame.size.height/2 - 25;

    if ((contact_x > (target_x - margin)) &&
        (contact_x < (target_x + margin)))
    {
        NSLog(@"Hit");
        self.score++;
    }
}
}

Any ideas? All help is much appreciated!

Thanks in advance

Stijn

1

1 Answers

2
votes

I originally suspected it could be the lack of a proper SKPhysicsContactDelegate as it was not mentioned in the original post. Upon inspection of the code however, this was not the case and the problem was simply that the SKPhysicsBody used for rectangle was created with the ill-fitting bodyWithCircleOfRadius: rather than the bodyWithRectangleOfSize:.

rectangle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: rectangle.size];

Original answer, not correct for this particular case, but leaving it here for anyone with a similar problem down the line:

There is no mention of the SKPhysicsContactDelegate anywhere to be seen in the code you supply. The didBeginContact: method is a delegate method for the physicsWorld property of your SKScene.

So if you for instance want to have the SKScene being its own delegate it must support the SKPhysicsContactDelegate protocol. e. g.:

@interface YourSceneSubclass : NSObject <SKPhysicsContactDelegate>

Then you need to set the delegate somewhere in your scene before the game starts proper...

self.physicsWorld.contactDelegate = self;