如何旋转并向从 Reality Composer 加载的实体添加线性力? [英] How do I spin and add a linear force to an Entity loaded from Reality Composer?

查看:22
本文介绍了如何旋转并向从 Reality Composer 加载的实体添加线性力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Reality Composer 中构建了一个场景,其中有一个球,可以启动漂浮在空中的场景.我试图在同时旋转球的同时以编程方式扔球.

我尝试通过 Reality Composer 中的行为来做到这一点,但无法让两种行为同时工作,而且,一旦我开始动画,球会立即落到地上.

我的第二次尝试是放弃行为路线,我尝试以编程方式执行此操作,但我无法添加力,因为加载的球是实体而不是模型实体.我做错了什么?

我想旋转球,同时施加一个力并启用重力.

解决方案

使用以下代码创建一个自定义类 Physics 符合 RealityKit 的物理协议(用于控制质量、速度和运动学/动力学模式).

物理协议的符合层次结构如下所示:

HasPhysics: HasPhysicsBody, HasPhysicsMotion||- HasCollision: HasTransform

这是一个代码:

导入 ARKit导入 RealityKit物理类:实体、HasPhysicsBody、HasPhysicsMotion {必需的 init() {超级初始化()self.physicsBody = PhysicsBodyComponent(massProperties: .default,材料:无,模式:.kinematic)self.physicsMotion = PhysicsMotionComponent(linearVelocity: [0.1, 0, 0],角速度:[1, 3, 5])}}

然后在 ViewController 中创建该类的实例:

class ViewController: UIViewController {@IBOutlet var arView:ARView!覆盖 func viewDidLoad() {super.viewDidLoad()让物理 = 物理()arView.backgroundColor = .darkGray让 boxAnchor = 尝试!经验.loadBox()boxAnchor.steelBox?.scale = [5, 5, 5]让 boxEntity = boxAnchor.children[0].children[0].children[0]boxEntity.name = "立方体"打印(框实体)让 kinematicComponent: PhysicsBodyComponent =physics.physicsBody!让motionComponent:PhysicsMotionComponent =physics.physicsMotion!boxEntity.components.set(kinematicComponent)boxEntity.components.set(motionComponent)arView.scene.anchors.append(boxAnchor)}}


另外,看看

I've constructed a scene in Reality Composer that has a ball that starts the scene floating in the air. I'm attempting to programmatically throw the ball while simultaneously spinning it.

I tried to do this through behaviors in Reality Composer, but can't get both behaviors to work simultaneously, also, the ball immediately falls to the ground once I start the animation.

My second attempt was to forgo the behavior route and I attempted to do this programmatically, but I can not add a force, because the loaded ball is an Entity and not a ModelEntity. What am I doing wrong?

I want to spin the ball, apply a force and enable gravity simultaneously.

解决方案

Use the following code to create a custom class Physics that conforms to RealityKit's physics protocols (for controlling mass, velocity and kinematics/dynamics mode).

Here's how physics protocols' conforming hierarchy looks like:

HasPhysics: HasPhysicsBody, HasPhysicsMotion
                   |
                   |- HasCollision: HasTransform

Here's a code:

import ARKit
import RealityKit

class Physics: Entity, HasPhysicsBody, HasPhysicsMotion {

    required init() {
        super.init()

        self.physicsBody = PhysicsBodyComponent(massProperties: .default, 
                                                      material: nil, 
                                                          mode: .kinematic)

        self.physicsMotion = PhysicsMotionComponent(linearVelocity: [0.1, 0, 0], 
                                                   angularVelocity: [1, 3, 5])
    }
}

Then create an instance of that class in ViewController:

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let physics = Physics()
        arView.backgroundColor = .darkGray

        let boxAnchor = try! Experience.loadBox()
        boxAnchor.steelBox?.scale = [5, 5, 5]

        let boxEntity = boxAnchor.children[0].children[0].children[0]
        boxEntity.name = "CUBE"

        print(boxEntity)

        let kinematicComponent: PhysicsBodyComponent = physics.physicsBody!
        let motionComponent: PhysicsMotionComponent = physics.physicsMotion!

        boxEntity.components.set(kinematicComponent)
        boxEntity.components.set(motionComponent)

        arView.scene.anchors.append(boxAnchor)
    }
}


Also, look at THIS POST to find out how to implement physics without custom Physics class.


这篇关于如何旋转并向从 Reality Composer 加载的实体添加线性力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆