title: Swift: Contact detection between 2 nodes?
body: <p>Alright, so I've been following various other SO links and trying to figure this out- I need to have contact detection between 2 nodes. Not collision detection, which I learned results in the nodes bouncing each other around. I don't want them to knock each other around, I just want to know when they touch.</p>
  
  <p>Right now I have physics bodies for the 2 nodes, <code>savior</code> and <code>chicken1</code> as well as the ground (<code>ground</code>) upon which <code>savior</code> sits. These are all set up here:</p>
  
  <pre><code>        savior.physicsBody?.dynamic = true
          savior.physicsBody?.allowsRotation = false
          savior.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(savior.size.width, savior.size.height))
  
          chicken1.physicsBody?.dynamic = true
          chicken1.physicsBody?.allowsRotation = false
          chicken1.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(chicken1.size.width, chicken1.size.height)) 
  
          ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height*2))
          ground.physicsBody?.dynamic = false
  </code></pre>
  
  <p>I need to set up contact detection between <code>savior</code> and <code>chicken1</code>. There seem to be various ways to do this, but this is what I put together:</p>
  
  <pre><code>//Contact detection
  
          self.physicsWorld.contactDelegate = self
  
          savior.physicsBody?.categoryBitMask = saviorCategory
          savior.physicsBody?.contactTestBitMask = animalCategory
          savior.physicsBody?.collisionBitMask = 0
  
          chicken1.physicsBody?.categoryBitMask = animalCategory
          chicken1.physicsBody?.contactTestBitMask = saviorCategory
          chicken1.physicsBody?.collisionBitMask = 0
  
          func didBeginContact(contact: SKPhysicsContact) {
  
              var firstBody : SKPhysicsBody
              var secondBody : SKPhysicsBody
  
              if contact.bodyA.categoryBitMask &lt; contact.bodyB.categoryBitMask
              {
                  firstBody = contact.bodyA
                  secondBody = contact.bodyB
              }
              else {
                  firstBody = contact.bodyB
                  secondBody = contact.bodyA
              }
  
              if firstBody.categoryBitMask == 0 &amp;&amp; secondBody.categoryBitMask == 1 {
                  println("they made contact")
              }
  
      }
  </code></pre>
  
  <p>This code results in <code>savior</code> falling right through <code>ground</code> and going right through <code>chicken1</code>, with no contact detection because even when <code>savior</code> and <code>chicken1</code> touch, nothing happens.</p>
  
  <p>I need <code>savior</code> and <code>ground</code> to continue to collide, but I don't want <code>savior</code> and <code>chicken1</code> to collide, just touch. 
  The program needs to execute something when they touch.
  It's a mess but how can I fix this?</p>
  
  <p>EDIT:</p>
  
  <p>Here is what I have, <code>animalCategory</code> has been changed to <code>chickenCategory</code> for clarity and no contact is detected. Also <code>savior</code> still falls through <code>ground</code>.</p>
  
  <pre><code> self.physicsWorld.contactDelegate = self
          var screenTouches = Bool()
  
          let saviorCategory: UInt32 = 0x1 &lt;&lt; 0
          let chickenCategory: UInt32 = 0x1 &lt;&lt; 1
  
          savior.physicsBody?.categoryBitMask = saviorCategory
          savior.physicsBody?.contactTestBitMask = chickenCategory
          savior.physicsBody?.collisionBitMask = chickenCategory
  
          chicken1.physicsBody?.categoryBitMask = chickenCategory
          chicken1.physicsBody?.contactTestBitMask = saviorCategory
          chicken1.physicsBody?.collisionBitMask = saviorCategory
  
          func didBeginContact(contact: SKPhysicsContact) {
  
              var firstBody : SKPhysicsBody
              var secondBody : SKPhysicsBody
  
              if contact.bodyA.categoryBitMask == chickenCategory &amp;&amp; contact.bodyB.categoryBitMask == saviorCategory {
                  println("contact made")
                  savior.hidden = true
              }
              else if contact.bodyA.categoryBitMask == saviorCategory &amp;&amp; contact.bodyB.categoryBitMask == chickenCategory {
                  println("contact made")
                  savior.hidden = true
              }
          }
  </code></pre>