3
votes
scorenode = SKSpriteNode()
    scorenode.size = CGSize(width: self.frame.width * 0.2, height: 1)
    scorenode.physicsBody = SKPhysicsBody(rectangleOf: scorenode.size)
    scorenode.color = .red
    scorenode.physicsBody?.affectedByGravity = false
    scorenode.physicsBody?.isDynamic = false

    scorenode.physicsBody?.categoryBitMask = PhysicsCategory.score
    scorenode.physicsBody?.contactTestBitMask = PhysicsCategory.circle
    scorenode.physicsBody?.collisionBitMask = 0


    obstaclePair = SKNode()
    obstaclePair.name = "obstaclePair"

    rightObstacle = SKSpriteNode(imageNamed:"square")
    rightObstacle.size = CGSize(width: self.frame.width * 0.8, height: self.frame.height / 10)

    rightObstacle.color = oColor
    rightObstacle.colorBlendFactor = 1.0
    rightObstacle.physicsBody = SKPhysicsBody(rectangleOf: rightObstacle.size)

    rightObstacle.physicsBody?.categoryBitMask = PhysicsCategory.obstacle
    rightObstacle.physicsBody?.collisionBitMask = PhysicsCategory.circle
    rightObstacle.physicsBody?.contactTestBitMask = PhysicsCategory.circle
    rightObstacle.physicsBody?.isDynamic = false

    leftObstacle = SKSpriteNode(imageNamed:"square")
    leftObstacle.size = CGSize(width: self.frame.width * 0.8 , height: self.frame.height / 10)
    leftObstacle.physicsBody = SKPhysicsBody(rectangleOf: leftObstacle.size)

    leftObstacle.color = oColor
    leftObstacle.colorBlendFactor = 1.0
    leftObstacle.physicsBody?.categoryBitMask = PhysicsCategory.obstacle
    leftObstacle.physicsBody?.collisionBitMask = PhysicsCategory.circle
    leftObstacle.physicsBody?.contactTestBitMask = PhysicsCategory.circle
    leftObstacle.physicsBody?.isDynamic = false
    circle = SKSpriteNode(imageNamed: "Circle")


circle.size = CGSize(width: 50, height: 50)
    circle.position = CGPoint(x: self.frame.width / 2, y: self .frame.height / 4)
    circle.color = .blue
    circle.colorBlendFactor = 1.0
    circle.physicsBody = SKPhysicsBody(circleOfRadius: circle.frame.height / 2)
    circle.physicsBody?.categoryBitMask = PhysicsCategory.circle
    circle.physicsBody?.collisionBitMask = PhysicsCategory.obstacle
    circle.physicsBody?.contactTestBitMask = PhysicsCategory.obstacle | PhysicsCategory.score
    circle.physicsBody?.affectedByGravity = false
    circle.physicsBody?.isDynamic = true
    circle.zPosition = 10

This is the physics part of my project, and I have a Circle SKSpritenode and a score node SKSpritenode. They aren't supposed to collide and i didn't put a collision bit mask for them both, but they still collide. What am i doing wrong?

2
I suggest you post the definition of PhysicsCategory0x141E
that's because isDynamic property.Mina
I am going with @0x141E, the category numbering is probably wrong, obstacle is probably 3, which is not a valid category numberKnight0fDragon
By default everything collides with everything. Collision bit mask by default has all bit set. So when two objects intersect a physics engine preforms logical AND between this body's collision bit mask and other's body categoryBitMask. If a result is non-zero value, a collision will occur. So like others said, check categories and keep in mind that default value of collision bit mask is 0xffffffff. Read here more : stackoverflow.com/a/31111039Whirlwind
Look here - stackoverflow.com/documentation/sprite-kit/6261/… for a sample project showing how to manage collisions with a helper function checkPhysics() that will analyse your physics setup and tell you what will collide or contact what.Steve Ives

2 Answers

2
votes

you have

 static let circle : UInt32 = 0x1 << 1
 static let obstacle : UInt32 = 0x1 << 1
 static let score : UInt32 = 0x1 << 1

you need

static let circle : UInt32 = 0x1 << 0
static let obstacle : UInt32 = 0x1 << 1
static let score : UInt32 = 0x1 << 2
0
votes

If you're not doing anything physics related with the score node why not just make it a labelnode and remove the physics from it.