title: How to Detect collision in Swift, Sprite kit
body: <p>I was programming this simple space game in Swift until I encountered the problem of detecting collisions. After looking around forums, tutorials, etc, I tried to implement collisions by declaring bitmasks like so:</p>
  
  <p>object 1</p>
  
  <pre><code>    enemy?.physicsBody = SKPhysicsBody(circleOfRadius: ((enemy?.size.width)!/2))
      enemy?.physicsBody?.categoryBitMask = enemyBitMask
      enemy?.physicsBody?.contactTestBitMask = bulletBitMask
      enemy?.physicsBody?.collisionBitMask = 0
  </code></pre>
  
  <p>object 2</p>
  
  <pre><code>    bullet?.physicsBody? = SKPhysicsBody(rectangleOf: (bullet?.size)!)
      bullet?.physicsBody?.categoryBitMask =  bulletBitMask
      bullet?.physicsBody?.contactTestBitMask =  enemyBitMask
      bullet?.physicsBody?.collisionBitMask = 0
      bullet?.physicsBody?.usesPreciseCollisionDetection = true
  </code></pre>
  
  <p>I also put a print statement in the </p>
  
  <pre><code>func didBegin(_ contact: SKPhysicsContact) { print("Hello") }
  </code></pre>
  
  <p>here is how I configure my sprites:</p>
  
  <pre><code>    func CreateNewEnemy() {
      var enemy : SKSpriteNode?
  
         let moveEnemyDown = SKAction.repeatForever(SKAction.moveBy(x: 0, y: -1, duration: 0.01))
         let rotateEnemy = SKAction.repeatForever(SKAction.rotate(byAngle: 25, duration: 5))
  
        let enemyXpos = randomNum(high:  self.frame.size.width/2, low: -1 * self.frame.size.width/2)
        let enemyYpos = randomNum(high:  2.5*self.frame.size.height, low: self.frame.size.height/2)
        let enemyOrigin : CGPoint = CGPoint(x: enemyXpos, y: enemyYpos)
  
        enemy = SKSpriteNode(imageNamed: possibleEnemyImage[Int(arc4random_uniform(4))])
        print(enemy?.size.height)
        enemy?.scale(to: CGSize(width: player.size.height, height: player.size.height))
        print(enemy?.size.height)
        enemy?.position = enemyOrigin
        enemy?.run(moveEnemyDown)
        enemy?.run(rotateEnemy)
        let enemyRadius : CGFloat = (enemy?.size.width)!/2
        print(enemyRadius)
  
        enemy?.physicsBody? = SKPhysicsBody(circleOfRadius: enemyRadius)
  
        enemy?.physicsBody?.categoryBitMask = enemyCategory
        enemy?.physicsBody?.contactTestBitMask = bulletCategory
        enemy?.physicsBody?.collisionBitMask = 0
        enemy?.zPosition = 1
  
        enemiesArray.append(enemy!)
        self.addChild(enemy!)
  }
  </code></pre>
  
  <p>to create enemy (Called in did move to view function)</p>
  
  <pre><code>    func CreateAllEnemies(amountOfEnemies : UInt8) {
      for _ in 0...amountOfEnemies {
          CreateNewEnemy()
      }
  }
  </code></pre>
  
  <p>and the other sprite </p>
  
  <pre><code>    func CreateNewBullet() {
       let bulletOrigin : CGPoint = CGPoint(x: player.position.x, y: player.position.y+player.size.height/2)
       let moveBulletUp = SKAction.repeatForever(SKAction.moveBy(x: 0, y: 3, duration: 0.01))
  
       var bullet : SKSpriteNode?
       bullet = SKSpriteNode(imageNamed: "bulletImage")
       bullet?.position = bulletOrigin
       bullet?.run(moveBulletUp)
  
       bullet?.physicsBody? = SKPhysicsBody(rectangleOf: (bullet?.size)!)
       bullet?.physicsBody?.categoryBitMask =  bulletCategory
       bullet?.physicsBody?.contactTestBitMask = enemyCategory
       bullet?.physicsBody?.collisionBitMask = 0
       bullet?.physicsBody?.isDynamic = true
       bullet?.physicsBody?.usesPreciseCollisionDetection = true
       bullet?.zPosition = 1
  
       bulletsArray.append(bullet!)
       self.addChild(bullet!)
  }
  </code></pre>
  
  <p>this one is created with a timer </p>
  
  <pre><code>bulletTimer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(CreateNewBullet) , userInfo: nil, repeats: true)
  </code></pre>
  
  <p>Unfortunately it does not print anything in the console after I see the two objects touching.</p>