簡體   English   中英

如何讓飛機與 RealityKit 中的 object 相撞

[英]How can I let the plane collide with an object in RealityKit

我在realityKit中有一個球和一個平面(地板)。 球落下並且必須在平面上collide 但是球正在through平面上falling來。 當球擊中飛機時,我想要打印聲明“OCCURED”,但這也沒有發生。

我的代碼:

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()
    }
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM