How do I spin and add a linear force to an Entity loaded from Reality Composer?

风流意气都作罢 提交于 2020-06-13 06:04:06

问题


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.


回答1:


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.





回答2:


To add a force you need the Entity to conform to HasPhysicsMotion. To see if your Entity imported from RC can have forces applied to it, check if (myEntity as? HasPhysics) returns nil or a value.

If that returns nil, make your own Entity subclass which has the HasPhysics protocol, and set your entity as a child of it. If you want it to collide with other things in your scene then you’ll also want HasCollision protocol.

All the things you mentioned can be achieved from this point!



来源:https://stackoverflow.com/questions/61163705/how-do-i-spin-and-add-a-linear-force-to-an-entity-loaded-from-reality-composer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!