So I'm trying to use the Body Motion Capture sample code provided by Apple. Not using storyboard. Using SwiftUI content View with a button to go to an ARView. The app builds fine but when I use the button to switch to ARView, im getting a runtime error. Any ideas out to fix this ? Thanks :)
import UIKit
import RealityKit
import ARKit
import Combine
import SwiftUI
import Foundation
struct NavigationIndicator: UIViewControllerRepresentable {
typealias UIViewControllerType = ARVieww
func makeUIViewController(context: Context) -> ARVieww {
return ARVieww()
}
func updateUIViewController(_ uiViewController:
NavigationIndicator.UIViewControllerType, context:
UIViewControllerRepresentableContext) { }
}
class ARVieww: UIViewController, ARSessionDelegate {
var character: BodyTrackedEntity?
let characterOffset: SIMD3 = [0, 0, 0] // Offset the character by one meter to the left
let characterAnchor = AnchorEntity()
@IBOutlet var arView: ARView!
override func viewDidAppear(_ animated: Bool) {
print("yay")
super.viewDidAppear(animated)
arView.session.delegate = self
// If the iOS device doesn't support body tracking, raise a developer error for
// this unhandled case.
guard ARBodyTrackingConfiguration.isSupported else {
fatalError("This feature is only supported on devices with an A12 chip")
}
// Run a body tracking configration.
let configuration = ARBodyTrackingConfiguration()
arView.session.run(configuration)
arView.scene.addAnchor(characterAnchor)
// Asynchronously load the 3D character.
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadBodyTrackedAsync(named: "character/robot").sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Error: Unable to load model: (error.localizedDescription)")
}
cancellable?.cancel()
}, receiveValue: { (character: Entity) in
if let character = character as? BodyTrackedEntity {
// Scale the character to human size
character.scale = [1.0, 1.0, 1.0]
self.character = character
cancellable?.cancel()
} else {
print("Error: Unable to load model as BodyTrackedEntity")
}
})
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
for anchor in anchors {
guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
// Update the position of the character anchor's position.
let bodyPosition = simd_make_float3(bodyAnchor.transform.columns.3)
characterAnchor.position = bodyPosition + characterOffset
// Also copy over the rotation of the body anchor, because the skeleton's pose
// in the world is relative to the body anchor's rotation.
characterAnchor.orientation = Transform(matrix: bodyAnchor.transform).rotation
if let character = character, character.parent == nil {
// Attach the character to its anchor as soon as
// 1. the body anchor was detected and
// 2. the character was loaded.
characterAnchor.addChild(character)
}
}
}
}
When you post code, please format code in a more readable way and use code formatter, such as:
struct NavigationIndicator: UIViewControllerRepresentable {
typealias UIViewControllerType = ARVieww
func makeUIViewController(context: Context) -> ARVieww {
return ARVieww()
}
func updateUIViewController(_ uiViewController: NavigationIndicator.UIViewControllerType, context: UIViewControllerRepresentableContext) {
}
}
Where exactly do you get the crash ?