【问题标题】:RealityKit – Which Entity is intersecting with other EntityRealityKit – 哪个实体与其他实体相交
【发布时间】:2021-05-18 06:22:41
【问题描述】:
let height: Float = 1
let width: Float = 0.5
let box = MeshResource.generateBox(width: 0.02, height: height, depth: width)

这个盒子会有一个与当前相机位置相同的实时位置,在 AR World 中我会有多个不同形状的盒子,我想确定哪个对象与当前实时盒子相交。

我不能用位置匹配(最近的)来做到这一点。我真的很想知道接触/相交实时框的对象。

提前致谢。

【问题讨论】:

    标签: swift augmented-reality arkit realitykit


    【解决方案1】:

    您可以使用subscribe() 方法轻松做到这一点。以下代码为参考:

    (在 Reality Composer 中启用了两个对象的物理)

    import UIKit
    import RealityKit
    import Combine
    
    class ViewController: UIViewController {
        
        @IBOutlet var arView: ARView!
        var subscriptions: [Cancellable] = []
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            let boxScene = try! Experience.loadBox()
            arView.scene.anchors.append(boxScene)
            
            let floorEntity = boxScene.children[0].children[1]
    
            let subscribe = arView.scene.subscribe(to: CollisionEvents.Began.self,
                                                   on: floorEntity) { (event) in
                print("Collision Occured")
                print(event.entityA.name)
                print(event.entityB.name)
            }
            
            self.subscriptions += [subscribe]
        }
    }
    

    【讨论】:

    • 非常感谢,这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2018-06-07
    • 2015-10-03
    • 2013-08-18
    • 2022-07-24
    • 1970-01-01
    • 2013-08-05
    相关资源
    最近更新 更多