3

I am setting the collision bitmask on a physics body so that it does not collide but collisions are still happening.

-(void) createPlayer{
    player = [SKSpriteNode spriteNodeWithImageNamed:@"GonGonRed"];
    player.position = CGPointMake(40, 100);
    player.size = CGSizeMake(35*self.frame.size.height/320, 35*self.frame.size.height/320);
    player.zPosition = 7;    
    player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:player.size.width/2-1];
    player.physicsBody.dynamic = NO;
    player.physicsBody.allowsRotation = NO;
    player.physicsBody.usesPreciseCollisionDetection = YES;
    player.physicsBody.categoryBitMask = 1;
    player.physicsBody.contactTestBitMask = 4;
    player.physicsBody.collisionBitMask = 2;
    player.physicsBody.mass = 0.013963;
    [self addChild:player];
}


SKSpriteNode *wheel = [SKSpriteNode spriteNodeWithImageNamed:@"wheel"];
wheel.size = CGSizeMake(newwidth, newheight);
wheel.position = CGPointMake(px, py);
wheel.name = @"wheel";    
wheel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:newwidth/2];
wheel.physicsBody.dynamic = NO;
wheel.physicsBody.restitution = obstacleRestitution;
wheel.physicsBody.usesPreciseCollisionDetection = YES;
wheel.physicsBody.categoryBitMask = 3;
wheel.physicsBody.collisionBitMask = 0;
wheel.physicsBody.contactTestBitMask = 0;
wheel.zPosition = 5;

The wheel and the player should not collide but they are. I

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
Max Hudson
  • 9,961
  • 14
  • 57
  • 107

1 Answers1

3

This is caused by the below line.

wheel.physicsBody.categoryBitMask = 3;

The SpriteKit compares the body's categoryBitMask to the other body's collisionBitMask by performing a logical AND operation. When the result is Non-Zero Value, the collision is fired.

In this case, SpriteKit compares wheel's categoryBitMask to player's collisionBitMask like below.

The wheel's categoryBitMask is 0x011 (= 3)
The player's collisionBitMask is 0x010 (= 2)
The AND operation result is 0x010. This is Non-Zero so the collision is occurred.

So You should change wheel's categoryBitMask value for avoiding logical AND operation's result becoming Non-Zero, like below.

wheel.physicsBody.categoryBitMask = 8;

In addition, the dynamic property have to be set to YES.

kykomi
  • 122
  • 6
  • CategoryBitMasks are supposed to be a Uint32 with only 1 (or 0) bit(s) set, which means their value will be 0, 1, 2,4, 8, 16, 32, 64, etc etc. Any other value (like 3) effectively means that the physicsBody is in 2 categories at once (which might be valid is some scenarios). – Steve Ives Apr 16 '16 at 09:04
  • If you have an issue with collisions/contacts not working as expected, try adding the func checkPhysics() from this answer iOS SpriteKit - collisions and contacts not working as expected and then call checkPhysics() once you have set up everything as you think it should be (at the end of didMoveToView maybe?) and see if the printed results match what you expect – Steve Ives Apr 16 '16 at 15:28