我需要使用ARKit检测虚拟物体何时与实际世界物体接触。
有什么方法能够找出来吗?
我需要使用ARKit检测虚拟物体何时与实际世界物体接触。
有什么方法能够找出来吗?
首先,您需要创建一个碰撞类别结构体,符合 OptionSet
协议,并具有位集类型的属性:
import ARKit
struct Category: OptionSet {
let rawValue: Int
static let sphereCategory = Category(rawValue: 1 << 0)
static let targetCategory = Category(rawValue: 1 << 1)
}
然后在生命周期方法中设置一个 物理代理(Physics Delegate)
到 SCNPhysicsContactDelegate
协议:
class ViewController: UIViewController, SCNPhysicsContactDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
sceneView.scene = SCNScene()
sceneView.scene.physicsWorld.contactDelegate = self
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal]
sceneView.session.run(config)
}
}
SCNPhysicsContactDelegate
包含 3 个可选的 physicsWorld()
方法(稍后我们将使用第一个):
public protocol SCNPhysicsContactDelegate: NSObjectProtocol {
optional func physicsWorld(_ world: SCNPhysicsWorld,
didBegin contact: SCNPhysicsContact)
optional func physicsWorld(_ world: SCNPhysicsWorld,
didUpdate contact: SCNPhysicsContact)
optional func physicsWorld(_ world: SCNPhysicsWorld,
didEnd contact: SCNPhysicsContact)
}
接下来为球形碰撞器定义 categoryBitMask
和 collisionBitMask
:
fileprivate func createSphere() -> SCNNode {
var sphere = SCNNode()
sphere.geometry = SCNSphere(radius: 0.1)
sphere.physicsBody = .init(type: .kinematic,
shape: .init(geometry: sphere.geometry!,
options: nil))
sphere.physicsBody?.isAffectedByGravity = true
sphere.physicsBody?.categoryBitMask = Category.sphereCategory.rawValue
sphere.physicsBody?.collisionBitMask = Category.targetCategory.rawValue
sphere.physicsBody?.contactTestBitMask = Category.targetCategory.rawValue
return sphere
}
...并且为实际检测到的平面定义反向位掩码:
fileprivate func visualizeDetectedPlane() -> SCNNode {
var plane = SCNNode()
plane.geometry = SCNPlane(width: 0.7, height: 0.7)
plane.physicsBody = .init(type: .kinematic,
shape: .init(geometry: plane.geometry!,
options: nil))
plane.physicsBody?.isAffectedByGravity = false
plane.physicsBody?.categoryBitMask = Category.targetCategory.rawValue
plane.physicsBody?.collisionBitMask = Category.sphereCategory.rawValue
plane.physicsBody?.contactTestBitMask = 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!")
}
}