Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top