Collision Detection after Object Tracking

So I am tracking 2 objects in my scene, and spawning a tiny arrow on each of the objects (this part is working as intended).

Inside my scene I have added Collision Components and Physics Body Components to each of the arrows.

I want to detect when when a collision occurs between the 2 arrow entities.. I have made the collision boxes big enough so they should definitely be overlapping, however I am not able to detect when the Collision occurs.

This is the code that I use for the scene -

import SwiftUI

import RealityKit

import RealityKitContent

struct DualObjectTrackingTest: View {

@State private var subscription: EventSubscription?

var body: some View {
    RealityView { content in
        if let immersiveContentEntity = try? await Entity(named: "SceneFind.usda", in: realityKitContentBundle) {
            content.add(immersiveContentEntity)
            print("Collision check started")
        }
    } update: { content in
        if let arrow = content.entities.first?.findEntity(named: "WhiteArrow") as? ModelEntity {
            let subscription = content.subscribe(to: CollisionEvents.Began.self, on: arrow) { collisionEvent in
                
                print("Collision has occured")
            }
        }
    }
}

}

All I see in my console logs is "Collision check started" and then whenever I move the 2 objects really close to each other so as to overlap the collision boxes, I don't see any updates in the logs.

Can anyone give me some further guidance/resources on this?

Thanks again!

Additionally, can this even be done? I tracked an object (using the anchoring method and the target being the reference object), then spawn a digital arrow on this tracked object. Now I draw collision boxes around these spawned digital arrows - can I check for collisions between 2 such arrows that are spawned based on 2 different objects being tracked? I read earlier that anchored objects have a different coordinate and physical space, so I'm now questioning if this is the right way to check this. Any suggestions would be very helpful! Thank you

Hello @adityach

By default, AnchorEntity and its children exist in a separate physics simulation, and do not interact with other entities outside its hierarchy. However, AnchoringComponent has a physicsSimulation property that lets you configure this behavior. I suspect you need to set this property to .none, like so:

myAnchorEntity.anchoring.physicsSimulation = .none

This will allow your AnchorEntity and child entities to participate in the same physics simulation as the other entities in your scene.

Additionally, make sure you have successfully started a SpatialTrackingSession, as described in this thread.

Let me know if this helps!

So this is what I have as my SwiftUI code at the moment

import SwiftUI

import RealityKit

import RealityKitContent

struct DualObjectTrackingTest: View {

@State private var subs: [EventSubscription] = []

var body: some View {
    RealityView { content in
        if let immersiveContentEntity = try? await Entity(named: "SceneFind.usda", in: realityKitContentBundle) {
            content.add(immersiveContentEntity)
            print("Collision check started")
        }
    } update: { content in
        //if let cube = content.entities.first?.findEntity(named: "WhiteArrow") as? ModelEntity {
            let event = content.subscribe(to: CollisionEvents.Began.self) { collisionEvent in
                
                print("Collision has occured")
            }
            DispatchQueue.main.async {
                subs.append(event)
            }
        }
    }
}

How do I change the physics components of the entities that are in the scene that I am loading? I have 2 anchor components added to 2 transforms in my scene - Transform1 and Transform2. Both of these transforms have an anchoring component each with the target being set to a reference object anchor that I am using. I don't see any properties that I can configure inside Reality Composer Pro, so if I need to reach these particular transforms that have the anchoring components, how do I do that from inside my code?

It might be a very rudimentary question, but I'm learning swiftUI as I go so some guidance here would be very helpful!

Thank you again

Hey @adityach

You can use the findEntity API to retrieve a named Entity in your loaded scene. Once you have the entity with the component, you can access the AnchoringComponent like you would with any Entity. Something like this might work:

if let immersiveContentEntity = try? await Entity(named: "SceneFind.usda", in: realityKitContentBundle) {
    if let myEntity = immersiveContentEntity.findEntity(named: "MyEntityName"), var anchoring = myEntity.components[AnchoringComponent.self] {
        // do something with anchoring
        anchoring.physicsSimulation = .none
        // remember to set the component back on your entity (it is a value type)
        myEntity.components[AnchoringComponent.self] = anchoring
    }
}

Let me know if this helps!

Apologies for the multiple comments, I have now been able to fix the physics simulation issue. I'm now adding collision boxes and physics body components to the 2 entities that I am spawning on my 2 object anchors, and trying to detect a collision between the two when I place the 2 objects on top of each other. Does not seem to be working at the moment. Any advice on what I can do for this?

This is the code below that I'm using

struct DualObjectTrackingTest: View {

@State private var subs: [EventSubscription] = []

var body: some View {
    RealityView { content in
        if let immersiveContentEntity = try? await Entity(named: "SceneFind.usda", in: realityKitContentBundle) {
            content.add(immersiveContentEntity)
            print("Collision check started")
            
            if let Anchor1 = immersiveContentEntity.findEntity(named: "Object1"),
               var transform1 = Anchor1.components[AnchoringComponent.self],
               let Anchor2 = immersiveContentEntity.findEntity(named: "Object2"),
               var transform2 = Anchor2.components[AnchoringComponent.self] {
                transform1.physicsSimulation = .none
                transform2.physicsSimulation = .none
            }
        }
    } update: { content in
        let event = content.subscribe(to: CollisionEvents.Began.self) { collisionEvent in
            
            print("Collision HAS OCCURED!")
        }
            DispatchQueue.main.async {
                subs.append(event)
            }
        }
    }
}

I don't see any console logs indicating that the collision has occurred, even though the entities and their components (collision boxes) should technically be in the same coordinate and physical space..

Collision Detection after Object Tracking
 
 
Q