How do I make an entity a physics entity in RealityKit?

﹥>﹥吖頭↗ 提交于 2020-05-26 09:24:11

问题


I am not able to figure out how to make the "ball" entity a physics entity/body and apply a force to it.

// I'm using UIKit for the user interface and RealityKit + 
// the models made in Reality Composer for the Augmented reality and Code

import RealityKit
import ARKit

class ViewController: UIViewController {

    var ball: (Entity & HasPhysics)? {      
        try? Entity.load(named: "golfball") as? Entity & HasPhysics
    }

    @IBOutlet var arView: ARView!

    // referencing the play now button on the home screen  
    @IBAction func playNow(_ sender: Any) { }

    // referencing the slider in the AR View - this slider will be used to 
    // control the power of the swing. The slider values range from 10% to 
    // 100% of swing power with a default value of 55%. The user will have 
    // to gain experience in the game to know how much power to use.
    @IBAction func slider(_ sender: Any) { } 

    //The following code will fire when the view loads   
    override func viewDidLoad() {
        super.viewDidLoad()

        // defining the Anchor - it looks for a flat surface .3 by .3 
        // meters so about a foot by a foot - on this surface, it anchors 
        // the golf course and ball when you tap
        let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.3, 0.3])

        // placing the anchor in the scene
        arView.scene.addAnchor(anchor)     

        // defining my golf course entity - using modelentity so it 
        // participates in the physics of the scene
        let entity = try? ModelEntity.load(named: "golfarnew")

        // defining the ball entity - again using modelentity so it 
        // participates in the physics of the scene
        let ball = try? ModelEntity.load(named: "golfball")

        // loading my golf course entity        
        anchor.addChild(entity!)

        // loading the golf ball      
        anchor.addChild(ball!)

        // applying a force to the ball at the balls position and the 
        // force is relative to the ball            
        ball.physicsBody(SIMD3(1.0, 1.0, 1.0), at: ball.position, relativeTo: ball)

        // sounds, add physics body to ball, iPad for shot direction, 
        // connect slider to impulse force
    }
}

回答1:


Use the following code to find out how to implement a RealityKit's physics:

import ARKit
import RealityKit

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let boxScene = try! Experience.loadBox()     
        let secondBoxAnchor = try! Experience.loadBox()

        let boxEntity = boxScene.steelBox as! (Entity & HasPhysics)

        let kinematics: PhysicsBodyComponent = .init(massProperties: .default, 
                                                           material: nil, 
                                                               mode: .kinematic)

        let motion: PhysicsMotionComponent = .init(linearVelocity: [0.1 ,0, 0], 
                                                  angularVelocity: [3, 3, 3])

        boxEntity.components.set(kinematics)
        boxEntity.components.set(motion) 

        let anchor = AnchorEntity()
        anchor.addChild(boxEntity)
        arView.scene.addAnchor(anchor)
        arView.scene.addAnchor(secondBoxAnchor)

        print(boxEntity.isActive)         // Entity must be active!
    }
}


Also, look at THIS POST to find out how to implement RealityKit's physics with a custom class.




来源:https://stackoverflow.com/questions/59183770/how-do-i-make-an-entity-a-physics-entity-in-realitykit

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