3
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?

Repardeimaj
  • 401
  • 3
  • 15
  • I suggest you post the definition of `PhysicsCategory` – 0x141E Jan 07 '17 at 03:17
  • that's because isDynamic property. – Mina Jan 07 '17 at 05:02
  • I am going with @0x141E, the category numbering is probably wrong, obstacle is probably 3, which is not a valid category number – Knight0fDragon Jan 07 '17 at 05:42
  • 4
    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 : http://stackoverflow.com/a/31111039 – Whirlwind Jan 07 '17 at 08:49
  • Look here - http://stackoverflow.com/documentation/sprite-kit/6261/sknode-collision/24162/simple-sprite-kit-project-showing-collisions-contacts-touch-events 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 Jan 07 '17 at 11:40
  • thank you the example helped me. i had static let circle : UInt32 = 0x1 << 1 static let obstacle : UInt32 = 0x1 << 1 static let score : UInt32 = 0x1 << 1 instead of 0 1 2 – Repardeimaj Jan 07 '17 at 14:45
  • is that backwards? – Knight0fDragon Jan 07 '17 at 19:24

2 Answers2

2

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
billy123
  • 118
  • 1
  • 9
0

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.

ItsSgtMarv
  • 105
  • 1
  • 11