Can’t Figure Out How to Get My Earth Entity to Rotate on its Axis

I can‘t Figure Out How to Get My Earth Entity to Rotate on its Axis. This is a follow up post from a previous Apple Developer forum post.

How would I have the earth (parent) entity rotate CCW underneath the orbiting starship child? I tried adding the following code block to the RealityView but it is not working:

if let rotatingEarth = starshipEntity.findEntity(named: "Earth") {
                rotatingEarth.transform.rotation = simd_quatf.init(angle: 360, axis: SIMD3(x: 0, y: 1, z: 0))
                
                if let animation = try? AnimationResource.generate(with: rotatingEarth as! AnimationDefinition) {
                    rotatingEarth.playAnimation(animation)
                }

            }

Any advice on getting the earth to rotate? I tried reviewing the Hello World WWDC23 project code, but I was unable to understand the complexity and how that sample project got the earth to rotate.

i want to do this for visionOS 1.2. I realize there are some new animation and possible other capabilities coming up in vision 2.0 but I want to try to address this issue in the current released visionOS version.

Hello World uses Entity Component System (ECS) to rotate the Earth. Understanding RealityKit’s modular architecture is a good overview of ECS. It's also covered the WWDC23 session Build spatial experiences with RealityKit.

In this case, you can reuse Hello World's RotationComponent and RotationSystem. This is done in three steps: first copy World/Systems/RotationSystem.swift to your project, next register the component and system, finally add the rotation component to the Earth entity.

Register the component and system in your app's initializer.

init() {
    RotationComponent.registerComponent()
    RotationSystem.registerSystem()
}

Add the component to your Earth entity. Optionally, you can pass speed or axis to the component. This example uses the default values.

earth.components.set(RotationComponent())

That's it! Your Earth should now rotate.

Can’t Figure Out How to Get My Earth Entity to Rotate on its Axis
 
 
Q