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

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

问题描述

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

//我使用 UIKit 作为用户界面和 RealityKit +//在 Reality Composer 中为增强现实和代码制作的模型导入 RealityKit导入 ARKit类视图控制器:UIViewController {var ball: (Entity & HasPhysics)?{尝试?Entity.load(named: "golfball") as?实体&拥有物理学}@IBOutlet var arView:ARView!//引用主屏幕上的立即播放按钮@IBAction func playNow(_ sender: Any) { }//引用 AR 视图中的滑块 - 此滑块将用于//控制摆动的力量.滑块值范围从 10% 到//100% 的摆动功率,默认值为 55%.用户将拥有//在游戏中获得经验以知道使用多少功率.@IBAction func 滑块(_ 发件人:任意){ }//以下代码将在视图加载时触发覆盖 func viewDidLoad() {super.viewDidLoad()//定义 Anchor - 它寻找一个 0.3 x .3 的平面//大约一英尺一英尺 - 在这个表面上,它锚定//点击时的高尔夫球场和球让锚=锚实体(平面:.水平,minimumBounds:[0.3,0.3])//将锚点放置在场景中arView.scene.addAnchor(锚点)//定义我的高尔夫球场实体 - 使用模型实体所以它//参与场景的物理让实体=尝试?ModelEntity.load(named: "golfarnew")//定义球实体 - 再次使用模型实体所以它//参与场景的物理让球=尝试?ModelEntity.load(named: "golfball")//加载我的高尔夫球场实体anchor.addChild(实体!)//加载高尔夫球anchor.addChild(球!)//在球的位置对球施加一个力,然后//力是相对于球的ball.physicsBody(SIMD3(1.0, 1.0, 1.0), at: ball.position, relativeTo: ball)//声音,将物理身体添加到球,iPad 用于射击方向,//将滑块连接到冲击力}}

解决方案

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

导入 ARKit导入 RealityKit类视图控制器:UIViewController {@IBOutlet var arView:ARView!覆盖 func viewWillAppear(_ 动画:布尔){super.viewWillAppear(动画)让 boxScene = 尝试!经验.loadBox()让 secondBoxAnchor = 试试!经验.loadBox()让 boxEntity = boxScene.steelBox 为!(Entity & HasPhysics)让运动学: PhysicsBodyComponent = .init(massProperties: .default,材料:无,模式:.kinematic)让运动: PhysicsMotionComponent = .init(linearVelocity: [0.1 ,0, 0],角速度:[3, 3, 3])boxEntity.components.set(运动学)boxEntity.components.set(motion)让锚= AnchorEntity()anchor.addChild(boxEntity)arView.scene.addAnchor(锚点)arView.scene.addAnchor(secondBoxAnchor)print(boxEntity.isActive)//实体必须是活动的!}}


另外,看看

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

解决方案

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天全站免登陆