0

So thanks to this answer from Mr. Jazz, I have been able to make all objects in one Reality Composer scene, draggable.

Before making them draggable, I had added behaviors to two of the model entities. It worked perfectly: when I opened the app, the scene waa visible and when I clicked on the blue rectangle, it disappeared; when I clicked on the green button, the button moved up and down. The code below worked for the scene where the model entities have NOT been grouped.

  let cubesBlueAnchor = try! Experience.loadCubesBlue()
            self.arView.scene.anchors.append(cubesBlueAnchor)
            cubesBlueAnchor.actions.behavior.onAction = handleTapOnEntity(_:)
            

See below the screenshot of the Reality Composer behavior screen, I have used the same behaviors for both scenes.

enter image description here

However, once they are grouped in realitykit , their behavior is no longer accessible. I am thinking this is something I need Combine for, but I am not sure. Perhaps there is a simpler solution.

I have copied the scene with the same exact behaviors where the blueCubes is the scene where the haven't been grouped/can't be dragged but the behaviors work.

In the cubesScene, the objects are draggable and scalable, but their behaviors can't be accessed. This is the code that is working.

   let cubesAnchor = try! Experience.loadCubes()
    arView.scene.anchors.append(cubesAnchor)
    print (cubesAnchor)
    
  
    let redModel =  cubesAnchor.children[0].children[0]
    
    
    let group = ModelEntity() as ModelEntity & HasCollision
    group.addChild(redModel)
    
    
    group.generateCollisionShapes(recursive: false)
    self.arView.installGestures(.all, for: group)
    
    let shape = ShapeResource.generateBox(width: 1, height: 1, depth: 1)
    let collision = CollisionComponent(shapes: [shape],
                                       mode: .trigger,
                                       filter: .sensor)
    group.components.set(collision)
    
    let anchor = AnchorEntity()
    anchor.addChild(group)
    anchor.scale = [5,5,5]
    arView.scene.anchors.append(anchor)
        
      

See a screenshot of the phone's screen, showing both. ( The screen capture shows the blueCubes behavior where the blue rectangle is disappearing.

enter image description here enter image description here

I want to be able to do both - have objects grouped under one entity where they can be dragged AND have the behavior for each model entity fired when I click on each one of them, on the same scene.

But I don't know how to go about it. Maybe removing the collisions when the object is not being dragged?

See the full code below

import UIKit
import RealityKit

class ViewController: UIViewController {
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.arView.debugOptions = .showPhysics
        
        let cubesAnchor = try! Experience.loadCubes()
        arView.scene.anchors.append(cubesAnchor)
        print (cubesAnchor)
      
        let redModel =  cubesAnchor.children[0].children[0]
    
        let group = ModelEntity() as ModelEntity & HasCollision
        group.addChild(redModel)
        
        group.generateCollisionShapes(recursive: false)
        self.arView.installGestures(.all, for: group)
        
        let shape = ShapeResource.generateBox(width: 1, height: 1, depth: 1)
        let collision = CollisionComponent(shapes: [shape],
                                           mode: .trigger,
                                           filter: .sensor)
        group.components.set(collision)
        
        let anchor = AnchorEntity()
        anchor.addChild(group)
        anchor.scale = [5,5,5]
        arView.scene.anchors.append(anchor)
        
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
            //self.arView.scene.anchors.removeAll()
            let cubesBlueAnchor = try! Experience.loadCubesBlue()
            self.arView.scene.anchors.append(cubesBlueAnchor)
            cubesBlueAnchor.actions.behavior.onAction = handleTapOnEntity(_:)

        }
        
        
        func handleTapOnEntity(_ entity: Entity?){
            guard let entity = entity else { return }
            
        }
        
        
        
    }
}
Scott Thompson
  • 22,629
  • 4
  • 32
  • 34

0 Answers0