1

I don't understand UInt32 in spritekit categorybitmask. They are very confusing to me because they are typed like: "0b01 << 1" or "0x01" or something like that. I understand their usage in spritekit, but I don't understand the unit.

Jiri
  • 43
  • 7
  • 1
    This isn't a SpriteKit question. You're having trouble understanding binary, hex, bitmasks and the syntax for denoting such concepts in swift. Check out the language guides for these concepts and it'll start to make sense. – Dennis Mar 12 '18 at 18:16
  • I don't think that's quite fair. SpriteKit advertises itself as an "easy to use" game engine, and then does an absolutely atrocious job of being easy to use, a terrible job of being a game engine and a horrid job of explaining itself. This is not isolated to this part of the "engine". – Confused Mar 13 '18 at 03:01
  • That there isn't a more elegant and simple to use masking system for physics object detection and responses is a travesty and an indictment. – Confused Mar 13 '18 at 03:03
  • @Confused I don't mean to open an OT dialogue here, but what game engine(s) do you prefer as an alternative to SpriteKit? – peacetype Mar 13 '18 at 04:25
  • Each has their strengths and weaknesses in different areas. SpriteKit's greatest benefit, I think, is iteration build times and performance on a Mac, to that same Mac, to test gameplay tunings and ideas, and relatively lightweight iOS builds. – Confused Mar 13 '18 at 04:31
  • But SpriteKit isn't really a game engine. It's a sort of visual framework with stuff that looks like a game engine might look, but requires the amount of work, effort, understanding and research you'd expect of a framework. – Confused Mar 13 '18 at 04:32
  • Because of this framework-like nature of SpriteKit, new users coming from anything with a modicum of abstraction and concern for the user experience are completely baffled by the bizarre, low-level, obscure and archaic things like this post covers. – Confused Mar 13 '18 at 04:35
  • @Confused Thanks. SpriteKit is my first foray into game dev so it's helpful to hear how it stacks up against alternatives from people who know. I recently tried Unreal Engine but didn't like the GUI approach to coding. I'm thinking to check out Unity next since it's a popular option. – peacetype Mar 13 '18 at 07:16
  • SpriteKit is a LOT of work to do even the simplest of things. And then you need to worry about the effort with Swift initialisation. You'll lose your mind chasing that around in circles. And nobody has written a good book on SpriteKit. Least of all Apple. – Confused Mar 13 '18 at 07:21
  • 1
    @Confused I think spritekit is lacking many features it could have, but for me that kinda makes it good for me because it's my first coding related thing I have used so, I learn many other things along the learning it (now these binaries). The biggest thing for me is that it belongs to apple bubble, so it's not compatible with android. Do you know any easy and free ways to port a game to android from spritekit. And I think sprtekit is a framework not a engine. – Jiri Mar 13 '18 at 17:26
  • I agree, @Jiri, learning (some) Sprite Kit is the easiest way for me to get a look at how Apple thinks about its frameworks. Good and bad. – Confused Mar 15 '18 at 02:03

1 Answers1

1

You have to understand a little about binary which can be tricky when you first encounter it. The code that is confusing you is shorthand for working with binary.

For example, in 0b01 << 1 the 0b prefix means the following number is given in binary. This is helpful because if you just saw, for example, 1001, you might think it is the number "one thousand and one". But if you write 0b1001 you know that here, 1001 is a binary number. (Read more)

Similarly, 0x is a prefix used to express hexadecimal numbers (because x kinda sounds like "hex" I guess). I personally prefer to use the 0b prefix when dealing with bitmasks in SpriteKit, but that's just me.

Now on to bit shifting. << is the bitwise left shift operator in Swift. It moves all of the bits in a number to the left by a certain number of places. For example, the decimal number 3 is written as 0011 in binary.

3 = 0011            // the number 3 is 0011 in binary
3 << 1 = 0110 = 6   // take 0011 and shift all bits one place to the left
3 << 2 = 1100 = 12  // take 0011 and shift all bits two places to the left

The example you gave was 0b01 << 1. If you just enter 0b01, that is the binary representation of the decimal number 1. By entering 0b01 << 1 you are telling the program to take the binary number 01 (more explicitly written as 0b01) and shift its bits one place to the left, which results in the binary number 10 (more explicitly written as 0b10) , which is the number 2 in decimal.

You can actually write 0b01 as 0b1 for short and it means the same thing since you are just omitting the leading zero.

Try entering the following in an Xcode Playground and look at the results you get:

0b1        // decimal 1
0b1 << 1   // decimal 2
0b1 << 2   // decimal 4
0b1 << 3   // decimal 8
0b1 << 4   // decimal 16

You can play around with this, bit shifting by greater amounts, and you will see that the numbers continue to double. It turns out that this is a handy way for SpriteKit to implement bitmasks for its physics engine.

UPDATE:

You can define up to 32 different categories to use for physics bitmasks. Here they all are with their corresponding decimal values:

0b1       // 1
0b1 << 1  // 2
0b1 << 2  // 4
0b1 << 3  // 8
0b1 << 4  // 16
0b1 << 5  // 32
0b1 << 6  // 64
0b1 << 7  // 128
0b1 << 8  // 256
0b1 << 9  // 512
0b1 << 10 // 1,024
0b1 << 11 // 2,048
0b1 << 12 // 4,096
0b1 << 13 // 8,192
0b1 << 14 // 16,384
0b1 << 15 // 32,768
0b1 << 16 // 65,536
0b1 << 17 // 131,072
0b1 << 18 // 262,144
0b1 << 19 // 524,288
0b1 << 20 // 1,048,576
0b1 << 21 // 2,097,152
0b1 << 22 // 4,194,304
0b1 << 23 // 8,388,608
0b1 << 24 // 16,777,216
0b1 << 25 // 33,554,432
0b1 << 26 // 67,108,864
0b1 << 27 // 134,217,728
0b1 << 28 // 268,435,456
0b1 << 29 // 536,870,912
0b1 << 30 // 1,073,741,824
0b1 << 31 // 2,147,483,648
peacetype
  • 1,928
  • 3
  • 29
  • 49
  • Thank you a lot for your answer. Now I understand this. But if I'm right you can't have one mask be "0b10" (that is 2 I think) and some other one "0b11" (that is 3). Can you clarify this to me, please. – Jiri Mar 13 '18 at 17:13
  • 1
    When dealing with bitmasks in SpriteKit you should stick to typing them like `0b1 << 2` and not `0b10`. It's easier for humans to read them that way. I updated my answer to explain why you don't use values like decimal 3 with SpriteKit bitmasks. Notice that 3 and many other numbers are skipped when the bitmask values are doubled. – peacetype Mar 13 '18 at 19:38
  • Thank you for making much effort for educating me with this. Have a great day! – Jiri Mar 13 '18 at 19:45
  • I would but my reputation is too low (13 should be 15) I will come back when it’s 15 ;) – Jiri Mar 13 '18 at 22:44
  • Gotta get those reputations ;) – Jiri Mar 13 '18 at 23:05