Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 游戏引擎Collison Bitmask。。。为什么是0x01等?_Ios_Sprite Kit_Cocos2d X_Bitmask - Fatal编程技术网

Ios 游戏引擎Collison Bitmask。。。为什么是0x01等?

Ios 游戏引擎Collison Bitmask。。。为什么是0x01等?,ios,sprite-kit,cocos2d-x,bitmask,Ios,Sprite Kit,Cocos2d X,Bitmask,在Sprite Kit(iOS开发)和Cocos2d-x(我知道这是Sprite Kit的灵感来源,因此他们使用了很多相同的工具)中都遇到了这种情况,我最终决定弄清楚为什么会发生这种情况: 使用physic引擎时,我创建了一个精灵,并向其中添加了physicsBody。在大多数情况下,我了解如何设置类别、冲突和联系人位掩码,以及它们是如何工作的。问题在于实际的位掩码编号: SpriteKit: static const uint32_t missileCategory = 0x1 &l

在Sprite Kit(iOS开发)和Cocos2d-x(我知道这是Sprite Kit的灵感来源,因此他们使用了很多相同的工具)中都遇到了这种情况,我最终决定弄清楚为什么会发生这种情况:

使用physic引擎时,我创建了一个精灵,并向其中添加了physicsBody。在大多数情况下,我了解如何设置类别、冲突和联系人位掩码,以及它们是如何工作的。问题在于实际的位掩码编号:

SpriteKit:

static const uint32_t missileCategory     =  0x1 << 0;
sprite.physicsBody.categoryBitMask = missileCategory;

我完全搞不懂为什么要写0x01或0x1位掩码的原因是,它使您/程序能够轻松、非常快速地计算两个对象之间是否发生碰撞。因此:这是某种优化

假设我们有三个类别

  • 导弹
    0x1很好,但只需再进一步,在其他方向


    为什么用十六进制表示法?(
    0x1来回答您的具体问题

    “为什么有32种不同的类别可用?我想是32位的 整数有0-10亿个数字(当然是无符号的)。那为什么呢 难道我没有几十亿种可能的分类吗?”

    答案是,该类别始终被视为32位掩码,其只应设置一个位。因此,以下是有效值:

    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
    

    0000000000000000000000000000000 1=1=1您可能希望签出,这有一个很好的示例。这是他们使用位掩码的部分,向上滚动以了解他们将如何设置位掩码。使用0x1 0B10000000000000000000、2147483648、0x80000000或0x1,我无法浏览此部分:“如果导弹和导弹集合!=0{…}”",依我的想法,导弹是00010,导弹集合是00101,两者都不是0,所以会发生这样的事情……我怎么会不理解它呢?@这是一个按位and,按位and的结果,在两个输入都有1的地方都有1,所有其他位都是0。在您的示例中,00010和00101在同一位置没有任何1,因此对它们进行AND运算的结果为0。
    
    let player : UInt8 = 0b1 << 0  // 00000001 = 1
    let missile : UInt8 = 0b1 << 1 // 00000010 = 2
    let wall : UInt8 = 0b1 << 2    // 00000100 = 4
    
    let playerCollision = player | missile | wall // 00000111 = 7
    let missileCollision = player | wall          // 00000101 = 5
    
    if player & missileCollision != 0 {
        print("potential collision between player and missile") // prints
    }
    if missile & missileCollision != 0 {
        print("potential collision between two missiles") // does not print
    }
    
    let playerCategory : UInt8 = 0b1 << 0
    let missileCategory : UInt8 = 0b1 << 1
    let wallCategory : UInt8 = 0b1 << 2
    
    struct EntityStruct {
        var categoryBitmask : UInt8
        var collisionBitmask : UInt8
    }
    
    let player = EntityStruct(categoryBitmask: playerCategory, collisionBitmask: playerCategory | missileCategory | wallCategory)
    let missileOne = EntityStruct(categoryBitmask: missileCategory, collisionBitmask: playerCategory | wallCategory)
    let missileTwo = EntityStruct(categoryBitmask: missileCategory, collisionBitmask: playerCategory | wallCategory)
    let wall = EntityStruct(categoryBitmask: wallCategory, collisionBitmask: playerCategory | missileCategory | wallCategory)
    
    func canTwoObjectsCollide(first:EntityStruct, _ second:EntityStruct) -> Bool {
        if first.categoryBitmask & second.collisionBitmask != 0 {
            return true
        }
        return false
    }
    
    canTwoObjectsCollide(player, missileOne)     // true
    canTwoObjectsCollide(player, wall)           // true
    canTwoObjectsCollide(wall, missileOne)       // true
    canTwoObjectsCollide(missileTwo, missileOne) // false
    
    let catA = 0b0001
    let catB = 0b0010
    let catC = 0b0100
    
    static const uint32_t catA =  1 << 0;
    static const uint32_t catB =  1 << 1;
    static const uint32_t catC =  1 << 2;
    
    static const uint32_t catA =  1;
    static const uint32_t catB =  2;
    static const uint32_t catC =  4;
    
    if (bodyA.categoryBitMask & bodyB.collisionBitMask != 0)
    
    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
    
    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