【问题标题】:Within RealityKit, how can I make the world without friction?在 RealityKit 中,我怎样才能让世界没有摩擦?
【发布时间】:2020-09-18 00:42:55
【问题描述】:

我想让物理世界没有摩擦和阻尼。

我尝试将场景的重力设置为(0,0,0),并制作一个正方形和一个球,在点击时给予力量。我想让球永远移动,但它只是在一段时间后停止。

我怎样才能使实体摩擦为零?

【问题讨论】:

    标签: swift augmented-reality arkit realitykit


    【解决方案1】:

    将新的Physics Material 应用到您的模型实体。

    为此使用generate(friction:restitution:)type method

    static func generate(friction: Float = 0, 
                      restitution: Float = 0) -> PhysicsMaterialResource
    

    在哪里

    /*   the coefficient of friction is in the range [0, infinity]   */
    
    /*   and the coefficient of restitution is in the range [0, 1]   */
    

    这是一个代码:

    arView.environment.background = .color(.darkGray)
    
    let mesh = MeshResource.generateSphere(radius: 0.5)
    let material = SimpleMaterial()
    let model = ModelEntity(mesh: mesh,
                       materials: [material]) as (ModelEntity & HasPhysics)
        
    let physicsResource: PhysicsMaterialResource = .generate(friction: 0, 
                                                          restitution: 0)
        
    model.components[PhysicsBodyComponent] = PhysicsBodyComponent(
                                            shapes: [.generateSphere(radius: 0.51)],
                                              mass: 20,         // in kilograms
                                          material: physicsResource, 
                                              mode: .dynamic)
    
    model.generateCollisionShapes(recursive: true)
    
    let anchor = AnchorEntity()
    anchor.addChild(model)
    arView.scene.anchors.append(anchor)
    

    附:由于 RealityKit 中物理引擎的一些不完善,我想不可能创建一个永恒的弹跳。似乎下一个 RealityKit 的更新将修复物理引擎的缺陷。

    【讨论】:

    • 我已经尝试了你的建议。它确实改善了我试图做的事情,但在与墙壁碰撞 3~4 秒后,墙壁最终停止了。你知道为什么会这样吗?我设置了墙壁和球。
    • 请分享您的代码。通过 GitHub 或任何文件共享服务。
    • 请通过dropmefiles.com分享.rcproject场景
    • 有趣的是,每次我尝试,颠簸的数量都会改变。我找不到任何模式。
    • 我猜这是因为内存管理。在它的移动过程中,我在现场添加了更多的球,然后它的总动量得以保持。看起来如果场景没有变化,框架会故意将其关闭。
    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2021-05-27
    • 2017-09-07
    • 2011-06-14
    • 1970-01-01
    • 2020-12-19
    相关资源
    最近更新 更多