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)
                    )
                )
            )
        }
    }
}
Answered by Vision Pro Engineer in 875606022

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.

Accepted Answer

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.

Hi @Vision Pro Engineer, thank you for your response.

I followed the link you provided, and it worked! Thank you very much. ModelSortGroup is the solution.

I won't be using ManipulationComponent because I need visionOS 2.0, but it's a very interesting component.

That said, I encountered a problem with ModelSortGroup in my actual application, not the demo one (where my image comes from).

I couldn't find any explanation, but using generateCollisionShapes(recursive: false) didn't work, whereas my spheres (which are real entities loaded from files) worked when I manually created the collision shape. I tried displaying the collision shapes using visionOS debugging, and both methods seemed to produce exactly the same collision shape, position, etc.

Is there a logical explanation?

Thanks for your help, it really solved my issue.

Just to be clear, are CollisionFilter & CollisionGroup only working for physical collisions and not hit tests?

Nested RealityKit entity collisions priority
 
 
Q