【问题标题】:How do I make an entity a physics entity in RealityKit?如何使实体成为 RealityKit 中的物理实体?
【发布时间】:2020-03-29 17:07:15
【问题描述】:

我无法弄清楚如何使“球”实体成为物理 entity/body 并向其施加力。

// 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
    }
}

【问题讨论】:

    标签: swift augmented-reality arkit realitykit


    【解决方案1】:

    使用以下代码了解如何实现 RealityKit 的物理特性:


    特别注意:参与物理在 Reality Composer 中处于开启状态。


    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!
        }
    }
    


    另外,请查看THIS POST,了解如何使用自定义类实现 RealityKit 的物理。


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多