我正在尝试使用SCNPhysicsContactDelegate构建一些项目,但发现它太早发现了节点之间的联系。我需要它是100%正确和正确的。我附上了过早的联系人检测的屏幕截图。
他们的物理机构有我的代码:

nodeA代码:

        nodeA = SCNNode(geometry: SCNBox(width: 0.3, height: 0.3, length: 0.3, chamferRadius: 0.05))
        nodeA.position = SCNVector3(0, 3.3, 0)
        nodeA.geometry?.firstMaterial?.diffuse.contents = UIColor.red
        nodeA.physicsBody = SCNPhysicsBody.dynamic()
        nodeA.physicsBody?.physicsShape = SCNPhysicsShape(node: nodeA, options: nil)
        nodeA.physicsBody?.categoryBitMask = PhysicsCategories.nodeA
        nodeA.physicsBody?.contactTestBitMask = PhysicsCategories.nodeB
        nodeA.physicsBody?.isAffectedByGravity = false
        nodeA.name = "nodeA"
        nodeA.opacity = 0
        self.scene.rootNode.addChildNode(self.nodeA)


nodeB代码:
(nodeB已经存在,因此我仅添加物理实体)

        nodeB.physicsBody = SCNPhysicsBody.kinematic()
        nodeB.physicsBody?.physicsShape = SCNPhysicsShape(node: nodeB, options: nil)
        nodeB.physicsBody?.categoryBitMask = PhysicsCategories.nodeB
        nodeB.physicsBody?.contactTestBitMask = PhysicsCategories.nodeA


联系功能代码:

        func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
            scene.isPaused = true
        }


bad collision image

能否请您帮我找到解决方案?
我期待着你们的听证会。

谢谢雅库布

最佳答案

我找到了解决方案!这两行导致不精确的接触检测:

nodeA.physicsBody?.physicsShape = SCNPhysicsShape(node: nodeA, options: nil)nodeB.physicsBody?.physicsShape = SCNPhysicsShape(node: nodeB, options: nil)

问题是,当您使用节点属性创建SCNPhysicsShape()时,它是不精确的,但是当您使用节点几何形状创建它时,它们都完全正确且精确。
在我的情况下,解决方案如下所示(用这些行替换问题行):

nodeA.physicsBody?.physicsShape = SCNPhysicsShape(geometry: SCNBox(width: 0.3, height: 0.3, length: 0.3, chamferRadius: 0.05), options: nil)
nodeB.physicsBody?.physicsShape = SCNPhysicsShape(geometry: SCNBox(width: 0.1, height: 0.5, length: 1, chamferRadius: 0), options: nil)

10-07 19:11