如何在RealityKit中使实体成为物理实体? [英] How do I make an entity a physics entity in RealityKit?

查看:183
本文介绍了如何在RealityKit中使实体成为物理实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何使球"实体成为物理entity/body并对其施加力.

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

推荐答案

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

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.


这篇关于如何在RealityKit中使实体成为物理实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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