title: How does collisionBitMask work? Swift/SpriteKit
body: <p>As far as I'm aware the default for Physics bodies is to bounce off each other when they hit each other until you set their collisionBitMask to an equal number.</p>
  
  <p>However, I'm having a huge issue accomplishing what seems like it should be very simple because of collisionBitmasks I believe.</p>
  
  <pre><code>let RedBallCategory  : UInt32 = 0x1 &lt;&lt; 1
  let GreenBallCategory: UInt32 = 0x1 &lt;&lt; 2
  let RedBarCategory : UInt32 = 0x1 &lt;&lt; 3
  let GreenBarCategory : UInt32 = 0x1 &lt;&lt; 4
  let WallCategory : UInt32 = 0x1 &lt;&lt; 5
  
  
  greenBall.physicsBody?.categoryBitMask = GreenBallCategory
  greenBall.physicsBody?.contactTestBitMask = RedBarCategory
  greenBall.physicsBody?.collisionBitMask = GreenHealthCategory
  
  redBall.physicsBody?.categoryBitMask = RedBallCategory
  redBall.physicsBody?.contactTestBitMask = GreenBarCategory
  redBall.physicsBody?.collisionBitMask = RedHealthCategory
  
  let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
  self.physicsBody = borderBody
  self.physicsBody?.friction = 0
  borderBody.contactTestBitMask = RedBallCategory | GreenBallCategory
  borderBody.categoryBitMask = WallCategory
  </code></pre>
  
  <p>So here I've got my 2 balls and my border body. I can get collision detection which I want, but when I add the border body's category bit mask, it allows the balls to both go through and off the screen, which I don't want.</p>
  
  <p>I also want the balls to bounce off each other, but only when I comment out one of the ball's categoryBitMasks do they bounce. Otherwise they go through each other.</p>
  
  <p>That makes absolutely no sense to me because each of these items has a different collisionbitmask. I also had it sometimes where setting all the numbers equal to 5 would allow everything to pass through each other, but then setting it all to 6 would allow everything to hit each other.</p>
  
  <p>How exactly do collision bitmasks work and is there a proper way to manage a lot of crisscrossing collision rules?</p>