Game Engine Collison Bitmask… Why 0x01 etc?

前端 未结 3 1920
旧时难觅i
旧时难觅i 2020-12-09 21:38

Coming across this situation both in Sprite Kit (iOS Development) and in Cocos2d-x (which I know was pretty much the inspiration for Sprite Kit, hence why they use a lot of

3条回答
  •  爱一瞬间的悲伤
    2020-12-09 22:24

    To answer your specific question

    "why there are 32 different categories available? I thought a 32-bit integer had numbers 0-some billion number (unsigned of course). So why do I not have billions of different possible categories?"

    The answer is that the category is always treated as a 32-digit bit mask of which ONLY ONE bit should be set. So these are the valid values:

    00000000000000000000000000000001 = 1 = 1 << 0
    00000000000000000000000000000010 = 2 = 1 << 1
    00000000000000000000000000000100 = 4 = 1 << 2
    00000000000000000000000000001000 = 8 = 1 << 3
    00000000000000000000000000010000 = 16 = 1 << 4
    00000000000000000000000000100000 = 32 = 1 << 5
    00000000000000000000000001000000 = 64 = 1 << 6
    00000000000000000000000010000000 = 128 = 1 << 7
    00000000000000000000000100000000 = 256 = 1 << 8
    00000000000000000000001000000000 = 512 = 1 << 9
    00000000000000000000010000000000 = 1024 = 1 << 10
    00000000000000000000100000000000 = 2048 = 1 << 11
    .
    .
    .
    10000000000000000000000000000000 = 2,147,483,648 = 1 << 31
    

    So there are 32 diffeeent categories available. Your categoryBitMask however can have multiple bits sets so can indeed be any number from 1 to whatever the maximum of UInt32 is. for example, in an arcade game you might have categories such as:

    00000000000000000000000000000001 = 1 = 1 << 0   //Human
    00000000000000000000000000000010 = 2 = 1 << 1   //Alien
    00000000000000000000000000000100 = 4 = 1 << 2   //Soldier
    00000000000000000000000000001000 = 8 = 1 << 3   //Officer
    00000000000000000000000000010000 = 16 = 1 << 4  //Bullet
    00000000000000000000000000100000 = 32 = 1 << 5 //laser
    00000000000000000000000001000000 = 64 = 1 << 6 //powershot
    

    so a human civilian might have a categoryBitMask of 1, a human soldier 5 (1 + 4), an alien officer 6, a normal bullet 16, a missile 80 (16 + 64), mega-death ray 96 etc etc.

提交回复
热议问题