Nested RealityKit entity collisions priority

Hello, I'm struggling trying to interact with a RealityKit entity nested (or at least visually nested) in a second one.

I think I've the same issue as mentioned on this StackOverflow post, but I can't manage to reproduce the solution. https://stackoverflow.com/questions/79244424/how-to-prioritize-a-specific-entity-when-collision-boxes-overlap-in-realitykit

What I'd like to achieve is to translate the red box using a DragGesture, while still be able to interact using a TapGesture on the sphere. Currently, I can only do one at a time.

Does anyone know the solution?

extension CollisionGroup {
    static let parent: CollisionGroup = CollisionGroup(rawValue: 1 << 0)
    static let child: CollisionGroup = CollisionGroup(rawValue: 1 << 1)
}

struct ImmersiveView: View {
    var body: some View {
        RealityView { content in
            let boxMesh = MeshResource.generateBox(size: 0.35)
            let boxMaterial = SimpleMaterial(color: .red.withAlphaComponent(0.25), isMetallic: false)
            let boxEntity = ModelEntity(mesh: boxMesh, materials: [boxMaterial])
            
            let sphereMesh = MeshResource.generateSphere(radius: 0.05)
            let sphereMaterial = SimpleMaterial(color: .blue, isMetallic: false)
            let sphereEntity = ModelEntity(mesh: sphereMesh, materials: [sphereMaterial])
            
            content.add(sphereEntity)
            content.add(boxEntity)
            
            boxEntity.components.set(InputTargetComponent())
            boxEntity.components.set(
                CollisionComponent(
                    shapes: [ShapeResource.generateBox(size: SIMD3<Float>(repeating: 0.35))],
                    isStatic: true,
                    filter: CollisionFilter(
                        group: .parent,
                        mask: .parent.subtracting(.child)
                    )
                )
            )
            
            sphereEntity.components.set(InputTargetComponent())
            sphereEntity.components.set(HoverEffectComponent())
            sphereEntity.components.set(
                CollisionComponent(
                    shapes: [ShapeResource.generateSphere(radius: 0.05)],
                    isStatic: true,
                    filter: CollisionFilter(
                        group: .child,
                        mask: .child.subtracting(.parent)
                    )
                )
            )
        }
    }
}

Hi @mlbonniec

Start with the snippet that answers this post, then make the following changes:

  • Add manipulation component using: container.components.set(ManipulationComponent()).
  • Change the .gesture modifier to .highPriorityGesture. This prevents ManipulationComponent from intercepting the gesture.

If you are not familiar with ManipulationComponent, consider watching the manipulation component section of What's new in RealityKit.

Nested RealityKit entity collisions priority
 
 
Q