You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在RealityKit中设置低重力让模型缓慢下落

在RealityKit中设置低重力实现模型缓慢下落

在RealityKit里有两种常用方法实现低重力效果,不需要依赖线性冲量:

方法一:修改全局物理世界重力

RealityKit场景默认重力为SIMD3<Float>(0, -9.8, 0)(y轴向下9.8m/s²),你可以直接修改ARView场景的physicsWorld属性,设置自定义低重力值,这个修改会影响场景中所有开启物理效果的动态物体。

把以下代码添加到创建arView之后的位置:

// 将重力修改为y轴向下2m/s²的低重力效果
arView.scene.physicsWorld.gravity = SIMD3<Float>(0, -2.0, 0)

修改后的完整代码:

func makeUIView(context: Context) -> ARView {
    
    let arView = ARView(frame: .zero)
    
    // 修改全局物理世界重力为低重力
    arView.scene.physicsWorld.gravity = SIMD3<Float>(0, -2.0, 0)

    // Create a cube model
    let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.005)
    let material = SimpleMaterial(color: .gray, roughness: 2.0, isMetallic: true)
    let box = ModelEntity(mesh: mesh, materials: [material])
    box.transform.translation.y = 0.05
    box.generateCollisionShapes(recursive: true)
    box.physicsBody = PhysicsBodyComponent(massProperties: .default, material: .default, mode: .dynamic)
    

    // Create horizontal plane anchor for the content
    let anchor = AnchorEntity(.plane(.horizontal, classification: .any , minimumBounds: SIMD2<Float>(0.2, 0.2)))
    
    anchor.addChild(box)
    
    // Add the horizontal plane anchor to the scene
    arView.scene.anchors.append(anchor)

    return arView
}

方法二:给单个物体添加抵消重力的力

如果你只想让特定物体拥有低重力,不影响场景中其他物体,可以给目标物体添加PhysicsForceComponent,施加向上的力抵消部分默认重力。

假设物体默认质量为1kg,默认重力带来的向下力是9.8N,若要实现2m/s²的下落加速度,只需施加7.8N的向上力:

// 给立方体添加向上的抵消力,实现低重力
let offsetForce = SIMD3<Float>(0, 7.8, 0)
box.components[PhysicsForceComponent.self] = PhysicsForceComponent(force: offsetForce, relativeTo: nil)

将这段代码放在创建box并设置好物理体之后即可。

两种方法都能实现物体缓慢下落的效果,可根据需求选择全局修改或单个物体配置。

内容的提问来源于stack exchange,提问作者john doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 13:37:49