如何在Realitykit中使实体成为物理实体?

时间:2019-12-04 19:55:31

标签: swift augmented-reality game-physics realitykit

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

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 个答案:

答案 0 :(得分:0)

使用以下代码找出如何实现RealityKit的物理原理:

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的物理原理。


enter image description here