什么是精灵套件的“类别掩码”和“碰撞面具”? [英] What are Sprite Kit's "Category Mask" and "Collision Mask"?

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

问题描述

我正在关注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这是什么类型的对象。
碰撞位掩码告诉Sprite Kit这个物体碰撞的物体(即撞击和反弹)。
ContactTest位掩码告诉Sprite-Kit你希望被通知的联系人,即当这个对象触及另一个对象时。

The Category bit mask tells Sprite-Kit what sort of object this is. The Collision bit mask tells Sprite Kit what objects this object collides with (i.e. will hit and bounce off). The ContactTest bit mask tells Sprite-Kit what contacts you want to be notified about i.e. when this object touches another object.

冲突由Sprite自动处理-Kit游戏引擎;联系人由您的代码处理 - 当您感兴趣的联系人发生时,您的代码( didBeginContact' for Swift 2,'didBegin(联系方式:调用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 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.

每个对象的碰撞位掩码包含代表此对象碰撞的对象的位with ie:

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的类别。但是如果你希望它们互相反弹,那么每个对象的collisionBitMask必须包含另一个的categoryBitMask。如果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位)。 collisionBitMask是11,它是8 + 4 + 1,所以在二进制中这是'00001101',这意味着它与categoryBitMask为8,4或1的对象发生冲突,所以我假设你的场景中有这些categoryBitMasks的对象。

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.

默认情况下,所有内容都会反弹,即collisionBitMask全部为'1'即b'111111 ..'并且没有任何通知联系人的任何其他内容,即contactTestBitMask全部为'0' 。

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.

此外 - 所有这些都适用于physicsBodies,而不是节点。

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

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

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