如何旋转并向从Reality Composer加载的实体添加线性力? [英] How do I spin and add a linear force to an Entity loaded from Reality Composer?
问题描述
我已经在Reality Composer中构建了一个场景,该场景的球使场景开始漂浮在空中.我正在尝试以编程方式将球掷出,同时使它旋转.
我试图通过Reality Composer中的行为来做到这一点,但是不能同时使两种行为同时起作用,而且一旦开始动画,球会立即掉落到地面上.
我的第二次尝试是放弃行为路线,但我尝试以编程方式执行此操作,但是我无法添加力,因为加载的球是Entity而不是ModelEntity.我在做什么错了?
我想旋转球,施加力并同时启用重力.
使用以下代码创建一个自定义类Physics
,该类符合RealityKit的物理协议(用于控制质量,速度和运动学/动力学模式).>
这是物理协议的顺应层次结构的样子:
HasPhysics: HasPhysicsBody, HasPhysicsMotion
|
|- HasCollision: HasTransform
这是一个代码:
import ARKit
import RealityKit
class Physics: Entity, HasPhysicsBody, HasPhysicsMotion {
required init() {
super.init()
self.physicsBody = PhysicsBodyComponent(massProperties: .default,
material: nil,
mode: .kinematic)
self.physicsMotion = PhysicsMotionComponent(linearVelocity: [0.1, 0, 0],
angularVelocity: [1, 3, 5])
}
}
然后在ViewController中创建该类的实例:
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let physics = Physics()
arView.backgroundColor = .darkGray
let boxAnchor = try! Experience.loadBox()
boxAnchor.steelBox?.scale = [5, 5, 5]
let boxEntity = boxAnchor.children[0].children[0].children[0]
boxEntity.name = "CUBE"
print(boxEntity)
let kinematicComponent: PhysicsBodyComponent = physics.physicsBody!
let motionComponent: PhysicsMotionComponent = physics.physicsMotion!
boxEntity.components.set(kinematicComponent)
boxEntity.components.set(motionComponent)
arView.scene.anchors.append(boxAnchor)
}
}
还要查看此帖子了解如何在没有自定义Physics
类的情况下实现物理学.
I've constructed a scene in Reality Composer that has a ball that starts the scene floating in the air. I'm attempting to programmatically throw the ball while simultaneously spinning it.
I tried to do this through behaviors in Reality Composer, but can't get both behaviors to work simultaneously, also, the ball immediately falls to the ground once I start the animation.
My second attempt was to forgo the behavior route and I attempted to do this programmatically, but I can not add a force, because the loaded ball is an Entity and not a ModelEntity. What am I doing wrong?
I want to spin the ball, apply a force and enable gravity simultaneously.
Use the following code to create a custom class Physics
that conforms to RealityKit's physics protocols (for controlling mass, velocity and kinematics/dynamics mode).
Here's how physics protocols' conforming hierarchy looks like:
HasPhysics: HasPhysicsBody, HasPhysicsMotion
|
|- HasCollision: HasTransform
Here's a code:
import ARKit
import RealityKit
class Physics: Entity, HasPhysicsBody, HasPhysicsMotion {
required init() {
super.init()
self.physicsBody = PhysicsBodyComponent(massProperties: .default,
material: nil,
mode: .kinematic)
self.physicsMotion = PhysicsMotionComponent(linearVelocity: [0.1, 0, 0],
angularVelocity: [1, 3, 5])
}
}
Then create an instance of that class in ViewController:
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let physics = Physics()
arView.backgroundColor = .darkGray
let boxAnchor = try! Experience.loadBox()
boxAnchor.steelBox?.scale = [5, 5, 5]
let boxEntity = boxAnchor.children[0].children[0].children[0]
boxEntity.name = "CUBE"
print(boxEntity)
let kinematicComponent: PhysicsBodyComponent = physics.physicsBody!
let motionComponent: PhysicsMotionComponent = physics.physicsMotion!
boxEntity.components.set(kinematicComponent)
boxEntity.components.set(motionComponent)
arView.scene.anchors.append(boxAnchor)
}
}
Also, look at THIS POST to find out how to implement physics without custom Physics
class.
这篇关于如何旋转并向从Reality Composer加载的实体添加线性力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!