Domanda

Hello I have 3 phsyic bodies:

2 of them interact perfectly as expected, but when I try to add some boundaries, my other 2 objects won't collide with the boundaries:

SKShapeNode* node = [[SKShapeNode alloc]init];
CGRect rect = CGRectMake(0, 0, 270, 1);
node.path = [UIBezierPath bezierPathWithRect:rect].CGPath;
node.fillColor = SKColor.whiteColor;
node.strokeColor = nil;
node.position = CGPointMake(20, 50);

node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rect.size];
node.physicsBody.affectedByGravity = NO;
node.physicsBody.categoryBitMask = boundriesCat;
node.physicsBody.contactTestBitMask = blockCat | breakerCat;
node.physicsBody.collisionBitMask = blockCat | breakerCat;

[self addChild:node];

Here the init of the other object:

CGRect circle = CGRectMake(0, 0, 20, 20);
    self.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
    if (code == 4)
        self.fillColor = SKColor.redColor;
    if (code == 5)
        self.fillColor = SKColor.greenColor;
    if (code == 6)
        self.fillColor = SKColor.blueColor;
    if (code == 7)
        self.fillColor = SKColor.yellowColor;
    self.strokeColor = nil;

    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:circle.size];
    self.physicsBody.dynamic = YES;
    self.physicsBody.categoryBitMask = breakerCat;
    self.physicsBody.contactTestBitMask = blockCat | breakerCat;
    self.physicsBody.collisionBitMask = boundriesCat;

I hope you can help, first day with sprite kit ;(

È stato utile?

Soluzione

Check your collisionBitMasks. By default they are set to 0xFFFFFFFF (node will collide with all the other nodes). However, if you want to explicitly set the collisions, you should set your masks something like this:

static const uint32_t kBoundariesCat = (0x01 << 0); // 0001
static const uint32_t kBreakerCat    = (0x01 << 1); // 0010
static const uint32_t kBlockCat      = (0x01 << 2); // 0100

static const uint32_t kBoundariesCatCollisionMask = kBreakerCat | kBlockCat;      // 0110
static const uint32_t kBreakerCatCollisionMask    = kBoundariesCat | kBlockCat;   // 0101
static const uint32_t kBlockCatCollisionMask      = kBoundariesCat | kBreakerCat; // 0011

boundariesCat.physicsBody.categoryBitMask = kBoundariesCat;
boundariesCat.physicsBody.collisionBitMask = kBoundariesCatCollisionMask;

breakerCat.physicsBody.categoryBitMask = kBreakerCat;
breakerCat.physicsBody.collisionBitMask = kBreakerCatCollisionMask;

breakerCat.physicsBody.categoryBitMask = blockCat;
breakerCat.physicsBody.collisionBitMask = kBlockCatCollisionMask;

Also, if you want to enable contact handlers, you set the contactTestBitMasks:

static const uint32_t kBoundariesCatContactMask = kBreakerCat | kBlockCat;
static const uint32_t kBreakerCatContactMask    = kBoundariesCat | kBlockCat;
static const uint32_t kBlockCatContactMask      = kBoundariesCat | kBreakerCat;

boundariesCat.physicsBody.contactTestBitMask = kBoundariesCatContactMask;
breakerCat.physicsBody.contactTestBitMask = kBreakerCatContactMask;
breakerCat.physicsBody.contactTestBitMask = kBlockCatContactMask;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top