Swift 阿基特&x2013;与真实世界对象的碰撞
我需要使用ARKit检测虚拟对象何时与真实世界对象接触Swift 阿基特&x2013;与真实世界对象的碰撞,swift,augmented-reality,scenekit,arkit,Swift,Augmented Reality,Scenekit,Arkit,我需要使用ARKit检测虚拟对象何时与真实世界对象接触 有什么方法可以找到它吗?首先,您需要创建一个符合OptionSet协议并具有位集类型属性的冲突类别结构: import ARKit struct Category: OptionSet { let rawValue: Int static let sphereCategory = Category(rawValue: 1 << 0) static let targetCategory = C
有什么方法可以找到它吗?首先,您需要创建一个符合
OptionSet
协议并具有位集类型属性的冲突类别结构:
import ARKit
struct Category: OptionSet {
let rawValue: Int
static let sphereCategory = Category(rawValue: 1 << 0)
static let targetCategory = Category(rawValue: 1 << 1)
}
SCNPhysicsContactDelegate
包含3个可选的physicsWorld()方法(稍后我们将使用第1个):
在此之后,为球体碰撞器定义categoryBitMask
和collisionBitMask
:
fileprivate func createSphere() -> SCNNode {
var sphere = SCNNode()
sphere.geometry = SCNSphere(radius: 0.1)
sphere.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
sphere.physicsBody?.isAffectedByGravity = true
sphere.physicsBody?.categoryBitMask = Category.sphereCategory.rawValue
sphere.physicsBody?.collisionBitMask = Category.targetCategory.rawValue
return sphere
}
…并按与实际检测平面相反的顺序定义位掩码:
fileprivate func visualizeDetectedPlane() -> SCNNode {
var plane = SCNNode()
plane.geometry = SCNPlane(width: 0.7, height: 0.7)
plane.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
plane.physicsBody?.isAffectedByGravity = false
plane.physicsBody?.categoryBitMask = Category.targetCategory.rawValue
plane.physicsBody?.collisionBitMask = Category.sphereCategory.rawValue
return plane
}
只有在将SCN平面添加到实际检测平面并将SCN添加到SCN场景中时,才能使用physicsWorld(u:didbeagin:)
实例方法检测碰撞:
抱歉,无法理解什么是真实世界检测到的飞机
fileprivate func createSphere() -> SCNNode {
var sphere = SCNNode()
sphere.geometry = SCNSphere(radius: 0.1)
sphere.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
sphere.physicsBody?.isAffectedByGravity = true
sphere.physicsBody?.categoryBitMask = Category.sphereCategory.rawValue
sphere.physicsBody?.collisionBitMask = Category.targetCategory.rawValue
return sphere
}
fileprivate func visualizeDetectedPlane() -> SCNNode {
var plane = SCNNode()
plane.geometry = SCNPlane(width: 0.7, height: 0.7)
plane.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
plane.physicsBody?.isAffectedByGravity = false
plane.physicsBody?.categoryBitMask = Category.targetCategory.rawValue
plane.physicsBody?.collisionBitMask = Category.sphereCategory.rawValue
return plane
}
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
if contact.nodeA.physicsBody?.categoryBitMask ==
Category.targetCategory.rawValue ||
contact.nodeB.physicsBody?.categoryBitMask ==
Category.targetCategory.rawValue {
print("BOOM!")
}
}