0

I have 3 SKSpriteNodes in my Scene. One bird, one coin and a border around the scene. I don't want the coin and the bird to collide with each other but withe the border. I assign a different collisionBitMask and categoryBitMask to every node:

enum CollisionType:UInt32{
    case Bird  = 1
    case Coin = 2
    case Border = 3 
}

Like so:

bird.physicsBody!.categoryBitMask = CollisionType.Bird.rawValue
bird.physicsBody!.collisionBitMask = CollisionType.Border.rawValue

coin.physicsBody!.categoryBitMask = CollisionType.Coin.rawValue
coin.physicsBody!.collisionBitMask = CollisionType.Border.rawValue

But the coin and the bird still collide with each other. What am I doing wrong?

Jonas Ester
  • 535
  • 4
  • 19

1 Answers1

2

The bitmask is on 32 bits. Declaring them like you did corresponds to :

enum CollisionType:UInt32{
    case Bird = 1 // 00000000000000000000000000000001
    case Coin = 2 // 00000000000000000000000000000010
    case Border = 3 // 00000000000000000000000000000011
}

What you want to do is to set your border value to 4. In order to have the following bitmask instead :

enum CollisionType:UInt32{
    case Bird = 1 // 00000000000000000000000000000001
    case Coin = 2 // 00000000000000000000000000000010
    case Border = 4 // 00000000000000000000000000000100
}

Keep in mind that you'll have to follow the same for next bitmask : 8, 16, ... an so on.

Edit :

Also, you might want to use a struct instead of an enum and use another syntax to get it easier (it's not mandatory, just a matter of preference) :

struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let Bird       : UInt32 = 0b1       // 1
    static let Coin       : UInt32 = 0b10      // 2
    static let Border     : UInt32 = 0b100     // 4
}

That you could use like this :

bird.physicsBody!.categoryBitMask = PhysicsCategory.Bird
bird.physicsBody!.collisionBitMask = PhysicsCategory.Border

coin.physicsBody!.categoryBitMask = PhysicsCategory.Coin
coin.physicsBody!.collisionBitMask = PhysicsCategory.Border
lchamp
  • 6,592
  • 2
  • 19
  • 27
  • Thank you very much! I am a newbie in this topic. Can you maybe give me some link or keywords where I can find some more information about things like "UInt32 = 0b100"? – Jonas Ester Mar 05 '15 at 19:39
  • You're welcome. The syntax is "bit shifting", maybe the apple documentation ( http://goo.gl/DXVBJD ) is too complicated to start with. But, the basic idea is that the initial bit (1) representation is : 00000000000000000000000000000001. If you shift it from one bit, using either this syntax `0b10` or this one `0x1 << 1` the bit goes one place to the left : 00000000000000000000000000000010. You can shift it from two place using `0b100` or `0x1 << 2` and so on... :) – lchamp Mar 05 '15 at 19:46
  • So, if I need for example another PhysicsCategory I could do `static let Board : UInt32 = 0b1000 // 8` right? – Jonas Ester Mar 05 '15 at 20:21
  • 1
    Sorry, I was responding other questions and trying to make a table for you. So those are bits, search for them on internet if needed. But to respond your question : yes you would do that. Here the table I made for you, it might help you understand better : http://goo.gl/Qmnr6a – lchamp Mar 05 '15 at 20:31
  • WOW!! Men, you are awesome!!! Thank you so much for your work. Because of your table I now understand the principle of bit shifting. – Jonas Ester Mar 05 '15 at 20:47