I have successfully loaded an USDZ file to my scene. Now I want to add motion the ModelEntity. I have added PhysicsMotionComponent but it is not working. After Loading model it is static as usual. No movement. How to give an entity motion in RealityKit?
When I see the components I see that components are added. But The entity in not moving. What am I doing wrong?
My codes:
import SwiftUI
import RealityKit
import ARKit
import Combine
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arVIew = ARView(frame: .zero)
context.coordinator.arVIew = arVIew
arVIew.session.delegate = context.coordinator
let config = ARWorldTrackingConfiguration()
arVIew.session.run(config, options: [.resetTracking,.removeExistingAnchors])
let anchorEntity = AnchorEntity()
loadASingleModel(name:"toy_biplane") { entity in
if let entity = entity {
let kinematics: PhysicsBodyComponent = .init(massProperties: .default,
material: nil,
mode: .kinematic)
let motion: PhysicsMotionComponent = .init(linearVelocity: [0.1 ,0, 0],
angularVelocity: [3, 3, 3])
entity.components.set(kinematics)
entity.components.set(motion)
anchorEntity.addChild(entity)
//anchorEntity.transform.matrix.columns.3.z = -1.0
arVIew.scene.anchors.append(anchorEntity)
print("Model Added: ",entity.name)
}else{
print("No entity avaible")
}
}
return arVIew
}
// MARK: - AsychLoading working
func loadASingleModel(name:String,completion:@escaping (_ model:ModelEntity?)->Void){
var cancellable:AnyCancellable?
cancellable = Entity.loadModelAsync(named:name)
.sink(receiveCompletion: { handler in
if case let .failure(error) = handler {
print("Unable to load a model due to error \(error)")
}
cancellable?.cancel()
completion(nil)
}, receiveValue: { [self] (model: ModelEntity?) in
if let model = model {
cancellable?.cancel()
print("Congrats! Model is successfully loaded!")
completion(model)
}
})
}
func updateUIView(_ uiView: ARView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(arViewContainer: self)
}
class Coordinator:NSObject{
var parent:ARViewContainer
var arVIew:ARView?
init(arViewContainer:ARViewContainer){
parent = arViewContainer
}
}
}
extension ARViewContainer.Coordinator:ARSessionDelegate{
func session(_ session: ARSession, didUpdate frame: ARFrame) {
}
}