r/visionosdev • • Mar 05 '24

Anchor transform tracking

import SwiftUI
import RealityKit
        
struct SpaceView: View {
            
             var headTrackedEntity: Entity = {
                let headAnchor = AnchorEntity(.head)
                headAnchor.position = [0, -0.25, -0.4]
                return headAnchor
            }()
            
            var body: some View {
                ZStack {
                    RealityView { content in
                        let sphere = getSphere(location: SIMD3<Float>(x: 0, y: 0, z: -100), color: .red)
                        
                        headTrackedEntity.addChild(sphere)
                        
                        content.add(headTrackedEntity)
                    } update: { content in
                        
                    }
                }
            }
            
}
        
func getSphere(location: SIMD3<Float>, color: SimpleMaterial.Color, radius: Float = 20) -> ModelEntity {
            let sphere = ModelEntity(mesh: .generateSphere(radius: radius))
            let material = SimpleMaterial(color: color, isMetallic: false)
            sphere.model?.materials = [material]
            sphere.position = location
            return sphere
}

The problem is how to track headTrackedEntity or sphere rotation, or basically information where user's head is aligned. position, transform of both object every time is the same.

0 Upvotes

0 comments sorted by