简体   繁体   English

SpriteKit 避免两个节点之间的碰撞

[英]SpriteKit avoid collision between two nodes

Desired behaviour期望的行为

在此处输入图片说明

  • The red node (players arm) is connected to the blue one with a pinned joint.红色节点(玩家手臂)通过固定关节连接到蓝色节点。
  • The red node has a physicsBody because: 1) it should move if the blue node does and 2) it should collide with the rest of the world but not with the blue shape红色节点有一个physicsBody因为:1)如果蓝色节点移动,它应该移动;2)它应该与世界其他地方碰撞,但不与蓝色形状碰撞
  • Vice versa: The blue node (players body) should collide with the rest of the world – but obviously not with the red node.反之亦然:蓝色节点(玩家身体)应该与世界其他地方发生碰撞——但显然不会与红色节点发生碰撞。

Problem问题

Nodes and joint are setup fine (see left), but the physicsBody do interact with each other (right):节点和关节设置良好(见左图),但physicsBody确实相互交互(右图):

在此处输入图片说明

Please note: In order to get the joint right I've had to add a child node (circle) to the red shape (players arm).请注意:为了使关节正确,我必须向红色形状(玩家手臂)添加一个子节点(圆圈)。 So we're talking about three nodes in total here.所以我们在这里总共讨论三个节点。

Recent attempt最近的尝试

Tried a bunch of things.尝试了一堆东西。 Following Apples' documentation and this thread I learned that this could be solved with the correct combination of categoryBitMask and collisionBitMask .按照Apples 的文档这个线程,我了解到这可以通过categoryBitMaskcollisionBitMask的正确组合来解决。

BitMasks:位掩码:

struct Category {
    static let none: UInt32 = 0
    static let all: UInt32 = UInt32.max
    static let playerBody: UInt32 = 0b1 // 1
    static let playerArm: UInt32 = 0b10 // 2
    static let regularObject: UInt32 = 0b100 // 4
}

🔵 Blue Shape: 🔵 蓝色形状:

body.categoryBitMask = Category.playerBody
body.collisionBitMask = Category.all

🔴 Red Shape (as well as its child node for the joint): 🔴红色形状(以及它的关节子节点):

body.categoryBitMask = Category.playerArm
body.collisionBitMask = Category.none

... but this did not solve the problem. ……但这并没有解决问题。

Struggeling for three days in a row now.现在连续三天挣扎。 I know this sort of question got already asked a numerous of times but none of them lead me to the right direction.我知道这种问题已经被问过很多次了,但没有一个能引导我走向正确的方向。 Really appreciate any advice!真的很感激任何建议!

This is from Apple documentation: collisionBitMask (but for Scenekit, maybe it is the same in Spritekit)这是来自 Apple 文档:collisionBitMask(但对于 Scenekit,也许在 Spritekit 中是相同的)

The value for this key is an NSNumber object containing an NSUInteger value.这个键的值是一个包含 NSUInteger 值的 NSNumber 对象。 SceneKit tests for contacts only with physics bodies whose categoryBitMask property overlaps with this bit mask. SceneKit 仅测试其 categoryBitMask 属性与此位掩码重叠的物理体的接触。 The default value is all, specifying that searches should test all physics bodies regardless of their category .默认值为 all ,指定搜索应测试所有物理体,而不管其类别如何

so you should not use Category.all所以你不应该使用 Category.all

Turns out that I've unwrapped the wrong physicsBody for the red shape so code changes affected the wrong nodes 🤦事实证明,我为红色形状打开了错误的physicsBody ,因此代码更改影响了错误的节点 🤦

It basically works now the way I've described it in the original question.它现在基本上按照我在原始问题中描述的方式工作。

However—if you're new to this topic like me: the very best explanation I could find is this one (especially the »ask your self« part).但是——如果你和我一样刚接触这个话题:我能找到的最好的解释就是这个(尤其是“问问你自己”部分)。 Happy coding everyone!祝大家编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM