RealityView and Anchors

Hello,

In the documentation for an ARView we see a diagram that shows that all Entity's are connected via AnchorEntity's to the Scene:

https://developer.apple.com/documentation/realitykit/arview

What happens when we are using a RealityView? Here the documentation suggests we direclty add Entity's:

https://developer.apple.com/documentation/realitykit/realityview/

Three questions:

  • Do we need to add Entitys to an AnchorEntity first and add this via content.add(...)?
  • Is an Entity ignored by physics engine if attached via an Anchor?
  • If both the AnchorEntity and an attached Entity is added via content.add(...), does is its anchor's position ignored?
Answered by Vision Pro Engineer in 788913022

AnchorEntity is a subclass of Entity. Think of it as an Entity whose transform (rotation, position) is controlled by ARKit. Since it's an Entity it can have children and their positions are relative to their parent (AnchorEntity). Here's a snippet that creates a sphere, adds it to an AnchorEntity (targeted to the user's right palm) and adds that AnchorEntity to the RealityView's content:

let anchorEntity = AnchorEntity(.hand(.right, location: .palm))
let sphereMesh = MeshResource.generateSphere(radius: 0.1)
let sphereMaterial = SimpleMaterial(color: .red, isMetallic: true)
let sphere = ModelEntity(mesh: sphereMesh, materials: [sphereMaterial])

// make the sphere a child of the AnchorEntity
anchorEntity.addChild(sphere)

// change to position the sphere relative to the AnchorEntity (user's hand).
sphere.position = [0, 0, 0]

content.add(anchorEntity)

Let's answer your questions keeping in mind "an AnchorEntity is an Entity".

  • Generally create a parent/child relationship between the AnchorEntity and the Entity so the entity can be positioned relative to the AnchorEntity.
  • No, child entities respond to physics if they have a PhysicsBodyComponent.
  • Calling content.add causes the passed in entity to be reparented to content which would make AnchorEntity and Entity siblings (not parent/child). Changes to AnchorEntity's transform would have no effect on Entity.

Here's a link to the documentation for AnchorEntity and AnchoringComponent.Target.

Accepted Answer

AnchorEntity is a subclass of Entity. Think of it as an Entity whose transform (rotation, position) is controlled by ARKit. Since it's an Entity it can have children and their positions are relative to their parent (AnchorEntity). Here's a snippet that creates a sphere, adds it to an AnchorEntity (targeted to the user's right palm) and adds that AnchorEntity to the RealityView's content:

let anchorEntity = AnchorEntity(.hand(.right, location: .palm))
let sphereMesh = MeshResource.generateSphere(radius: 0.1)
let sphereMaterial = SimpleMaterial(color: .red, isMetallic: true)
let sphere = ModelEntity(mesh: sphereMesh, materials: [sphereMaterial])

// make the sphere a child of the AnchorEntity
anchorEntity.addChild(sphere)

// change to position the sphere relative to the AnchorEntity (user's hand).
sphere.position = [0, 0, 0]

content.add(anchorEntity)

Let's answer your questions keeping in mind "an AnchorEntity is an Entity".

  • Generally create a parent/child relationship between the AnchorEntity and the Entity so the entity can be positioned relative to the AnchorEntity.
  • No, child entities respond to physics if they have a PhysicsBodyComponent.
  • Calling content.add causes the passed in entity to be reparented to content which would make AnchorEntity and Entity siblings (not parent/child). Changes to AnchorEntity's transform would have no effect on Entity.

Here's a link to the documentation for AnchorEntity and AnchoringComponent.Target.

RealityView and Anchors
 
 
Q