Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 阿基特&x2013;与真实世界对象的碰撞_Swift_Augmented Reality_Scenekit_Arkit - Fatal编程技术网

Swift 阿基特&x2013;与真实世界对象的碰撞

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

我需要使用ARKit检测虚拟对象何时与真实世界对象接触


有什么方法可以找到它吗?

首先,您需要创建一个符合
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!")
    }
}