Swift SceneKit:为什么它过早地检测到节点之间的接触?
Swift SceneKit: Why does it detect contact between nodes too early?
我正在尝试使用 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)
我正在尝试使用 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)