gpt4 book ai didi

swift - ARkit 中未检测到 SceneKit 碰撞

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:56 30 4
gpt4 key购买 nike

我正在尝试使用 ARkit 和 SceneKit 构建游戏,但是在设置物理世界委托(delegate)和对象的物理体之后,委托(delegate)方法 didBeginContact 没有被调用。

这是我的物理体结构

struct CollisionCategory {
static let moltenBullet :Int = 4
static let iceShield :Int = 8
}

这是我设置物理体的方式

 let collisionShape = SCNPhysicsShape(node: node, options: nil)
self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: collisionShape)
self.physicsBody?.categoryBitMask = self.categoryMask
self.physicsBody?.contactTestBitMask = self.collisionMask
self.physicsBody?.collisionBitMask = self.collisionMask

我使用 self 是因为它是继承自 SCNNode 的自定义类的物理体,collisionMask 和 categoryMask 使用上述结构在构造函数中设置。

在 didBeginContact 中,我想打印一些东西,但没有任何反应。我已经将 SCNPhysicsContactDelegate 设置为

sceneView.scene.physicsWorld.contactDelegate

sceneView 是 ARSCNView。

我正确地看到了 physicsBody 的形状

SCNDebugOptionShowPhysicsShape

为什么会这样?

最佳答案

您的节点不会相互交互,因为它们属于不同的类别。以下是 collsionBitMask 根据 the documentation 工作的方式:

When two physics bodies contact each other, a collision may occur. SceneKit compares the body’s collision mask to the other body’s category mask by performing a bitwise AND operation. If the result is a nonzero value, then the body is affected by the collision.

contactBitMaskcategoryBitMask 的工作原理相同。

当 SceneKit 检查您的 physicsBody 是否有接触时,会发生这种情况:

object1.physicsBody.contactTestBitMask = 4 = 0100b
object2.physicsBody.categoryBitMask = 8 = 1000b
-----
(bitwise AND) 0000b = 0 -> no collision

定义类别的最佳方法是使用 OptionSet , Apple 实际上为您提供默认类别 SCNPhysicsCollisionCategory .

如果您希望两个节点或 physicsBody 进行交互,它们需要共享至少一个类别。否则它们不会发生碰撞。

您的示例可能如下所示:

struct CollisionCategory: OptionSet {
let rawValue: Int

static let moltenBullet = CollisionCategory(rawValue: 4)
static let iceWall = CollisionCategory(rawValue : 8)
}

然后你像这样分配你的类别:

class Bullet: SCNNode {
...

// The physics body-category is a bullet
self.physicsBody?.categoryBitMask = CollisionCategory.moltenBuller.rawValue
// This contacts with both other bullets and ice walls!
self.physicsBody?.contactBitMask = [CollsionCategory.moltenBullet, CollisionCategory.iceWall].rawValue
self.physicsBody?.collisionBitMask = self.physicsBody!.contactBitMask
}

关于swift - ARkit 中未检测到 SceneKit 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46091056/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com