Hi,
I'm new to realitykit and still learning. I'm trying to implement a feature on visionOS that triggers specific logic when the user's head comes into contact with another entity. When two entities are added directly to the realityView, I am able to subscribe to their collision event correctly, but when I add one of the entities to an anchorEntity that is anchored on the user's head, I am unable to receive the collision subscription. , and I found that if an entity declares that it obeys the hasAnchor protocol, it cannot participate in collision detection normally either. Why does this happen? Is this a feature or a bug?
Here is how I subscribe to collision events:
collisionSubscription = content.subscribe(to: CollisionEvents.Began.self, on: nil, componentType: nil) { collisionEvent in
print("đź’Ą Collision between \(collisionEvent.entityA.name) and \(collisionEvent.entityB.name)")
}
the following two entity collides fine:
@State private var anotherEntity : Entity = CollisionEntity(model: MeshResource.generateSphere(radius: 1), materials: [SimpleMaterial(color: .white, isMetallic: false)], position: [-2,1,0])
@State private var headEntity : Entity = CollisionEntity(model: MeshResource.generateSphere(radius: 0.5), materials: [SimpleMaterial(color: .yellow, isMetallic: false)], position: [0, -0.35, -3])
but with anchoring, I can't get collision notifications
@State private var anotherEntity : Entity = CollisionEntity(model: MeshResource.generateSphere(radius: 1), materials: [SimpleMaterial(color: .white, isMetallic: false)], position: [-2,1,0])
@State private var headEntity : Entity = {
let headAnchor = AnchorEntity(.head)
headAnchor.addChild( CollisionEntity(model: MeshResource.generateSphere(radius: 0.5), materials: [SimpleMaterial(color: .yellow, isMetallic: false)], position: [0, -0.35, -3]))
return headAnchor
}
Any information or suggestion are welcomed, thanks!
Hello,
This is a common misunderstanding. AnchorEntity's exist in an independent coordinate space and an independent physics simulation, they cannot collide with anything outside of their own hierarchy.
If you want to "anchor" an Entity to the user's head and have that anchor in the same coordinate space and physics simulation as the rest of your scene, you should use queryDeviceAnchor(atTimestamp:) to get the device pose (i.e. head position and orientation), and then update the transform of your custom head anchor Entity accordingly.