我有一个球和一个平面(地板)在realityKit. 在飞机上,球落到了collide度.但球是falling-through的飞机.当球击中平面时,我想要一条打印声明"发生",但这也没有发生.

我的代码是:

struct ARViewContainer: UIViewRepresentable {
    
    let arView = ARView(frame: .zero)
    
    func makeUIView(context: Context) -> ARView {

        var subscriptions: [Cancellable] = []
        
        let anchor = AnchorEntity()
    
        //generate a plane
        let planeModel = ModelEntity(mesh: .generatePlane(width: 2.0, 
                                    depth: 2.0))
        as (Entity & HasCollision)
       
        planeModel.position = [0,-10,-1.5]
        planeModel.generateCollisionShapes(recursive: true)
        
        //generate a ball
        let ballModel = ModelEntity(mesh: .generateSphere(radius: 0.4))
                                 as (Entity & HasCollision & HasPhysicsBody)
        ballModel.generateCollisionShapes(recursive: true)
        ballModel.position = [0,0, -1.5]
        
        ballModel.physicsBody = .init()
        ballModel.physicsBody?.mode = .dynamic
        
        let sub = arView.scene.subscribe(to: CollisionEvents.Began.self,
                                on: ballModel) { _ in print("OCCURED!") }
        subscriptions.append(sub)
        
        anchor.addChild(ballModel)
        anchor.addChild(planeModel)
        arView.scene.addAnchor(anchor)
        
        return arView
    }
}

推荐答案

对撞机和对撞机

由于CollisionEvents的订阅,这个代码激活了物理并打印了"occured" N次:

import SwiftUI
import RealityKit
import Combine

struct ARViewContainer: UIViewRepresentable {
    
    @State var subscriptions: [AnyCancellable] = []
    let arView = ARView(frame: .zero)
    let anchor = AnchorEntity()
    
    func makeUIView(context: Context) -> ARView {

        // PLANE
        let planeModel = ModelEntity(mesh: .generatePlane(width: 2.0,
                                                          depth: 2.0)) 
                                                   as (Entity & HasPhysics)
       
        planeModel.position = [0,-1,-1.5]
        planeModel.generateCollisionShapes(recursive: false)
        planeModel.physicsBody = .init()
        planeModel.physicsBody?.mode = .static
        
        // BALL
        let ballModel = ModelEntity(mesh: .generateSphere(radius: 0.2), 
                               materials: [UnlitMaterial()])
                                                   as (Entity & HasPhysics)

        ballModel.position = [0, 1,-1.5]
        ballModel.generateCollisionShapes(recursive: false)            
        ballModel.physicsBody = .init()
        ballModel.physicsBody?.mode = .dynamic
        
        DispatchQueue.main.async {

            arView.scene.subscribe(to: CollisionEvents.Began.self,
                                   on: ballModel) { _ in
                print("OCCURED!")
                
            }.store(in: &subscriptions)
        }    
        anchor.addChild(ballModel)
        anchor.addChild(planeModel)
        arView.scene.addAnchor(anchor)           
        return arView
    }
    func updateUIView(_ view: ARView, context: Context) { }
}

struct ContentView : View {
    var body: some View {
        ARViewContainer().ignoresSafeArea()
    }
}

enter image description here

Swift相关问答推荐

无法创建MKAssociateRegion对象

swift:只要框架名称中有一个带有框架名称的公共实体,就指定框架中公共 struct 的路径

Swift 运算符中 inout 的行为

如何使用模型在 SwiftUI 的列表中进行搜索

Swift数据关系

是否可以限制 ExpressibleByIntegerLiteral struct 的初始值?

BLE 在 ESP32 和 Iphone 之间发送字符串

在Swift中如何将NSUrl转换为本地路径URL

如何从我的 macOS 应用程序打开 (.log) 文件?

如何在一个视图控制器中创建具有相同行为的多个集合视图?

为 ObservedObject 和 State 对象配置预览

如何在 Combine 合并运算符的输出上使用 eraseToAnyPublisher

UUID 哈希值是不确定的吗?

try 制作一个 Swift 扩展来替换字符串中的多次出现

SwiftUI Preview 不适用于 Core Data

如何有条件地格式化 SwiftUI 代码

Swift 2 中的新 @convention(c):我该如何使用它?

Swift 2.0 最低系统版本要求(部署目标)

<<错误类型>> 奇怪的错误

使用 JSONEncoder 对类型为 Codable 的变量进行编码