title: How to use correctly Scenekit's category mask, collision mask and physicsbody, contactTestBitMask and make proper collisions?
body: <p>I have been trying to learn scenekit and have finished one book but only collision detection part is not understood, maybe the most important part.
  There is category mask, collusion mask and physicsbody?.contactTestBitMask.</p>
  
  <p>I want to create a simple game to get this but I got stuck. I am not sure I get the idea.</p>
  
  <p>In game the game, there is a ball and collects pearls, and stays away from rocks and holes. In this case:</p>
  
  <p>Category masks:</p>
  
  <ul>
  <li>ball = 0b0001</li>
  <li>pearls = 0b0010</li>
  <li>rocks = 0b0100</li>
  <li>holes = 0b1000</li>
  </ul>
  
  <p>physicsBody?.contactTestBitMask:</p>
  
  <ul>
  <li>ball = pearl || rocks // means 0b1110</li>
  <li>pearls = 1</li>
  <li>rocks = 1</li>
  </ul>
  
  <p>Collusion masks are 1 because they all collide with each other.</p>
  
  <p>I am not sure I get this collision issue. So before I begin to write code, I wanted to be sure. In SCNPhysicsContactDelegate, function below solves how to learn when they contact with each other:</p>
  
  <pre><code>physicsWorld(_ didBegin contact: _) {
  
  var contactNode:SCNNode!
  if contact.nodeA.name == "ball" {
   contactNode = contact.nodeB
   } else {
   contactNode = contact.nodeA
  }
  
  if contactNode.physicsBody?.categoryBitMask == 0b0010 {
   // mean pearls
   // raise score etc
  }
  
  if contactNode.physicsBody?.categoryBitMask == 0b0100 || 0b1000{
   if contactNode.name == "Rock" { print("You rocked") }
   if contactNode.name == "Hole" { print("You need to climb") }
  }
  
  }
  </code></pre>
  
  <p>I have searched youtube and stack but there is only one explanation.
  <a href="https://stackoverflow.com/questions/27372138/how-to-set-up-scenekit-collision-detection">Stack Link</a>
  Youtube videos are not explaining these.
  The book examples are copyrighted, so I can't put them on here.</p>
  
  <p>Thank you,
  Have a nice day.</p>