如何在移动模型的同时生成其碰撞形状?

问题描述 投票:0回答:1

我在 SwiftUI 中有 ModelEntity 并且它会移动。但问题是:当添加

generateCollisionShape
方法时,它不再移动了。

我想要碰撞效果以及移动实体 我怎样才能同时实现这两个目标?

modelEntityClone.generateCollisionShapes(recursive: true)

modelEntityClone.physicsBody = PhysicsBodyComponent(massProperties: .default, 
                                                          material: .default, 
                                                              mode: .dynamic)
swift swiftui realitykit
1个回答
2
投票

希望这个技巧一定能有所帮助。这种物理现象将永远持续下去。 ))

enter image description here

struct ARViewContainer: UIViewRepresentable {
    
    let ball = ModelEntity(mesh: .generateSphere(radius: 0.5))
    let anchor = AnchorEntity()
    
    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)

        ball.physicsBody = .init()
        ball.physicsBody?.massProperties.mass = 0

        ball.physicsMotion = .init()
        ball.physicsMotion?.linearVelocity.x = 5.0
        ball.physicsMotion?.angularVelocity.z = -.pi

        ball.position.x = -4.0
        ball.generateCollisionShapes(recursive: true)
        anchor.addChild(ball)

        anchor.position.z = -3.0
        arView.scene.addAnchor(anchor)
        return arView
    }

    func updateUIView(_ uiView: ARView, context: Context) { }
}
© www.soinside.com 2019 - 2024. All rights reserved.