HelloWorld example: How to make the globe in immersive space interactable?

In the HelloWorld sample, there is an immersive view with a globe in it. It spins, but the user cannot spin it themselves.

I have looked at the volumetric window, where the globe can be interacted with, but if I understand it correctly, this works because the whole RealityView is being rotated if the user performs a drag gesture.

How could the same be accomplished for an entity inside a RealityView, in this case the globe inside the immersive view? If I just apply the dragRotation modifier, it will rotate the entire RealityView, which yields a strange result, as the globe is not centered on the world origin here, so it spins around the users head.

Is there a way to either translate the entire RealityView and then spin it, or just spin an entity inside it (the globe) on user interaction?

In Unity, I would just use another gameobject as a parent to the globe, translate it, and let the user spin it.

Replies

To interact with an entity you need to

generate collision shapes:

 let model = try await ModelEntity(named: modelName)
  model.generateCollisionShapes(recursive: true)
  model.components.set([InputTargetComponent()])

add a rotate gesture to your reality view

.gesture(RotateGesture().targetedToAnyEntity().onChanged({ value in
    //access the entity targeted with value.entity and the angle with value.rotation.radians
        }))

On a simulator press the option key and rotate.

  • Awesome, will try this out!

  • Tried it out, but didn't work as I expected. I tried this code:

        .gesture(RotateGesture().targetedToAnyEntity().onChanged({ value in
                guard let angle = earthEntity?.transform.rotation.angle else { return }
                earthEntity?.transform.rotation = simd_quatf(Rotation3D(angle: Angle2D(radians: value.rotation.radians), axis: .y))
            }))
    

    but it starts the rotation at 0. If I try to add the current rotation of the earth in radians instead, the globe rotates weirdly.

  • I also would like to use the drag gesture instead, like it is in the DragRotationModifier of the sample. But I can't get this modifier adapted so it rotates the sphere locally and not globally, which only works if the pivot point is at [0,0,0]. Is there any tutorials out there how to work with this 3D stuff?