【问题标题】:Applying Physics on USDZ model in RealityKit在 RealityKit 中的 USDZ 模型上应用物理
【发布时间】:2021-07-14 22:06:30
【问题描述】:

我正在尝试从 USDZ 文件加载实体,然后将物理应用到它的 ModelEntity

我正在使用 Apple 画廊中的 toy_robot_vintage 模型。它包含我想要使用的动画,因此我正在加载Entity。我想通了,我不能将物理添加到Entity,因为它不符合HasPhysicsBody,所以我得到vintage_robot_animRig_model_body 并在其上应用PhysicsBodyComponent。但是,它不起作用。我应该将物理应用到哪个ModelEntity,以及应该如何将物理应用到层次结构中的不同ModelEntity 应该反映我的模型?

我也尝试添加一个自定义的符合协议的Entity子类,但是在我创建一个对象之后,我没有看到它符合它。

这是我目前所拥有的:

let modelEntity = robotEntity.findEntity(named: "vintage_robot_animRig_model_body")
let physics = PhysicsBodyComponent(massProperties: .default, material: .default, mode: .dynamic)   
modelEntity?.components.set(physics)

【问题讨论】:

    标签: swift augmented-reality realitykit usdz


    【解决方案1】:

    使用generateCollisionShapes() 实例方法来激活物理并运行碰撞检测:

    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        let robot = try! Entity.loadAnchor(named: "robot")
        robot.scale = [0.03, 0.03, 0.03]
        let headEntity = robot.findEntity(named: "vintage_robot_animRig_model_head")
        
        robot.generateCollisionShapes(recursive: true)
        
        let physics = PhysicsBodyComponent(massProperties: .default, 
                                                 material: .default, 
                                                     mode: .dynamic)
        headEntity?.components.set(physics)
        arView.scene.anchors.append(robot)
        
        return arView
    }
    

    【讨论】:

    • 成功了,谢谢,你为社区做的很棒!
    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2020-12-17
    • 2022-08-03
    • 2020-12-18
    相关资源
    最近更新 更多