InstallGestures for RCProject

Hi,

Please could you help me to install rotation and translation gestures for an RCProject? I have the code below but this crashes when running. Any help is very much appreciated.

import SwiftUI
import RealityKit
import ARKit

struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> ARView {
    
    let arView = ARView(frame: .zero)
    
    // Load Model3 scene
    let anchor = try! Model3.loadScene()
    
    // Add the anchor to the scene
    arView.scene.anchors.append(anchor)
    
    
    anchor.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation, .translation], for: anchor as! HasCollision)
    
    
    anchor.actions.tap.onAction = HandleTapOnEntity(_:)
    
    return arView
    
}

func updateUIView(_ uiView: ARView, context: Context) {}

func HandleTapOnEntity(_ entity: Entity?) {
    guard let entity = entity else {return}
    
    // code here for action - print for now
    print("tapped \(entity.name)")
}

}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif

Can you give us more context about your project please?

Better actually if you can share with us your whole project file.

Hi Joash,

Sure. There’s no other views apart from ContentView as above at the moment. The RCproject loads immediately on start of the app. It has some behaviours but I’ve not got that far yet. I’ve only just started with a tap gesture recogniser.

Here’s the project file.

Thanks for your help.

hi @ahowe.CWC

Can you give me more information about what you want to do? Can you help me answer the following?

  • What are you trying to achieve?
  • What have you’ve done so far?
  • What went wrong?

And to give you an update to your source code, I was able to remove the issue about crashing, but I’m not sure if I was able to help with your main concern.

based on the code you provided It looks like you have a syntax issue in your code. The onAction handler should be assigned using a closure. Also, the HandleTapOnEntity function should be a part of the ARViewContainer struct.

Here’s the corrected code:

import SwiftUI
import RealityKit
import ARKit

struct ContentView: View {
    var body: some View {
        ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        // Load Model3 scene
        let anchor = try! Zone3.loadScene()
        
        // Add the anchor to the scene
        arView.scene.anchors.append(anchor)
        
        anchor.generateCollisionShapes(recursive: true)
        
        if let collisionAnchor = anchor as? HasCollision {
            arView.installGestures([.rotation, .translation], for: collisionAnchor)
        } else {
            print("Failed to cast anchor to HasCollision")
        }
        
        anchor.actions.tap.onAction = { entity in
            self.handleTapOnEntity(entity)
        }
        
        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {}
    
    func handleTapOnEntity(_ entity: Entity?) {
        guard let entity = entity else { return }
        
        // code here for action - print for now
        print("tapped \(entity.name)")
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

I don’t know if this is the right view you want to achieve.

Hi Joash,

Thanks for your help. Really appreciated.

I’m expanding on a simple AR app and adding my own RCProject instead of the normal box. That’s done as you see. Now, I’d like to be able to rotate and move that model. The code you gave still doesn’t seem to allow these movements on the model.

The tap function will be used to run a behaviour on the RCProject to make another element of the model appear and disappear with each tap. Again, I’ve not got to that part yet.

I’ve been able to use the modelEntity approach to introduce rotation and translation (no problem), but then I couldn’t make the tap procedure work.

Again, many thanks for the help.