什么是 Sprite Kit 的“类别掩码"?和“碰撞掩码"? [英] What are Sprite Kit's "Category Mask" and "Collision Mask"?

查看:23
本文介绍了什么是 Sprite Kit 的“类别掩码"?和“碰撞掩码"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 Sprite Kit 指南,在场景编辑器中,它要求我将 Category Mask 设置为 32,将 Collision Mask 设置为 11.这些数字有什么关系?

I am following the Sprite Kit guide, and in the scene editor it asks me to set the Category Mask to 32 and Collision Mask to 11. How are these numbers related?

推荐答案

  • 类别位掩码告诉 Sprite-Kit 这是什么类型的对象.
  • Collision 位掩码告诉 Sprite Kit 这个对象是什么对象碰撞(即会撞击并弹开).
  • ContactTest 位掩码告诉 Sprite-Kit 你想要什么联系人收到通知,即当这个物体接触到另一个物体时.
  • 碰撞由 Sprite-Kit 游戏引擎自动处理;联系人由您的代码处理 - 当您感兴趣的联系人发生时,您的代码(didBeginContact' 用于 Swift 2,'didBegin(contact:) 用于 Swift 3 和稍后)被调用.

    Collisions are handled automatically by the Sprite-Kit game engine; contacts are handled by your code - when a contact happens that you are interested in, your code (didBeginContact'for Swift 2,'didBegin(contact:) for Swift 3 and later) is called.

    您需要以二进制形式考虑这些位掩码,为简单起见,我们将从简单的类别位掩码开始,其中场景中的每个对象仅属于一个类别.

    You need to think of these bitmasks in binary and for simplicity, we'll start with simple category bit masks, whereby each object in your scene belongs to only one category.

    例如在你的场景中,你有一个玩家、一个玩家导弹、一个敌人和屏幕边缘.我们假设位掩码是 8 位而不是 32 位.将每个数字视为 8 个二进制数字(8 位)的范围,每个数字都是 0 或 1.

    E.g. in your scene you have a player, a player-missile, an enemy and the screen-edge. We'll assume that the bit masks are 8-bit instead of 32-bit. Think of each number as a range of 8 binary digits (8 bits) each of which is a 0 or 1.

    场景中的对象必须具有独特的类别,因此我们将按如下方式分配它们:

    The objects in your scene have to have unique categories, so we'll assign them as follows:

    player.categoryBitMask        = 00000001  (1 in decimal)
    playerMissile.categoryBitMask = 00000010  (2 in decimal)
    enemy.categoryBitMask         = 00000100  (4 in decimal)
    screenEdge.categoryBitMask    = 00001000  (8 in decimal)
    

    如果您使用的数字是十进制的,不是 2 的幂(1、2、4、8、16、32、64、128 等),那么将在类别位中设置超过 1 位使事情复杂化的掩码(这意味着该对象属于多个类别)并且您的代码必须变得更加复杂.

    If you use numbers that are, in decimal, something other than a power of 2 (1, 2, 4, 8, 16, 32, 64, 128 etc) then more than 1 bit will be set in the category bit mask which complicates things (it means that this object belongs to multiple categories) and your code has to get more complicated.

    现在想想什么会从什么(碰撞)中反弹.假设除了导弹穿过屏幕边缘外,其他所有东西都会反弹.

    Now think about what bounces off what (the collisions). Let's say everything bounces off everything else except the missile goes through the screen edge.

    每个对象的碰撞位掩码由代表该对象碰撞的对象的位组成,即:

    The collision bit masks for each object consists of the bits that represent the objects that this object collides with i.e.:

    player.collsionBitMask         = 00001111 (15 in decimal) (the bits for all other objects are set)
    playerMissile.collisionBitMask = 00000111 (7) (all object EXCEPT screenEdge)
    enemy.collisonBitMask          = 00001111 (15) (everything)
    screenEdge.collisonBitMask     = 00000000 (0) (collides with nothing)
    

    现在我们考虑我们对哪些对象交互感兴趣.我们想知道什么时候:

    Now we think about which object interactions we are interested in. We want to know when:

    • 玩家和敌人的接触
    • 敌人和玩家的接触或敌人和玩家的导弹接触.

    这表示为:

    player.contactTestBitMask = 00000100 (4) (the enemy's categoryBitMask)
    enemy.contractTestBitMask = 00000011 (3) (the player's categoryBitMask combined with the missile's categoryBitMask))
    

    注意,如果你想知道A和B什么时候接触,只要让A的contactTestBitMask(CTBM)包含B的categoryBitMask就够了;您不必也将 B 的 CTBM 设置为 A 的类别.但是如果你想让它们相互反弹,那么每个对象的碰撞位掩码必须包括另一个的类别位掩码.如果 A 的 collisonbitMask 包含 B 的类别,但反之不包含,那么当 2 碰撞时,A 会弹开,而 B 不会受到影响.

    Note that if you want to know when A and B touch, it's enough just to have A's contactTestBitMask (CTBM) to include B's categoryBitMask; you don't have to have B's CTBM set to A's category too. But if you want them to bounce off each other, then each object's collisionBitMask must include the other's categoryBitMask. If A's collisonbitMask includes B's category, but not vice versa, then when the 2 collide, A will bounce off but B will be unaffected by it.

    对于您的具体示例,(类别掩码为 32,碰撞掩码为 11),categoryBitMask 为 32,即 00100000(仅保留 8 位).碰撞位掩码是 11,也就是 8 + 4 + 1,所以在二进制中这是00001101",这意味着它与 categoryBitMask 为 8、4 或 1 的对象发生碰撞,因此我假设您的场景中存在具有这些 categoryBitMask 的对象.

    For your specific example, (Category Mask to 32 and collision mask to 11), the categoryBitMask is 32 which is 00100000 (sticking with only 8 bits). The collisionBitMask is 11, which is 8 + 4 + 1 so in binary this is '00001101' which means it collides with objects with a categoryBitMask of 8, 4 or 1, so I assume that there are objects in your scene with these categoryBitMasks.

    默认情况下,所有东西都会从其他东西反弹,即碰撞位掩码都是 '1',即 b'111111..' 并且没有任何通知与其他任何东西的联系,即 contactTestBitMask 都是 '0's.

    By default everything bounces off everything else i.e. the collisionBitMask is all '1's i.e. b'111111..' and nothing notifies of contacts with anything else i.e. contactTestBitMask is all '0's.

    此外 - 所有这些都适用于物理体,而不是节点.

    Also - all of this applies to physicsBodies, not nodes.

    这篇关于什么是 Sprite Kit 的“类别掩码"?和“碰撞掩码"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆