Use anchors to make virtual objects appear to stay in place in an AR scene.
Why use anchors?
As ARCore’s environmental understanding updates throughout an AR experience, virtual objects can appear to drift away from where they were placed. This can impact your app's realism and user experience.
Anchors ensure that objects appear to stay at the same position and orientation in space, helping you maintain the illusion of virtual objects placed in the real world.
How anchors work
If you are new to using anchors, it is helpful to review world space and poses.
- Coordinate space in which the camera and objects are positioned
- Camera and object positions are updated in world space from frame to frame
- Represents an object’s position and orientation in world space
- Also referred to as a transform in iOS
When you create an anchor, you use a pose that describes a position and orientation relative to the world space estimate for the current frame.
You attach one or more objects to this anchor. The anchor and the objects attached to it appear to stay where they are placed in the world. As the anchor pose adapts to world space updates in each frame, the anchor updates the objects' poses accordingly.
You can attach multiple objects to the same anchor to ensure that these objects maintain their relative position and orientation, even when the anchor's pose adjusts.
Use anchors in your scene
To use anchors in your scene, your code should:
- Create anchors in the context of a Trackable (such as a Plane) or the ARCore Session.
- Attach one or more objects to the anchor.
Anchors can support different kinds of positional behavior in your scene's objects.
Determining anchor context and how many anchors you need for your scene's objects depends on the positional behavior that your AR scene requires. See the following sections for more information.
Pick an anchor context
Add anchors to your scene using a Trackable or the ARCore Session.
|When you want objects to
|Attach the anchor to the
|Appear "welded" to the Trackable and be subject to the
same rotational effects as the Trackable.
|Appear to stay at the same pose in world space throughout the user experience
Anchor one or more objects
You can attach one or more objects to an anchor. Generally, these objects have a spatial relationship to one or more of the following:
- Each other
- A Trackable, such as a Plane
- A position in world space
Your app's realism and performance benefit from using anchors effectively and efficiently.
Objects attached to a nearby anchor appear to keep their place realistically and relative to each other throughout the AR experience.
Using only the anchors that you need helps to reduce CPU costs.
Reuse anchors when possible
In most cases, you should use the same anchor for multiple nearby objects rather than creating a new anchor for each object.
Use a new anchor for an object if the object needs to maintain a unique spatial relationship to a Trackable or a position in world space.
Keep in mind that, if each object in a scene has its own anchor, these anchors will adjust the object poses independently of one another in response to ARCore's estimate of world space in each frame. Separately anchored objects can shift or rotate relative to each other, breaking the illusion of an AR scene where virtual objects should stay in place relative to each other.
For example, imagine that your app lets users place virtual furniture pieces around a room. A user opens the app and ARCore begins tracking a tabletop and a floor in the room as Planes. The user positions a virtual lamp on the tabletop. Then, the user puts a virtual chair on the floor.
At this point, your scene should have one anchor attached to the tabletop Plane and another attached to the floor Plane.
If the user adds another virtual lamp to the tabletop, you can reuse the anchor already attached to the tabletop Plane. This way, both lamps appear to stick to the tabletop Plane and keep their places relative to one another. The chair also maintains its position relative to the floor Plane.
Keep objects close to anchors
When anchoring objects, make sure that they are close to the anchor you are using. Avoid placing objects farther than eight meters (8m) or twenty-six feet (26 ft.) from the anchor to prevent unexpected rotational movement due to ARCore's updates to world space coordinates.
If you need to place an object more than eight meters away from an existing anchor, create a new anchor closer to this position and attach the object to the new anchor.
Detach unused anchors
Detach any anchors that you no longer need so that your app is more performant.
Your app incurs a CPU cost for every Trackable, and ARCore does not release Trackables that have attached anchors.
Types of anchors
Anchor types are differentiated by the scope in which they are valid, from local to world-wide.
Local anchors are stored with the app locally, and valid only for that instance of the app. The user must be physically at the location where they are placing the anchor.
Cloud Anchors are stored in Google Cloud and may be shared between app instances. The user must be physically at the location where they are placing the anchor.
Geospatial anchors are based on geodetic latitude, longitude, and altitude, plus Google's Visual Positioning System (VPS) data, to provide precise location almost anywhere in the world. The user may place an anchor from a remote location as long as the app is connected to the internet and able to use the VPS.