【问题标题】:RealityKit – How to prevent Entities from overlapping?RealityKit – 如何防止实体重叠?
【发布时间】:2022-05-20 21:33:59
【问题描述】:

我正在尝试使用 RealityKit 创建 AR 体验,但我发现默认情况下,实体会在用户交互移动时相互移动并重叠。

我想防止对象重叠并相互进入,这样当它们被用户移动时,它们只是撞击/反弹而不会重叠。

我正在从 RealityComposer 文件中加载实体并将它们添加到场景中(在 catch 块中,其他未在此简化版本中显示):

let entity = try Experience.loadBallSort()
anchorEntity.addChild(entity) 
// anchorEntity is an AnchorEntity that is already attached to the scene

我正在使用像这样的默认手势来启用用户交互,这就是导致对象重叠的方式,因为它们一旦触摸就不会停止:

arView.installGestures([.rotation, .translation], for: entity)

在 Reality Composer 中,我启用了具有静态运动类型的物理,以及每个对象的默认物理材质/碰撞形状。我也尝试过使用generateCollisionShapes,但它不会改变碰撞的行为:

entity.generateCollisionShapes(recursive: true)

如何防止实体在 RealityKit 中重叠?

【问题讨论】:

  • 嗨!你有想过这个吗?我一直在尝试做完全相同的事情,而且我已经用完了文档/示例来尝试... :)
  • 您好,您找到解决方案了吗?谢谢!

标签: swift augmented-reality arkit realitykit reality-composer


【解决方案1】:

为了实现这样的场景,让我们使用 2 个对象 - 一个是 dynamic,另一个是 kinematic

PhysicsBodyMode.dynamic

  • 力量和碰撞控制着身体的运动。

PhysicsBodyMode.kinematic

  • 用户控制身体运动。这种类型的物理实体不受力或碰撞的影响,但在移动时可能会导致影响其他实体的碰撞。


代码:

var arView = ARView(frame: .zero)
arView.frame = self.view.frame
self.view.addSubview(arView)

let scene = try! Experience.loadModels()

// Kinematic
let red = scene.redBox!.children[0] as! (Entity & 
                                         HasCollision & 
                                         HasPhysicsBody)
red.physicsBody = .init()
red.physicsBody?.massProperties.mass = 5
red.physicsBody?.mode = .kinematic
red.generateCollisionShapes(recursive: true)
arView.installGestures([.translation], for: red)

// Dynamic    
let green = scene.greenCube!.children[0] as! (Entity & 
                                              HasCollision & 
                                              HasPhysicsBody)
green.physicsBody = .init()
green.physicsBody?.massProperties.mass = 5
green.physicsBody?.mode = .dynamic
green.generateCollisionShapes(recursive: true)


附言

不要在 Reality Composer 中应用物理,而是在 RealityKit 中以编程方式应用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2013-01-15
    • 2010-12-26
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多